个人随笔
目录
Java邮件发送示例
2019-12-02 23:50:10

我们工作中,经常需要借助程序来发送邮件,比如有人在我的博客评论了,然后这个博主有设置邮箱并且设置接收功能的话我就可以发个邮件给用户,那么怎么实现呢!

随笔博客邮箱使用场景设想

  1. 博主登录随笔博客后台,绑定邮箱账号
  2. 博主设置接收评论收藏提醒
  3. 用户点击评论后会登记一条消息到消息队列
  4. 邮件发送小程序接收到消息后就发送一条消息给博主的邮箱账号

准备工作

当然,发送邮件必须靠邮件服务器,代码只是模拟你登录邮箱然后发送邮件而已,所以这里需要有一个邮件服务器,可以选择网易,也可以选择QQ,这里选择的是腾讯QQ。

登录邮箱,生成授权码

这步很简单,因为我是用QQ邮箱,登录邮箱如下配置:

这里主要的是获取授权码。

代码示例

这里只是举一个发送邮件的例子。

1、pom.xml

  1. <!-- email -->
  2. <dependency>
  3. <groupId>org.apache.commons</groupId>
  4. <artifactId>commons-email</artifactId>
  5. <version>1.4</version>
  6. </dependency>

2、MailInfo

  1. public class MailInfo {
  2. // 收件人
  3. private List<String> toAddress = null;
  4. // 抄送人地址
  5. private List<String> ccAddress = null;
  6. // 密送人
  7. private List<String> bccAddress = null;
  8. // 附件信息
  9. private List<EmailAttachment> attachments = null;
  10. // 邮件主题
  11. private String subject;
  12. // 邮件的文本内容
  13. private String content;
  14. public List<String> getToAddress() {
  15. return toAddress;
  16. }
  17. public void setToAddress(List<String> toAddress) {
  18. this.toAddress = toAddress;
  19. }
  20. public List<String> getCcAddress() {
  21. return ccAddress;
  22. }
  23. public void setCcAddress(List<String> ccAddress) {
  24. this.ccAddress = ccAddress;
  25. }
  26. public List<String> getBccAddress() {
  27. return bccAddress;
  28. }
  29. public void setBccAddress(List<String> bccAddress) {
  30. this.bccAddress = bccAddress;
  31. }
  32. public List<EmailAttachment> getAttachments() {
  33. return attachments;
  34. }
  35. public void setAttachments(List<EmailAttachment> attachments) {
  36. this.attachments = attachments;
  37. }
  38. public String getSubject() {
  39. return subject;
  40. }
  41. public void setSubject(String subject) {
  42. this.subject = subject;
  43. }
  44. public String getContent() {
  45. return content;
  46. }
  47. public void setContent(String content) {
  48. this.content = content;
  49. }
  50. @Override
  51. public String toString() {
  52. return "MailInfo [toAddress=" + toAddress + ", ccAddress=" + ccAddress + ", bccAddress=" + bccAddress
  53. + ", attachments=" + attachments + ", subject=" + subject + ", content=" + content + "]";
  54. }
  55. }

3、MailUtil

  1. public class MailUtil {
  2. //邮箱
  3. private static String mailServerHost = "smtp.qq.com";
  4. private static String mailSenderAddress = "suibibk@qq.com";
  5. private static String mailSenderNick = "随笔博客";
  6. private static String mailSenderUsername = "suibibk@qq.com";
  7. private static String mailSenderPassword = "授权码";
  8. /**
  9. * 发送 邮件方法 (Html格式,支持附件)
  10. *
  11. * @return void
  12. */
  13. public static void sendEmail(MailInfo mailInfo) {
  14. try {
  15. HtmlEmail email = new HtmlEmail();
  16. // 配置信息
  17. email.setHostName(mailServerHost);
  18. email.setFrom(mailSenderAddress,mailSenderNick);
  19. email.setAuthentication(mailSenderUsername,mailSenderPassword);
  20. email.setCharset("UTF-8");
  21. email.setSubject(mailInfo.getSubject());
  22. email.setHtmlMsg(mailInfo.getContent());
  23. // 添加附件
  24. List<EmailAttachment> attachments = mailInfo.getAttachments();
  25. if (null != attachments && attachments.size() > 0) {
  26. for (int i = 0; i < attachments.size(); i++) {
  27. email.attach(attachments.get(i));
  28. }
  29. }
  30. // 收件人
  31. List<String> toAddress = mailInfo.getToAddress();
  32. if (null != toAddress && toAddress.size() > 0) {
  33. for (int i = 0; i < toAddress.size(); i++) {
  34. email.addTo(toAddress.get(i));
  35. }
  36. }
  37. // 抄送人
  38. List<String> ccAddress = mailInfo.getCcAddress();
  39. if (null != ccAddress && ccAddress.size() > 0) {
  40. for (int i = 0; i < ccAddress.size(); i++) {
  41. email.addCc(ccAddress.get(i));
  42. }
  43. }
  44. //邮件模板 密送人
  45. List<String> bccAddress = mailInfo.getBccAddress();
  46. if (null != bccAddress && bccAddress.size() > 0) {
  47. for (int i = 0; i < bccAddress.size(); i++) {
  48. email.addBcc(ccAddress.get(i));
  49. }
  50. }
  51. email.send();
  52. System.out.println("邮件发送成功!");
  53. } catch (EmailException e) {
  54. e.printStackTrace();
  55. }
  56. }

4、测试

  1. public class App {
  2. /**
  3. * @return void
  4. * @throws MalformedURLException
  5. */
  6. public static void main(String[] args) throws MalformedURLException {
  7. MailInfo mailInfo = new MailInfo();
  8. List<String> toList = new ArrayList<String>();
  9. toList.add("suibibk@qq.com");
  10. List<String> ccList = new ArrayList<String>();
  11. ccList.add("suibibk@qq.com");
  12. List<String> bccList = new ArrayList<String>();
  13. bccList.add("suibibk@qq.com");
  14. //添加附件
  15. EmailAttachment att = new EmailAttachment();
  16. att.setPath("C:\\Users\\forever\\Desktop\\活动管理平台\\images\\homeQrcode-b41d95e01e.png");
  17. att.setName("图片.png");
  18. List<EmailAttachment> atts = new ArrayList<EmailAttachment>();
  19. atts.add(att);
  20. mailInfo.setAttachments(atts);
  21. mailInfo.setToAddress(toList);//收件人
  22. mailInfo.setCcAddress(ccList);//抄送人
  23. mailInfo.setBccAddress(bccList);//密送人
  24. mailInfo.setSubject("测试主题");
  25. mailInfo.setContent("内容:<h1>test,测试</h1>");
  26. MailUtil.sendEmail(mailInfo);
  27. }
  28. }

运行测试程序,会提示发送成功,再看一下邮箱也接收到啦,要用网易请自行登录到网易邮箱去设置。

 143

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2