原文地址:http://www.open-open.com/lib/view/open1346857871615.html
使用Java Mail API来发送邮件也很容易实现,但是最近公司一个同事封装的邮件API实在让我无法接受,于是便打算改用Spring Mail API来发送邮件,顺便记录下这篇文章。
【Spring Mail API】
Spring Mail API都在org.springframework.mail及其子包org.springframework.mail.javamail中封装,且只提供了邮件发送的封装。
SimpleMailMessage: 对邮件的一个简单封装,只能用于表示一个纯文本的邮件,也不能包含附件等。
JavaMailSenderImpl: 邮件发送器,主要提供了邮件发送接口、透明创建Java Mail的MimeMessage、及邮件发送的配置(如:host/port/username/password...)。
MimeMailMessage、MimeMessageHelper:对MimeMessage进行了封装。Spring还提供了一个回调接口MimeMessagePreparator, 用于准备JavaMail的MIME信件
一下代码转载自:http://www.blogjava.net/tangzurui/archive/2008/12/08/244953.html
1.发送简单的文本邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package net.xftzr.mail;
import java.util.Properties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/** * 本类测试简单邮件 直接用邮件发送
*
* @author Administrator
*
*/
public class SingleMailSend
{ public static void main(String args[])
{
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定mail server
senderImpl.setHost( " smtp.163.com " );
// 建立邮件消息
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 设置收件人,寄件人 用数组发送多个邮件
// String[] array = new String[] {"sun111@163.com<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script>","sun222@sohu.com<script cf-hash="f9e31" type="text/javascript"> /* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script>"}; // mailMessage.setTo(array);
mailMessage.setTo(" toEmail@sina.com<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script> "); mailMessage.setFrom(" userName@163.com<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */ </script> ");
mailMessage.setSubject( " 测试简单文本邮件发送! " );
mailMessage.setText( " 测试我的简单邮件发送机制!! " );
senderImpl.setUsername( " userName " ); // 根据自己的情况,设置username
senderImpl.setPassword( " password " ); // 根据自己的情况, 设置password
Properties prop = new Properties();
prop.put( " mail.smtp.auth " , " true " ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
prop.put( " mail.smtp.timeout " , " 25000 " );
senderImpl.setJavaMailProperties(prop);
// 发送邮件
senderImpl.send(mailMessage);
System.out.println( " 邮件发送成功.. " );
}
} |
2.发送简单的html邮件
org.springframework.mail.javamail.MimeMessageHelper是处理JavaMail邮件常用的顺手组件之一。它可以让你摆脱繁复的javax.mail.internetAPI类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package net.xftzr.mail;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
/** * 本类测试html邮件
*
* @author sunny
*
*/
public class HTMLMailDemo
{ /**
* @param args
*/
public static void main(String[] args) throws Exception
{
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定mail server
senderImpl.setHost( "smtp.163.com" );
// 建立邮件消息,发送简单邮件和html邮件的区别
MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
// 设置收件人,寄件人
messageHelper.setTo( "Mailto@sina.com<script cf-hash=" f9e31 " type=" text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script>"); messageHelper.setFrom("username@163.com<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */ </script>");
messageHelper.setSubject( "测试HTML邮件!" );
// true 表示启动HTML格式的邮件
messageHelper
.setText(
"<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>" ,
true );
senderImpl.setUsername( "username" ); // 根据自己的情况,设置username
senderImpl.setPassword( "password" ); // 根据自己的情况, 设置password
Properties prop = new Properties();
prop.put( "mail.smtp.auth" , "true" ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
prop.put( "mail.smtp.timeout" , "25000" );
senderImpl.setJavaMailProperties(prop);
// 发送邮件
senderImpl.send(mailMessage);
System.out.println( "邮件发送成功.." );
}
} |
3.发送嵌套图片的邮件
Email允许添加附件,也允许在multipart信件中内嵌资源。内嵌资源可能是你在信件中希望使用的图像,或者样式表,但是又不想把它们作为附件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package net.xftzr.mail;
import java.io.File;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
/** * 本类测试邮件中嵌套图片
*
* @author sunny
*
*/
public class AttachedImageMail
{ public static void main(String[] args) throws Exception
{
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定mail server
senderImpl.setHost( "smtp.163.com" );
// 建立邮件消息,发送简单邮件和html邮件的区别
MimeMessage mailMessage = senderImpl.createMimeMessage();
// 注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,
// multipart模式
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
true );
// 设置收件人,寄件人
messageHelper.setTo( "toMail@sina.com<script cf-hash=" f9e31 " type=" text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script>"); messageHelper.setFrom("username@163.com<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */ </script>");
messageHelper.setSubject( "测试邮件中嵌套图片!!" );
// true 表示启动HTML格式的邮件
messageHelper.setText(
"<html><head></head><body><h1>hello!!spring image html mail</h1>"
+ "<img src=\"cid:aaa\"/></body></html>" , true );
FileSystemResource img = new FileSystemResource( new File( "g:/123.jpg" ));
messageHelper.addInline( "aaa" , img);
senderImpl.setUsername( "username" ); // 根据自己的情况,设置username
senderImpl.setPassword( "password" ); // 根据自己的情况, 设置password
Properties prop = new Properties();
prop.put( "mail.smtp.auth" , "true" ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
prop.put( "mail.smtp.timeout" , "25000" );
senderImpl.setJavaMailProperties(prop);
// 发送邮件
senderImpl.send(mailMessage);
System.out.println( "邮件发送成功.." );
}
} |
说明:嵌入图片<img src=\"cid:aaa\"/>,其中cid:是固定的写法,而aaa是一个contentId。
4.发送包含附件的邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package net.xftzr.mail;
import java.io.File;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
public class AttachedFileMail
{ /**
* 本类测试的是关于邮件中带有附件的例子
*
* @param args
*/
public static void main(String[] args) throws Exception
{
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定mail server
senderImpl.setHost( "smtp.163.com" );
// 建立邮件消息,发送简单邮件和html邮件的区别
MimeMessage mailMessage = senderImpl.createMimeMessage();
// 注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,
// multipart模式 为true时发送附件 可以设置html格式
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,
true , "utf-8" );
// 设置收件人,寄件人
messageHelper.setTo( "toMail@sina.com<script cf-hash=" f9e31 " type=" text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script>"); messageHelper.setFrom("username@163.com<script cf-hash="f9e31" type="text/javascript">
/* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */ </script>");
messageHelper.setSubject( "测试邮件中上传附件!!" );
// true 表示启动HTML格式的邮件
messageHelper.setText(
"<html><head></head><body><h1>你好:附件中有学习资料!</h1></body></html>" ,
true );
FileSystemResource file = new FileSystemResource(
new File( "g:/test.rar" ));
// 这里的方法调用和插入图片是不同的。
messageHelper.addAttachment( "test.rar" , file);
senderImpl.setUsername( "username" ); // 根据自己的情况,设置username
senderImpl.setPassword( "password" ); // 根据自己的情况, 设置password
Properties prop = new Properties();
prop.put( "mail.smtp.auth" , "true" ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
prop.put( "mail.smtp.timeout" , "25000" );
senderImpl.setJavaMailProperties(prop);
// 发送邮件
senderImpl.send(mailMessage);
System.out.println( "邮件发送成功.." );
}
}
|
相关推荐
最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring推出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了现在的spring-boot-starter-mail。
最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring推出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了现在的spring-boot-starter-mail。 一、注册并登录...
由于的需求,实现了一个小的邮件发送接口服务,使用了一个单独的SpringBoot项目,给他取名为spring-mail-sender,开源在了Github上。内部我给他命名为提醒服务,因为未来希望还能扩展下,用来做短信发送服务,或者...
这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、...
RMI Hessian、 Burlap Http Invoker <br>邮件 对于邮件发送服务的支援是由Spring的 org.springframework.mail.MailSender介面所定义,它有两个实作类别, org.springframework.mail.cos....
Spring Boot Quartz Scheduler示例:构建电子邮件调度应用 完整的教程: : 要求 Java-1.8.x Maven-3.xx MySQL-5.xx 设定步骤 1.克隆应用程序 git clone ...
通过Spring提供的Java Mail服务API来完成发邮件的功能,超级简单哦! 受上传文件大小限制,本人没能将jar包没能一起打包上传,但只需把SSH所需jar包拷贝到/WEB-INF/lib/目录下即可运行!
简单Java邮件是使用最简单的轻量级Java邮件库,同时能够发送复杂的电子邮件,包括,经过(!),,,, ,,甚至, 和具有属性覆盖的, 和工具。 只需发送电子邮件即可,而无需处理 。 Simple Java Mail库是之上的...
使用 Twilio 通过电子邮件发送 SMS 消息。 一个 node.js 脚本。下面是我的服务器环境列表。 CentOS 发布 5.3 32 位。 发送邮件 8.13.8-2.el5。 节点 v0.10.33。 您还需要一个帐户。 我的服务器主机名是“zen1....
MailService提供了Rest API来发送电子邮件,并使用Kafka以异步方式处理电子邮件发送过程。 它侦听api/v1/email端点以发送电子邮件。 它将事件发布到Kafka主题,然后由Kafka Consumer处理该事件。 概述 创建该代码库...
笔记记录应用程序,允许用户添加笔记,生成下载PDF并通过电子邮件发送笔记。 应用程序 : : 使用的技术和库 1,Spring MVC 2,Hibernate + MySql 3,Hibernate验证器 4,JavaMail API 5.iTextPDF库 6.html,css...
最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring推出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了现在的spring-boot-starter-mail,本章文章的介绍...
Java Mail API通过SMTP发送HTML电子邮件 JSTL 要求 Java Oracle 阿帕奇Tomcat 玛文 配置 在“ application.properties”中更改SMTP详细信息 登录详细信息 行政 用户名: 密码:admin 顾客 用户名: 密码:用户 ...
邮箱验证使用Spring-mail提供的API处理,邮件发送服务器[application.yml](src/main/resources/application.yml)进行配置。 ### 整合redis 使用Spring-redis-template进行redis操作,redis用来存储用户收藏文章信息和...
springboot+mail实现邮件发送 18. springboot + mybatis + mapper + pageHealper 多数据源 19. Springboot集成SpringRestdocs生成Api文档 20. Springboot开启HTTPS 21. Springboot整合Swagger2 22. Springboot...
技术栈: 安卓(客户端) 改造(用于从客户端发送 post/get 请求) Spring MVC(在服务器端开发 Rest Api 的框架) MySQL(用于数据库) Jackson(用于数据编组和解组) Java Mail Api(用于从服务器端发送邮件) ...
Lumos是基于Spring Boot的轻量级博客,是学习搭建使用Spring Boot的不错选择。 Lumos项目特点: 1、基于 Spring Boot 部署简单。 2、支持主库备库动态切换。 3、支持博客异常邮件提醒。 4、支持发送每天博客方法调...
同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-mail账号,查询mail 服务器的会话…… 还包括消息客户端程序,通过连接创建会话。创建发送者和映射消息。发送消息,同时对...
同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-mail账号,查询mail 服务器的会话…… 还包括消息客户端程序,通过连接创建会话。创建发送者和映射消息。发送消息,同时对...