Multipart Email允许添加附件和内部资源,内部资源可能是你在信件中希望使用的图像或样式表,但又不想把它们作为附件
一、附件:
接口:见MailSender和SimpleMailMessage的基本用法
实现类:
package com.mail.sender;
import com.mail.service.OrderManager;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.lang.management.MonitorInfo;
/** Multipart Email-添加附件
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 12-6-13
* Time: 上午11:19
* To change this template use File | Settings | File Templates.
*/
public class MimeHelper implements OrderManager {
private JavaMailSender javaMailSender;
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void placeOrder() throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message,true); //使用Multipart格式
} catch (MessagingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
FileSystemResource file = new FileSystemResource(new File("C:\\Users\\Administrator\\Pictures\\1.png"));
try {
helper.setTo("240609256@qq.com");//toXXX邮箱
helper.setFrom("hanxiaoying@iplamp.com"); //from XXX邮箱
helper.setSubject("helper");
helper.setText("hello");
helper.addAttachment("1.png",file); //添加附件
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
配置文件见:MimeMessageHelper的基本用法
的配置文件
测试没有异常就成功了
效果如下:
二、内部资源
实现类:
package com.mail.sender;
import com.mail.service.OrderManager;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 12-6-13
* Time: 下午1:51
* To change this template use File | Settings | File Templates.
*/
public class InResource implements OrderManager {
private JavaMailSender javaMailSender;
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void placeOrder() throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true); //使用Multipart格式
} catch (MessagingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
FileSystemResource file = new FileSystemResource(new File("C:\\Users\\Administrator\\Pictures\\1.png"));
try {
helper.setTo("240609256@qq.com");//toXXX邮箱
helper.setFrom("hanxiaoying@iplamp.com"); //from XXX邮箱
helper.setSubject("helper");
helper.setText("hello,看图:<html><body><img src='cid:identifier1234'></body></html>", true);//true 表示启动HTML格式的邮件
helper.addInline("identifier1234", file); //添加附件
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
配置文件:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.exmail.qq.com"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="username" value="hanxiaoying@iplamp.com"/>
<property name="password" value="hanxiaoying0303"/>
<property name="javaMailProperties">
<value>
mail.smtp.auth=true
</value>
</property>
</bean>
<bean id="inResource" class="com.mail.sender.InResource">
<property name="javaMailSender" ref="mailSender"/>
</bean>
测试一下,看下有没有成功:
- 大小: 23.3 KB
- 大小: 6.5 KB
分享到:
相关推荐
NULL 博文链接:https://huangyongxing310.iteye.com/blog/2321943
通过import org.springframework.web.multipart.MultipartFile;上传文件的一个工具性的jar.
org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest.class org.springframework.web.multipart.support.ByteArrayMultipartFileEditor.class org.springframework.web.multipart....
spring-boot-multipart-file-upload 使用Spring boot检查multipart request的操作
c#没有现成的multipart/form-data库,自己封的一个demo供参考。
6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...
当使用Spring MVC或Spring WebFlux框架的应用收到一个客户端请求,并用它来向另一个服务端发送multipart请求时,攻击者可利用该漏洞往里插入恶意内容。该漏洞的利用有一定的限制,要求攻击者能够猜到multipart字段的...
文件上传下载 根据multipart类的方法: 即multipartFile.transferTo(file); 就完成文件上传
13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置标签库 13.9.2. form标签 ...
Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack ...19.2. 使用Spring JMS ...
13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置 13.9.2. form标签 ...
使用indy自带的idhttp控件,用form表单(multipart/form-data)形式上传文件(图片,视频等)
13.8. Spring对分段文件上传(multipart file upload)的支持 13.8.1. 介绍 13.8.2. 使用MultipartResolver 13.8.3. 在表单中处理分段文件上传 13.9. 使用Spring的表单标签库 13.9.1. 配置 13.9.2. form标签 ...
本人愚钝,实验了好久,才将spring4.x的RESTful使用实验出来,欣喜之余,和大家分享一下经验。
WCF 是微软力推的通讯框架,它能够以 ASP.NET WebService 的方式实现 RESTful 方式的服务。但是缺陷是目前的版本不支持对 HTTP multipart 格式的自动解析...关于代码的使用方式,请参见 Vodurden 在 GitHub 上的说明。
前端开源库-fastify-multipartfastfy multipart,fastfy的multipart插件
用C语言实现multipart/form-data文件上传,没有用到curl之类的库。之前做个小的日志上传程序写的。
<form method="post" ENCTYPE="multipart/form-data"> file类型职务的普通参数传递到后台问题解决
解决java enctype multipart form-data文件上传传值问题 不使用jspsmartupload组件 可以轻轻松松 解决传值问题
小程序官方提供的api wx.uploadFile一次只能上传一个文件,Multipart实现了一般的表单上传,无文件数量限制