MailUtil.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.izouma.awesomeadmin.util;
  2. import com.izouma.awesomeadmin.dto.MailInfo;
  3. import org.apache.commons.mail.EmailAttachment;
  4. import org.apache.commons.mail.EmailException;
  5. import org.apache.commons.mail.HtmlEmail;
  6. import java.util.List;
  7. /**
  8. * 发送邮件Util
  9. */
  10. public class MailUtil {
  11. //邮箱
  12. private static String mailServerHost = "smtp.weiqiuwang.com";
  13. private static String mailSenderAddress = "scc@weiqiuwang.com";
  14. private static String mailSenderNick = "一米世界留言板";
  15. private static String mailSenderUsername = "scc@weiqiuwang.com";
  16. private static String mailSenderPassword = "2wsx@WSX";
  17. /**
  18. * 发送 邮件方法 (Html格式,支持附件)
  19. *
  20. * @return void
  21. */
  22. public static void sendEmail(MailInfo mailInfo) {
  23. try {
  24. HtmlEmail email = new HtmlEmail();
  25. // 配置信息
  26. email.setHostName(mailServerHost);
  27. email.setFrom(mailSenderAddress, mailSenderNick);
  28. email.setAuthentication(mailSenderUsername, mailSenderPassword);
  29. email.setCharset("UTF-8");
  30. email.setSubject(mailInfo.getSubject());
  31. email.setHtmlMsg(mailInfo.getContent());
  32. // 添加附件
  33. List<EmailAttachment> attachments = mailInfo.getAttachments();
  34. if (null != attachments && attachments.size() > 0) {
  35. for (int i = 0; i < attachments.size(); i++) {
  36. email.attach(attachments.get(i));
  37. }
  38. }
  39. // 收件人
  40. List<String> toAddress = mailInfo.getToAddress();
  41. if (null != toAddress && toAddress.size() > 0) {
  42. for (int i = 0; i < toAddress.size(); i++) {
  43. email.addTo(toAddress.get(i));
  44. }
  45. }
  46. // 抄送人
  47. List<String> ccAddress = mailInfo.getCcAddress();
  48. if (null != ccAddress && ccAddress.size() > 0) {
  49. for (int i = 0; i < ccAddress.size(); i++) {
  50. email.addCc(ccAddress.get(i));
  51. }
  52. }
  53. //邮件模板 密送人
  54. List<String> bccAddress = mailInfo.getBccAddress();
  55. if (null != bccAddress && bccAddress.size() > 0) {
  56. for (int i = 0; i < bccAddress.size(); i++) {
  57. email.addBcc(bccAddress.get(i));
  58. }
  59. }
  60. email.send();
  61. System.out.println("邮件发送成功!");
  62. } catch (EmailException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. }