使用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.发送简单的文本邮件
01 | package net.xftzr.mail; |
03 | import java.util.Properties; |
05 | import org.springframework.mail.SimpleMailMessage; |
06 | import org.springframework.mail.javamail.JavaMailSenderImpl; |
11 | * @author Administrator |
14 | public class SingleMailSend |
16 | public static void main(String args[]) |
18 | JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); |
20 | senderImpl.setHost( " smtp.163.com " ); |
23 | SimpleMailMessage mailMessage = new SimpleMailMessage(); |
24 | // 设置收件人,寄件人 用数组发送多个邮件 |
25 | // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"}; |
26 | // mailMessage.setTo(array); |
27 | mailMessage.setTo( " toEmail@sina.com " ); |
28 | mailMessage.setFrom( " userName@163.com " ); |
29 | mailMessage.setSubject( " 测试简单文本邮件发送! " ); |
30 | mailMessage.setText( " 测试我的简单邮件发送机制!! " ); |
32 | senderImpl.setUsername( " userName " ); // 根据自己的情况,设置username |
33 | senderImpl.setPassword( " password " ); // 根据自己的情况, 设置password |
35 | Properties prop = new Properties(); |
36 | prop.put( " mail.smtp.auth " , " true " ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确 |
37 | prop.put( " mail.smtp.timeout " , " 25000 " ); |
38 | senderImpl.setJavaMailProperties(prop); |
40 | senderImpl.send(mailMessage); |
42 | System.out.println( " 邮件发送成功.. " ); |
2.发送简单的html邮件 org.springframework.mail.javamail.MimeMessageHelper是处理JavaMail邮件常用的顺手组件之一。它可以让你摆脱繁复的javax.mail.internetAPI类
01 | package net.xftzr.mail; |
03 | import java.util.Properties; |
05 | import javax.mail.internet.MimeMessage; |
06 | import org.springframework.mail.javamail.JavaMailSenderImpl; |
07 | import org.springframework.mail.javamail.MimeMessageHelper; |
15 | public class HTMLMailDemo |
20 | public static void main(String[] args) throws Exception |
22 | JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); |
25 | senderImpl.setHost( "smtp.163.com" ); |
27 | // 建立邮件消息,发送简单邮件和html邮件的区别 |
28 | MimeMessage mailMessage = senderImpl.createMimeMessage(); |
29 | MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); |
32 | messageHelper.setTo( "Mailto@sina.com" ); |
33 | messageHelper.setFrom( "username@163.com" ); |
34 | messageHelper.setSubject( "测试HTML邮件!" ); |
38 | "<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>" , |
41 | senderImpl.setUsername( "username" ); // 根据自己的情况,设置username |
42 | senderImpl.setPassword( "password" ); // 根据自己的情况, 设置password |
43 | Properties prop = new Properties(); |
44 | prop.put( "mail.smtp.auth" , "true" ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确 |
45 | prop.put( "mail.smtp.timeout" , "25000" ); |
46 | senderImpl.setJavaMailProperties(prop); |
48 | senderImpl.send(mailMessage); |
50 | System.out.println( "邮件发送成功.." ); |
3.发送嵌套图片的邮件Email允许添加附件,也允许在multipart信件中内嵌资源。内嵌资源可能是你在信件中希望使用的图像,或者样式表,但是又不想把它们作为附件。
01 | package net.xftzr.mail; |
04 | import java.util.Properties; |
06 | import javax.mail.internet.MimeMessage; |
07 | import org.springframework.core.io.FileSystemResource; |
08 | import org.springframework.mail.javamail.JavaMailSenderImpl; |
09 | import org.springframework.mail.javamail.MimeMessageHelper; |
17 | public class AttachedImageMail |
19 | public static void main(String[] args) throws Exception |
21 | JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); |
24 | senderImpl.setHost( "smtp.163.com" ); |
26 | // 建立邮件消息,发送简单邮件和html邮件的区别 |
27 | MimeMessage mailMessage = senderImpl.createMimeMessage(); |
28 | // 注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用, |
30 | MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, |
34 | messageHelper.setTo( "toMail@sina.com" ); |
35 | messageHelper.setFrom( "username@163.com" ); |
36 | messageHelper.setSubject( "测试邮件中嵌套图片!!" ); |
38 | messageHelper.setText( |
39 | "<html><head></head><body><h1>hello!!spring image html mail</h1>" |
40 | + "<img src=\"cid:aaa\"/></body></html>" , true ); |
42 | FileSystemResource img = new FileSystemResource( new File( "g:/123.jpg" )); |
44 | messageHelper.addInline( "aaa" , img); |
46 | senderImpl.setUsername( "username" ); // 根据自己的情况,设置username |
47 | senderImpl.setPassword( "password" ); // 根据自己的情况, 设置password |
48 | Properties prop = new Properties(); |
49 | prop.put( "mail.smtp.auth" , "true" ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确 |
50 | prop.put( "mail.smtp.timeout" , "25000" ); |
51 | senderImpl.setJavaMailProperties(prop); |
54 | senderImpl.send(mailMessage); |
56 | System.out.println( "邮件发送成功.." ); |
说明:嵌入图片<img src=\"cid:aaa\"/>,其中cid:是固定的写法,而aaa是一个contentId。
4.发送包含附件的邮件
01 | package net.xftzr.mail; |
04 | import java.util.Properties; |
06 | import javax.mail.internet.MimeMessage; |
07 | import org.springframework.core.io.FileSystemResource; |
08 | import org.springframework.mail.javamail.JavaMailSenderImpl; |
09 | import org.springframework.mail.javamail.MimeMessageHelper; |
11 | public class AttachedFileMail |
18 | public static void main(String[] args) throws Exception |
20 | JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); |
23 | senderImpl.setHost( "smtp.163.com" ); |
24 | // 建立邮件消息,发送简单邮件和html邮件的区别 |
25 | MimeMessage mailMessage = senderImpl.createMimeMessage(); |
26 | // 注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用, |
27 | // multipart模式 为true时发送附件 可以设置html格式 |
28 | MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, |
32 | messageHelper.setTo( "toMail@sina.com" ); |
33 | messageHelper.setFrom( "username@163.com" ); |
34 | messageHelper.setSubject( "测试邮件中上传附件!!" ); |
36 | messageHelper.setText( |
37 | "<html><head></head><body><h1>你好:附件中有学习资料!</h1></body></html>" , |
40 | FileSystemResource file = new FileSystemResource( |
41 | new File( "g:/test.rar" )); |
43 | messageHelper.addAttachment( "test.rar" , file); |
45 | senderImpl.setUsername( "username" ); // 根据自己的情况,设置username |
46 | senderImpl.setPassword( "password" ); // 根据自己的情况, 设置password |
47 | Properties prop = new Properties(); |
48 | prop.put( "mail.smtp.auth" , "true" ); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确 |
49 | prop.put( "mail.smtp.timeout" , "25000" ); |
50 | senderImpl.setJavaMailProperties(prop); |
52 | senderImpl.send(mailMessage); |
54 | System.out.println( "邮件发送成功.." ); |
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。