当前位置:   article > 正文

java发送带附件的邮件_java邮箱发送附件的三种方式是什么

java邮箱发送附件的三种方式是什么

java发邮件有三种方式:

一、使用javax.mail传统方式。需引入mail-1.4.jar包。这是用java自带的类库实现,但是实现起来比较麻烦。

二、使用Apache Commons Mail方式。需要引入commons-email-1.2.jar包。用MultiPartEmail对象,设置完邮箱信息和EmailAttachment附件对象后,使用send方法发送。

  1. //附件,可以定义多个附件对象
  2. EmailAttachment attachment = new EmailAttachment();
  3. attachment.setPath("C:\\123.txt");
  4. attachment.setDisposition(EmailAttachment.ATTACHMENT);
  5. attachment.setDescription("这是附件描述");
  6. MultiPartEmail email = new MultiPartEmail();
  7. email.setHostName("smtp.163.com"); //设置发送主机的服务器地址
  8. email.addTo("xxx"); //设置收件人邮箱
  9. email.setFrom("xxx"); //发件人邮箱
  10. email.setAuthentication("用户名", "密码");//设置发件人在邮件服务器上注册的用户名和密码
  11. email.setCharset("UTF-8"); //设置主题的字符集
  12. email.setSubject("你好, 这是邮件主题"); //设置邮件的主题
  13. email.setMsg("这是邮件正文部分"); //邮件正文内容
  14. email.attach(attachment); //添加附件
  15. email.send();

 

三、使用spring框架方式。需引入spring-context-support-4.3.9.jar包。使用MimeMessageHelper对象发送。

  1. JavaMailSender javaMailSender = new JavaMailSenderImpl();
  2. MimeMessage mailMessage = javaMailSender.createMimeMessage();
  3. MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");
  4. messageHelper.setTo(tos);//邮件接收人
  5. messageHelper.setFrom(text);//邮件内容
  6. messageHelper.setSubject(subject);//主题
  7. messageHelper.setText(text, true);// true 表示启动HTML格式的邮件
  8. FileSystemResource file = new FileSystemResource(new File("c://123.txt"));
  9. messageHelper.addAttachment(AttachName, file);// 发送邮件
  10. javaMailSender.send(mailMessage);

 

最后附上发邮件工具类:

配置类:

  1. package cn.com.maxntech.email;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. @Component("mailConfig")
  5. @ConfigurationProperties(prefix = "application.email")
  6. public class MailConfig {
  7. private String host;
  8. private String username;
  9. private String password;
  10. private String auth;
  11. //省略get set
  12. }

工具类:

  1. package cn.com.maxntech.hu.utils;
  2. import java.io.File;
  3. import javax.mail.MessagingException;
  4. import javax.mail.internet.MimeMessage;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.core.io.FileSystemResource;
  7. import org.springframework.mail.javamail.JavaMailSender;
  8. import org.springframework.mail.javamail.MimeMessageHelper;
  9. import org.springframework.stereotype.Component;
  10. import cn.com.maxtech.email.MailConfig;
  11. /**
  12. * 邮件发送工具类
  13. * */
  14. @Component
  15. public class SendMailUtils {
  16. @Autowired
  17. private MailConfig mailConfig;
  18. @Autowired
  19. private JavaMailSender javaMailSender;
  20. /**
  21. * 发送带html格式的邮件
  22. * @param tos
  23. * @param subject
  24. * @param text
  25. * @throws Exception
  26. */
  27. public void sendTextWithHtml(String[] tos, String subject, String text)
  28. throws Exception {
  29. MimeMessage mailMessage = javaMailSender.createMimeMessage();
  30. initMimeMessageHelper(mailMessage, tos, mailConfig.getUsername(), subject, text);
  31. // 发送邮件
  32. javaMailSender.send(mailMessage);
  33. }
  34. /**
  35. * 发送带图片的邮件
  36. * @param tos
  37. * @param subject
  38. * @param text
  39. * @param imgId
  40. * @param imgPath
  41. * @throws MessagingException
  42. */
  43. public void sendTextWithImg(String[] tos, String subject, String text,
  44. String imgId, String imgPath) throws MessagingException {
  45. MimeMessage mailMessage = javaMailSender.createMimeMessage();
  46. MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage,tos,mailConfig.getUsername(),subject, text,true, true, "UTF-8");
  47. // 发送图片
  48. FileSystemResource img = new FileSystemResource(new File(imgPath));
  49. messageHelper.addInline(imgId, img);
  50. // 发送邮件
  51. javaMailSender.send(mailMessage);
  52. }
  53. /**
  54. * 发送带附件的邮件
  55. * @param tos
  56. * @param subject
  57. * @param text
  58. * @param AttachName
  59. * @param filePath
  60. * @throws MessagingException
  61. */
  62. public void sendWithAttament(String[] tos, String subject, String text,
  63. String AttachName, String filePath) throws MessagingException {
  64. MimeMessage mailMessage = javaMailSender.createMimeMessage();
  65. MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, mailConfig.getUsername(), subject, text,
  66. true, true, "UTF-8");
  67. FileSystemResource file = new FileSystemResource(new File(filePath));
  68. // 发送邮件
  69. messageHelper.addAttachment(AttachName, file);
  70. javaMailSender.send(mailMessage);
  71. }
  72. /**
  73. * 发送带图片和附件的邮件
  74. * @param tos
  75. * @param from
  76. * @param subject
  77. * @param text
  78. * @param imgId
  79. * @param imgPath
  80. * @param AttachName
  81. * @param filePath
  82. * @throws MessagingException
  83. */
  84. public void sendWithAll(String[] tos, String from, String subject, String text,
  85. String imgId, String imgPath, String AttachName, String filePath) throws MessagingException {
  86. MimeMessage mailMessage = javaMailSender.createMimeMessage();
  87. MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, mailConfig.getUsername(), subject, text,
  88. true, true, "UTF-8");
  89. // 插入图片
  90. FileSystemResource img = new FileSystemResource(new File(imgPath));
  91. messageHelper.addInline(imgId, img);
  92. // 插入附件
  93. FileSystemResource file = new FileSystemResource(new File(filePath));
  94. messageHelper.addAttachment(AttachName, file);
  95. // 发送邮件
  96. javaMailSender.send(mailMessage);
  97. }
  98. /**
  99. * 初始化helper
  100. * @param mailMessage
  101. * @param tos
  102. * @param from
  103. * @param subject
  104. * @param text
  105. * @return
  106. * @throws MessagingException
  107. */
  108. private MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
  109. String subject, String text) throws MessagingException {
  110. return initMimeMessageHelper(mailMessage, tos, from, subject, text, true, false, "UTF-8");
  111. }
  112. /**
  113. * 初始化helper
  114. * @param mailMessage
  115. * @param tos
  116. * @param from
  117. * @param subject
  118. * @param text
  119. * @param isHTML
  120. * @param multipart
  121. * @param encoding
  122. * @return
  123. * @throws MessagingException
  124. */
  125. private static MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,
  126. String subject, String text, boolean isHTML, boolean multipart, String encoding) throws MessagingException {
  127. MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, multipart, encoding);
  128. messageHelper.setTo(tos);
  129. messageHelper.setFrom(from);
  130. messageHelper.setSubject(subject);
  131. // true 表示启动HTML格式的邮件
  132. messageHelper.setText(text, isHTML);
  133. return messageHelper;
  134. }
  135. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/591856
推荐阅读
相关标签
  

闽ICP备14008679号