前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaMail-发送一封简单邮件(附带附件)

JavaMail-发送一封简单邮件(附带附件)

作者头像
雨临Lewis
发布2022-01-11 13:46:17
4270
发布2022-01-11 13:46:17
举报
文章被收录于专栏:雨临Lewis的博客

代码实现

最近使用到JavaMail,写了个简单的工具类,记录一下。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

import java.util.Date; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; import javax.mail.internet.MimeMultipart; public class JavaMailUtils { //连接SMTP服务器的主机名,这里以qq邮箱为例 private static final String SMTP_HOST = "smtp.qq.com"; //连接的端口号,587为ssl端口,默认为25端口 private static final String SMTP_PORT = "587"; //发件人 private static final String MAIL_FROM = "XXX@qq.com"; //连接邮件服务器的用户名(邮箱地址去除了@qq.com的部分) private static final String USER = "XXX"; /** * 授权码,就是你在邮件服务器上注册的密码,不是你的qq密码 * 在邮箱里开启smtp/imap服务时需要发送短信,成功后会得到一个授权码 */ private static final String PASSWORD = "授权码"; private JavaMailUtils() { } /** * send mail without attachments * * @param mailTo * @param mailCc * @param subject * @param content * @throws AddressException * @throws MessagingException */ public static void sendMail(final String mailTo, final String mailCc, final String subject, final String content) throws Exception { // get the session final Session session = getSession(); // create a mail final MimeMessage message = createMail(session, mailTo, mailCc, subject, content); // get the transport final Transport transport = session.getTransport(); transport.connect(USER, PASSWORD); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } public static void sendMail(final String mailTo, final String mailCc, final String subject, final String content, final String[] attachments) throws Exception { // get the session final Session session = getSession(); // create a mail final MimeMessage message = createMail(session, mailTo, mailCc, subject, content, attachments); // get the transport final Transport transport = session.getTransport(); transport.connect(USER, PASSWORD); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } /** * create a mail without attachments * * @param session * @param mailTo * @param mailCc * @param subject * @param content * @return * @throws Exception */ private static MimeMessage createMail(final Session session, final String mailTo, final String mailCc, final String subject, final String content) throws Exception { final MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(MAIL_FROM)); message.setRecipients(RecipientType.TO, InternetAddress.parse(mailTo)); message.setRecipients(RecipientType.CC, InternetAddress.parse(mailCc)); message.setSubject(subject); message.setContent(content, "text/html;charset=UTF-8"); message.setSentDate(new Date()); message.saveChanges(); return message; } /** * create a mail with attachments * * @param session * @param mailTo * @param mailCc * @param subject * @param content * @param attachments * @return */ private static MimeMessage createMail(final Session session, final String mailTo, final String mailCc, final String subject, final String content, final String[] attachments) throws Exception { final MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(MAIL_FROM)); message.setRecipients(RecipientType.TO, InternetAddress.parse(mailTo)); message.setRecipients(RecipientType.CC, InternetAddress.parse(mailCc)); message.setSubject(subject); // set the multipart of the mail final MimeMultipart multipart = new MimeMultipart(); // set content part of the mail final MimeBodyPart contentPart = new MimeBodyPart(); contentPart.setContent(content, "text/html;charset=UTF-8"); multipart.addBodyPart(contentPart); // set attachment part of the mail final MimeBodyPart attach1 = new MimeBodyPart(); final MimeBodyPart attach2 = new MimeBodyPart(); if (attachments != null && attachments.length != 0) { final int length = attachments.length; if (length == 1) { attach1.attachFile(attachments[0]); multipart.addBodyPart(attach1); } if (length == 2) { attach1.attachFile(attachments[0]); attach2.attachFile(attachments[1]); multipart.addBodyPart(attach1); multipart.addBodyPart(attach2); } } message.setContent(multipart); message.setSentDate(new Date()); message.saveChanges(); return message; } /** * get the session to transport mails */ private static Session getSession() { // set the properties for connecting the mail server final Properties props = new Properties(); props.setProperty("mail.debug", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", SMTP_HOST); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.port", SMTP_PORT); // ssl props.setProperty("mail.imap.ssl.enable", "true"); props.setProperty("mail.imap.ssl.socketFactory.class", "DummySSLSocketFactory"); props.setProperty("mail.imap.ssl.socketFactory.fallback", "false"); // create the session final Session session = Session.getDefaultInstance(props); return session; } }

以上的工具类有个进行了重载的方法,因为对于没有附件的邮件和有附件的邮件,在构造邮件对象时是不一样的,有附件的邮件会稍微复杂一点。接着是测试类。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

public class Test { //收件人,多人的话要用英文的","来隔开 private static final String MAIL_TO = "XXX@qq.com"; //抄送人,多人的话要用英文的","来隔开 private static final String MAIL_CC = "XXX@qq.com"; //邮件的主题(因为只是测试用,所以写成静态常量了) private static final String SUBJECT = "It's a test mail"; //邮件的内容(因为只是测试用,所以写成静态常量了) private static final String CONTENT = "Hello World!<br>Bye!"; private static final String FILE_1 = "附件1的路径"; private static final String FILE_2 = "附件2的路径"; public static void main(final String[] args) { final String[] attachments = {}; // final String[] attachments = {FILE_1, FILE_2}; try { // JavaMailUtils.sendMail(MAIL_TO, MAIL_CC, SUBJECT, CONTENT); JavaMailUtils.sendMail(MAIL_TO, MAIL_CC, SUBJECT, CONTENT, attachments); } catch (final Exception e) { e.printStackTrace(); } } }

最后是导入的jar包:

javax.mail-1.6.0.jar

补充

JavaMail提供了多个属性,这些属性的值都必须是字符串,否则设置无效,如下:

  • mail.smtp.sendpartial设置为"true",当一次发送多个地址时就不会因为某个地址无效而全部发送失败。
  • mail.smtp.auth设置为"false"时,则无需验证账号密码即可发送邮件。SMTP只是个简单的邮件发送协议,如果不设置校验,可能会造成垃圾邮件泛滥的问题。不过我发现公司项目在发送邮件时并没有设置验证,可能是图方便。

参考链接

警告

本文最后更新于 March 17, 2021,文中内容可能已过时,请谨慎使用。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-222,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码实现
  • 补充
  • 参考链接
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档