qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

利用Java实现电子邮件的批量发送

JAVA MAIL是利用现有的邮件账户发送邮件的工具,比如说,我在网易注册一个邮箱账户,通过JAVA Mail的操控,我可以不亲自登录网易邮箱,让程序自动的使用网易邮箱发送邮件。这一机制被广泛的用在注册激活和垃圾邮件的发送等方面。进行下载,并将mail.jar添加到classpath即可。如果你使用的是JAVA EE SDK,则可以在C:glassfishv3glassfishmodulesmail.jar找到所需的jar包,同样需要添加的classpath。

  JAVA邮件发送的大致过程是这样的的:

  1、构建一个继承自javax.mail.Authenticator的具体类,并重写里面的getPasswordAuthentication()方法。此类是用作登录校验的,以确保你对该邮箱有发送邮件的权利。

  2、构建一个properties文件,该文件中存放SMTP服务器地址等参数。

  3、通过构建的properties文件和javax.mail.Authenticator具体类来创建一个javax.mail.Session。Session的创建,就相当于登录邮箱一样。剩下的自然就是新建邮件。

  4、构建邮件内容,一般是javax.mail.internet.MimeMessage对象,并指定发送人,收信人,主题,内容等等。

  5、使用javax.mail.Transport工具类发送邮件。

  下面是我封装的代码,注释也比较详细。呼呼~~

  1、首先是继承自javax.mail.Authenticator的一个具体类。getPasswordAuthentication()方法也就是构建一个PasswordAuthentication对象并返回,有点费解JAVA Mail这样的设计意图,可能是javax.mail.Authenticator为我们提供了附加的保证安全的验证措施吧。

  1. package com.mzule.simplemail;  
  2. import javax.mail.Authenticator;  
  3. import javax.mail.PasswordAuthentication;  
  4. /** 
  5. * 服务器邮箱登录验证 
  6. * 
  7. * @author MZULE 
  8. * 
  9. */ 
  10. public class MailAuthenticator extends Authenticator {  
  11. /** 
  12. * 用户名(登录邮箱) 
  13. */ 
  14. private String username;  
  15. /** 
  16. * 密码 
  17. */ 
  18. private String password;  
  19. /** 
  20. * 初始化邮箱和密码 
  21. * 
  22. * @param username 邮箱 
  23. * @param password 密码 
  24. */ 
  25. public MailAuthenticator(String username, String password) {  
  26. this.username = username;  
  27. this.password = password;  
  28. }  
  29. String getPassword() {  
  30. return password;  
  31. }  
  32. @Override 
  33. protected PasswordAuthentication getPasswordAuthentication() {  
  34. return new PasswordAuthentication(username, password);  
  35. }  
  36. String getUsername() {  
  37. return username;  
  38. }  
  39. public void setPassword(String password) {  
  40. this.password = password;  
  41. }  
  42. public void setUsername(String username) {  
  43. this.username = username;  
  44. }  
  45. }

 2、邮件发送类,剩下的步骤都是在这个类实现的。代码中的SimpleMail是封装了邮件主题和内容的一个POJO。觉得在一个方法参数中既包含主题又包含内容,不太合适,故重载了此方法。还有就是因为大多数邮箱的SMTP服务器地址都是可以通过邮箱地址算出来,简单起见,提供了一个不需要SMTP服务器地址的构造器。

  1. package com.mzule.simplemail;  
  2. import java.util.List;  
  3. import java.util.Properties;  
  4. import javax.mail.MessagingException;  
  5. import javax.mail.Session;  
  6. import javax.mail.Transport;  
  7. import javax.mail.internet.AddressException;  
  8. import javax.mail.internet.InternetAddress;  
  9. import javax.mail.internet.MimeMessage;  
  10. import javax.mail.internet.MimeMessage.RecipientType;  
  11. /**  
  12. * 简单邮件发送器,可单发,群发。  
  13.  
  14. * @author MZULE  
  15.  
  16. */ 
  17. public class SimpleMailSender {  
  18. /**  
  19. * 发送邮件的props文件  
  20. */ 
  21. private final transient Properties props = System.getProperties();  
  22. /**  
  23. * 邮件服务器登录验证  
  24. */ 
  25. private transient MailAuthenticator authenticator;  
  26. /**  
  27. * 邮箱session  
  28. */ 
  29. private transient Session session;  
  30. /**  
  31. * 初始化邮件发送器  
  32.  
  33. * @param smtpHostName  
  34. * SMTP邮件服务器地址  
  35. * @param username  
  36. * 发送邮件的用户名(地址)  
  37. * @param password  
  38. * 发送邮件的密码  
  39. */ 
  40. public SimpleMailSender(final String smtpHostName, final String username,  
  41. final String password) {  
  42. init(username, password, smtpHostName);  
  43. }  
  44. /**  
  45. * 初始化邮件发送器  
  46.  
  47. * @param username  
  48. * 发送邮件的用户名(地址),并以此解析SMTP服务器地址  
  49. * @param password  
  50. * 发送邮件的密码  
  51. */ 
  52. public SimpleMailSender(final String username, final String password) {  
  53. //通过邮箱地址解析出smtp服务器,对大多数邮箱都管用  
  54. final String smtpHostName = "smtp." + username.split("@")[1];  
  55. init(username, password, smtpHostName);  
  56. }  
  57. /**  
  58. * 初始化  
  59.  
  60. * @param username  
  61. * 发送邮件的用户名(地址)  
  62. * @param password  
  63. * 密码  
  64. * @param smtpHostName  
  65. * SMTP主机地址  
  66. */ 
  67. private void init(String username, String password, String smtpHostName) {  
  68. // 初始化props  
  69. props.put("mail.smtp.auth""true");  
  70. props.put("mail.smtp.host", smtpHostName);  
  71. // 验证  
  72. authenticator = new MailAuthenticator(username, password);  
  73. // 创建session  
  74. session = Session.getInstance(props, authenticator);  
  75. }  
  76. /**  
  77. * 发送邮件  
  78.  
  79. * @param recipient  
  80. * 收件人邮箱地址  
  81. * @param subject  
  82. * 邮件主题  
  83. * @param content  
  84. * 邮件内容  
  85. * @throws AddressException  
  86. * @throws MessagingException  
  87. */ 
  88. public void send(String recipient, String subject, Object content)  
  89. throws AddressException, MessagingException {  
  90. // 创建mime类型邮件  
  91. final MimeMessage message = new MimeMessage(session);  
  92. // 设置发信人  
  93. message.setFrom(new InternetAddress(authenticator.getUsername()));  
  94. // 设置收件人  
  95. message.setRecipient(RecipientType.TO, new InternetAddress(recipient));  
  96. // 设置主题  
  97. message.setSubject(subject);  
  98. // 设置邮件内容  
  99. message.setContent(content.toString(), "text/html;charset=utf-8");  
  100. // 发送  
  101. Transport.send(message);  
  102. }  
  103. /**  
  104. * 群发邮件  
  105.  
  106. * @param recipients  
  107. * 收件人们  
  108. * @param subject  
  109. * 主题  
  110. * @param content  
  111. * 内容  
  112. * @throws AddressException  
  113. * @throws MessagingException  
  114. */ 
  115. public void send(List<String> recipients, String subject, Object content)  
  116. throws AddressException, MessagingException {  
  117. // 创建mime类型邮件  
  118. final MimeMessage message = new MimeMessage(session);  
  119. // 设置发信人  
  120. message.setFrom(new InternetAddress(authenticator.getUsername()));  
  121. // 设置收件人们  
  122. final int num = recipients.size();  
  123.  InternetAddress[] addresses = new InternetAddress[num];  
  124. for (int i = 0; i <num; i++) {  
  125. addresses[i] = new InternetAddress(recipients.get(i));  
  126. }  
  127. message.setRecipients(RecipientType.TO, addresses);  
  128. // 设置主题  
  129. message.setSubject(subject);  
  130. // 设置邮件内容  
  131. message.setContent(content.toString(), "text/html;charset=utf-8");  
  132. // 发送  
  133. Transport.send(message);  
  134. }  
  135. /**  
  136. * 发送邮件  
  137.  
  138. * @param recipient  
  139. * 收件人邮箱地址  
  140. * @param mail  
  141. * 邮件对象  
  142. * @throws AddressException  
  143. * @throws MessagingException  
  144.  */ 
  145. public void send(String recipient, SimpleMail mail)  
  146. throws AddressException, MessagingException {  
  147. send(recipient, mail.getSubject(), mail.getContent());  
  148. }  
  149. /**  
  150. * 群发邮件  
  151.  
  152. * @param recipients  
  153. * 收件人们  
  154. * @param mail  
  155.  * 邮件对  
  156.  * @throws AddressException  
  157. * @throws MessagingException  
  158. */ 
  159. public void send(List<String> recipients, SimpleMail mail)  
  160. throws AddressException, MessagingException {  
  161. send(recipients, mail.getSubject(), mail.getContent());  
  162. }  
  163. }

3、调用上面的邮箱发送器,可以构建一个工厂类,工厂类可以封装创建的过程,所以通过读配置文件获取邮箱用户名,密码都会变得十分方便。下面的代码是我在写观察者模式的时候写的,只是简单演示了工厂类。

  1.  package com.mzule.dp.observer.factory;  
  2. import com.mzule.dp.observer.constant.MailSenderType;  
  3. import com.mzule.simplemail.SimpleMailSender;  
  4. /**  
  5. * 发件箱工厂  
  6.  
  7. * @author MZULE  
  8.  
  9. */ 
  10. public class MailSenderFactory {  
  11. /**  
  12. * 服务邮箱  
  13. */ 
  14. private static SimpleMailSender serviceSms = null;  
  15. /**  
  16. * 获取邮箱  
  17.  
  18. * @param type 邮箱类型  
  19. * @return 符合类型的邮箱  
  20. */ 
  21. public static SimpleMailSender getSender(MailSenderType type) {  
  22. if (type == MailSenderType.SERVICE) {  
  23. if (serviceSms == null) {  
  24. serviceSms = new SimpleMailSender("invisible@126.com",  
  25. "hidden");  
  26. }  
  27. return serviceSms;  
  28. }  
  29. return null;  
  30. }  
  31. }

  4、发送邮件,还是观察者模式DEMO里面的代码,呼呼。

  1. package com.mzule.dp.observer.observer;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import java.util.Observable;  
  5. import java.util.Observer;  
  6. import javax.mail.MessagingException;  
  7. import javax.mail.internet.AddressException;  
  8. import com.mzule.dp.observer.constant.MailSenderType;  
  9. import com.mzule.dp.observer.factory.MailSenderFactory;  
  10. import com.mzule.dp.observer.po.Product;  
  11. import com.mzule.simplemail.SimpleMailSender;  
  12. public class ProductPriceObserver implements Observer {  
  13. @Override 
  14. public void update(Observable obj, Object arg) {  
  15. Product product = null;  
  16. if (obj instanceof Product) {  
  17. product = (Product) obj;  
  18. }  
  19. if (arg instanceof Float) {  
  20. Float price = (Float) arg;  
  21.  Float decrease = product.getPrice() - price;  
  22. if (decrease >0) {  
  23. // 发送邮件  
  24. SimpleMailSender sms = MailSenderFactory  
  25. .getSender(MailSenderType.SERVICE);  
  26. List<String> recipients = new ArrayList<String>();  
  27. recipients.add("invisible@qq.com");  
  28. recipients.add("invisible@gmail.com");  
  29. try {  
  30. for (String recipient : recipients) {  
  31. sms.send(recipient, "价格变动""您关注的物品" 
  32. + product.getName() + "降价了,由" 
  33. + product.getPrice() + "元降到" + price + "元,降幅达" 
  34. + decrease + "元人民币。赶快购物吧。");  
  35. }  
  36. catch (AddressException e) {  
  37. e.printStackTrace();  
  38. catch (MessagingException e) {  
  39. e.printStackTrace();  
  40. }  
  41. }  
  42. }  
  43. }  
  44. }

posted on 2011-11-03 09:23 顺其自然EVO 阅读(2787) 评论(2)  编辑  收藏

评论

# re: 利用Java实现电子邮件的批量发送 2013-08-04 11:43 骂咯

我看看 试试 不知道 怎么呀 来啊
www.minglu5.com名录屋
bbs.ibbcp.com影视剧发行  回复  更多评论   

# re: 利用Java实现电子邮件的批量发送 2014-01-06 21:27 miciing

不错!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜