Advertisement
Guest User

Untitled

a guest
Nov 9th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. package com.bj58.ecat.emc.diywebsite.tongji.wltsync.util;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.activation.DataHandler;
  6. import javax.mail.Address;
  7. import javax.mail.Message;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeBodyPart;
  12. import javax.mail.internet.MimeMessage;
  13. import javax.mail.internet.MimeMultipart;
  14. import javax.mail.internet.MimeUtility;
  15. import javax.mail.util.ByteArrayDataSource;
  16.  
  17. /**
  18. * 邮件处理类,基本用法如下:<br>
  19. * Mails.create().subject("标题").body("内容 ").send();<br>
  20. * 多次调用 {@link #body(bodyContent)} 或者 {@link #attachment(name, type, bytes)}
  21. * 可以多次添加邮件内容或者附件
  22. * @author lzxz1234<lzxz1234@gmail.com>
  23. */
  24. public class Mails {
  25.  
  26. private static final String host = Configs.getProperty("email.host");
  27. private static final String smtphost = Configs.getProperty("email.host.smtp");
  28. private static final String username = Configs.getProperty("email.user");
  29. private static final String password = Configs.getProperty("email.pass");
  30. private static final String fromEmail = username + "@" + host;
  31. public static final String[] receiveMails = Configs.getProperty(String[].class, "email.receivers");
  32.  
  33. private Session session;
  34. private MimeMessage message;
  35.  
  36. /**
  37. * 创建邮件对象的工厂类
  38. * @return
  39. * @throws Exception
  40. */
  41. public static Mails create() throws Exception {
  42.  
  43. Properties prop = new Properties();
  44. prop.setProperty("mail.host", smtphost);
  45. prop.setProperty("mail.transport.protocol", "smtp");
  46. prop.setProperty("mail.smtp.auth", "true");
  47. prop.setProperty("mail.smtp.starttls.enable","true");
  48. Mails result = new Mails();
  49. result.session = Session.getInstance(prop);
  50. return result;
  51. }
  52.  
  53. /**
  54. * 执行发送操作
  55. * @return
  56. * @throws Exception
  57. */
  58. public Mails send() throws Exception {
  59.  
  60. message.saveChanges();
  61. Transport ts = session.getTransport();
  62. ts.connect(smtphost, username, password);
  63. ts.sendMessage(message, message.getAllRecipients());
  64. ts.close();
  65. return this;
  66. }
  67.  
  68. /**
  69. * 设置邮件标题
  70. * @param subject
  71. * @return
  72. * @throws Exception
  73. */
  74. public Mails subject(String subject) throws Exception {
  75.  
  76. message = new MimeMessage(session);
  77. message.setFrom(new InternetAddress(fromEmail));
  78. message.setRecipients(Message.RecipientType.TO, getRecipient());
  79. message.setSubject(subject);
  80. message.setContent(new MimeMultipart("mixed"));
  81. return this;
  82. }
  83.  
  84. /**
  85. * 添加邮件体,支持 HTML 标签
  86. * @param bodyContent
  87. * @return
  88. * @throws Exception
  89. */
  90. public Mails body(String bodyContent) throws Exception {
  91.  
  92. MimeBodyPart text = new MimeBodyPart();
  93. text.setContent(bodyContent, "text/html;charset=UTF-8");
  94. ((MimeMultipart)message.getContent()).addBodyPart(text);
  95. return this;
  96. }
  97.  
  98. /**
  99. * 添加附件
  100. * @param name 附件名称
  101. * @param type 附件 MIME 值
  102. * @param bytes 附件体
  103. * @return
  104. * @throws Exception
  105. */
  106. public Mails attachment(String name, String type, byte[] bytes) throws Exception {
  107.  
  108. MimeBodyPart attach = new MimeBodyPart();
  109. DataHandler dh = new DataHandler(new ByteArrayDataSource(bytes, type));
  110. attach.setDataHandler(dh);
  111. attach.setFileName(MimeUtility.encodeWord(name));
  112. ((MimeMultipart)message.getContent()).addBodyPart(attach);
  113. return this;
  114. }
  115.  
  116. private static Address[] getRecipient() throws Exception {
  117.  
  118. InternetAddress[] result = new InternetAddress[receiveMails.length];
  119. for(int i = 0; i < receiveMails.length; i ++)
  120. result[i] = new InternetAddress(receiveMails[i]);
  121. return result;
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement