779aba8f7bb5bfd77ac1a58686b6d330fac53fdd.svn-base 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.yihu.utils;
  2. /**
  3. *
  4. */
  5. import java.io.File;
  6. import java.util.Properties;
  7. import javax.mail.Authenticator;
  8. import javax.mail.BodyPart;
  9. import javax.mail.Message;
  10. import javax.mail.PasswordAuthentication;
  11. import javax.mail.Session;
  12. import javax.mail.Transport;
  13. import javax.mail.internet.InternetAddress;
  14. import javax.mail.internet.MimeBodyPart;
  15. import javax.mail.internet.MimeMessage;
  16. import javax.mail.internet.MimeMultipart;
  17. import javax.mail.internet.MimeUtility;
  18. /**
  19. * @author 林小辉
  20. * @company
  21. * 2017-05-26
  22. */
  23. public class MailUtil {
  24. public static class MyAuthenticator extends Authenticator{
  25. String name;
  26. String password;
  27. public MyAuthenticator(String name,String password){
  28. this.name = name;
  29. this.password = password;
  30. this.getPasswordAuthentication();
  31. }
  32. @Override
  33. protected PasswordAuthentication getPasswordAuthentication() {
  34. return new PasswordAuthentication(name,password);
  35. }
  36. }
  37. /**
  38. *
  39. * @param subject 主题
  40. * @param context 邮件内容
  41. * @param emails 收件人(多人)
  42. * @param attachment 附件 (null标识不发送邮件)
  43. * @throws Exception
  44. */
  45. public static void send(String subject,String context,String[] emails) throws Exception{
  46. //发送邮箱的用户名和密码
  47. String name = "mailmaster@jkzl.com";
  48. String password = "qazwsx321$1";
  49. Properties props = System.getProperties();
  50. //设置smtp主机
  51. props.put("mail.smtp.host", "smtp.263.net");
  52. //设置smtp验证属性
  53. props.put("mail.smtp.auth","true");
  54. MyAuthenticator auth = new MyAuthenticator(name,password);
  55. //获得邮件会话对象
  56. Session session = Session.getDefaultInstance(props, auth);
  57. //创建MIME邮件对象
  58. MimeMessage message = new MimeMessage(session);
  59. subject = MimeUtility.encodeWord(subject, "UTF-8", "Q");
  60. message.setSubject(subject);
  61. //设置邮件发送人
  62. message.setFrom(new InternetAddress("mailmaster@jkzl.com"));
  63. //设置邮件收件人
  64. InternetAddress[] as = new InternetAddress[emails.length];
  65. for (int i = 0;i<emails.length;i++) {
  66. as[i]= new InternetAddress();
  67. as[i].setAddress(emails[i]);
  68. }
  69. message.setRecipients(Message.RecipientType.TO ,as);
  70. //设置邮件主题
  71. message.setSubject(subject);
  72. //设置上传的正文
  73. BodyPart bpContent = new MimeBodyPart();
  74. bpContent.setContent(context, "text/html;charset=UTF-8");
  75. MimeMultipart mp = new MimeMultipart();
  76. mp.addBodyPart(bpContent);
  77. message.setContent(mp);
  78. message.saveChanges();
  79. //创建SMTP邮件协议发送对象
  80. Transport transport = session.getTransport("smtp");
  81. //取得与邮件服务器的连接
  82. transport.connect((String)props.get("mail.smtp.host"), name, password);
  83. //通过邮件服务器发送邮件
  84. transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
  85. transport.close();
  86. }
  87. public static void main(String[] args) {
  88. String[] arry ={"906335852@qq.com"};
  89. try {
  90. MailUtil.send("BOOS故障单", "Test", arry);
  91. } catch (Exception e) {
  92. // TODO Auto-generated catch block
  93. e.printStackTrace();
  94. }
  95. }
  96. }