支持发送多人、抄送,密送,多个附件
1、引入maven依赖
<dependency> ? ? <groupId>javax.mail</groupId> ? ? <artifactId>mail</artifactId> ? ? <version>1.4.7</version> </dependency>
2、编写发送邮件工具类
package cn.ccb.qcy.common.utils;
import java.io.File; import java.util.Date; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern;
import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility;
import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component @Slf4j public class MailPublish {
?? ?private static final String smtpHost = "smtp.qq.com";// 主机名 ?? ?private static final String fromMail = "1047082173@qq.com";// 发件箱 ?? ?private static final String password = "mtklxmxinfnrbccg";// 授权码
?? ?/** ?? ? * 发送邮件,支持多个附件 ?? ? *? ?? ? * @param toMails ?主收件人,多个收件人用,分隔(必传) ?? ? * @param ccMails ?抄送人,多个收件人用,分隔(非必传) ?? ? * @param bccMails 密送人,多个收件人用,分隔(非必传) ?? ? * @param title ? ?标题(必传) ?? ? * @param content ?内容(支持html文本)(必传) ?? ? * @param path ? ? 单个附件传文件路径,多个附件传文件夹路径 (非必传) ?? ? * @throws Exception ?? ? */ ?? ?public static boolean sendMail(String toMails, String ccMails, String bccMails, String title, String content, ?? ??? ??? ?String path) throws Exception { ?? ??? ?boolean result = false; ?? ??? ?try { ?? ??? ??? ?Properties props = new Properties(); ?? ??? ??? ?props.setProperty("mail.transport.protocol", "SMTP"); ?? ??? ??? ?props.setProperty("mail.host", smtpHost); ?? ??? ??? ?props.setProperty("mail.smtp.auth", "true"); ?? ??? ??? ?Authenticator auth = new Authenticator() { ?? ??? ??? ??? ?public PasswordAuthentication getPasswordAuthentication() { ?? ??? ??? ??? ??? ?return new PasswordAuthentication(fromMail, password); ?? ??? ??? ??? ?} ?? ??? ??? ?}; ?? ??? ??? ?Session session = Session.getInstance(props, auth); ?? ??? ??? ?Message message = new MimeMessage(session); ?? ??? ??? ?message.setFrom(new InternetAddress(fromMail)); ?? ??? ??? ?message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMails)); ?? ??? ??? ?// Cc: 抄送(可选) ?? ??? ??? ?if (StringUtils.isNotBlank(ccMails)) { ?? ??? ??? ??? ?message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccMails)); ?? ??? ??? ?} ?? ??? ??? ?// Bcc: 密送(可选) ?? ??? ??? ?if (StringUtils.isNotBlank(bccMails)) { ?? ??? ??? ??? ?message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bccMails)); ?? ??? ??? ?}
?? ??? ??? ?message.setSubject(title);
?? ??? ??? ?// 邮件正文 ?? ??? ??? ?MimeBodyPart body = new MimeBodyPart(); ?? ??? ??? ?body.setContent(content, "text/html;charset=utf-8"); ?? ??? ??? ?// 将正文和附件放入multipart ?? ??? ??? ?Multipart multipart = new MimeMultipart(); ?? ??? ??? ?multipart.addBodyPart(body); ?? ??? ??? ?if (StringUtils.isNotBlank(path)) { ?? ??? ??? ??? ?File file = new File(path); ?? ??? ??? ??? ?if (file.isFile()) {// 单个附件 ?? ??? ??? ??? ??? ?// 准备附件 ?? ??? ??? ??? ??? ?DataSource source = new FileDataSource(new File(path)); ?? ??? ??? ??? ??? ?DataHandler handler = new DataHandler(source); ?? ??? ??? ??? ??? ?// 添加附件 ?? ??? ??? ??? ??? ?MimeBodyPart attachment = new MimeBodyPart(); ?? ??? ??? ??? ??? ?attachment.setDataHandler(handler); ?? ??? ??? ??? ??? ?attachment.setFileName(MimeUtility.encodeText(handler.getName())); ?? ??? ??? ??? ??? ?multipart.addBodyPart(attachment); ?? ??? ??? ??? ?} else if (file.isDirectory()) { ?? ??? ??? ??? ??? ?for (File dFile : file.listFiles()) { ?? ??? ??? ??? ??? ??? ?if (dFile.isFile()) { ?? ??? ??? ??? ??? ??? ??? ?// 准备附件 ?? ??? ??? ??? ??? ??? ??? ?DataSource source = new FileDataSource(dFile); ?? ??? ??? ??? ??? ??? ??? ?DataHandler handler = new DataHandler(source); ?? ??? ??? ??? ??? ??? ??? ?// 添加附件 ?? ??? ??? ??? ??? ??? ??? ?MimeBodyPart attachment = new MimeBodyPart(); ?? ??? ??? ??? ??? ??? ??? ?attachment.setDataHandler(handler); ?? ??? ??? ??? ??? ??? ??? ?attachment.setFileName(MimeUtility.encodeText(handler.getName())); ?? ??? ??? ??? ??? ??? ??? ?multipart.addBodyPart(attachment); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?log.info("路径:", file.getAbsolutePath()); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?message.setContent(multipart); ?? ??? ??? ?// 发送邮件 ?? ??? ??? ?Transport.send(message); ?? ??? ??? ?result = true; ?? ??? ??? ?System.out.println("发送邮件成功"); ?? ??? ?} catch (Exception e) { ?? ??? ??? ?log.error("发送邮件失败", e); ?? ??? ??? ?System.out.println("发送邮件异常:" + e); ?? ??? ??? ?result = false; ?? ??? ?} ?? ??? ?return result; ?? ?}
?? ?/** ?? ? * 校验邮箱格式 ?? ? *? ?? ? * @param email ?? ? * @return ?? ? */ ?? ?public static boolean checkEmail(String email) { ?? ??? ?boolean flag = false; ?? ??? ?try { ?? ??? ??? ?String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; ?? ??? ??? ?Pattern regex = Pattern.compile(check); ?? ??? ??? ?Matcher matcher = regex.matcher(email); ?? ??? ??? ?flag = matcher.matches(); ?? ??? ?} catch (Exception e) { ?? ??? ??? ?flag = false; ?? ??? ?} ?? ??? ?return flag; ?? ?}
?? ?public static void main(String[] args) throws Exception { ?? ??? ?String content = "<p>\r\n" ?? ??? ??? ??? ?+ " ? ?<span class=\"content\" style=\"line-height: 26px; display: block; padding: 40px 20px;\">QQ邮箱APP,让高效触手可及。在这里,你可以登录多个邮箱账号、便捷存储微信邮件、多窗口编辑邮件......还有更多功能,等你探索!</span>\r\n" ?? ??? ??? ??? ?+ "</p>\r\n" + "<h1 style=\"font-size: 30px; margin: 0px;\">\r\n" + " ? ?01\r\n" + "</h1>\r\n" ?? ??? ??? ??? ?+ "<h2 style=\"font-size: 20px; margin: 0px 0px 14px;\">\r\n" + " ? ?多帐号登录\r\n" + "</h2>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; margin-bottom: 0px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?可以同时登录多个邮箱帐号\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?一站式灵活管理各类邮件信息\r\n" + "</p>\r\n" + "<p>\r\n" ?? ??? ??? ??? ?+ " ? ?<img src=\"https://rescdn.qqmail.com/qqmail/images/manyAccountLoginImg.png\" alt=\"\"/>\r\n" ?? ??? ??? ??? ?+ "</p>\r\n" + "<h1 style=\"font-size: 30px; margin: 0px;\">\r\n" + " ? ?02\r\n" + "</h1>\r\n" ?? ??? ??? ??? ?+ "<h2 style=\"font-size: 20px; margin: 0px 0px 14px;\">\r\n" + " ? ?文件中转站\r\n" + "</h2>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; margin-bottom: 0px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?把各类文件存在云端\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?还能便捷导入微信文件,过期烦恼不再有\r\n" + "</p>\r\n" + "<p>\r\n" ?? ??? ??? ??? ?+ " ? ?<img src=\"https://rescdn.qqmail.com/qqmail/images/fileTransferStation.png\" alt=\"\"/>\r\n" ?? ??? ??? ??? ?+ "</p>\r\n" + "<h1 style=\"font-size: 30px; margin: 0px;\">\r\n" + " ? ?03\r\n" + "</h1>\r\n" ?? ??? ??? ??? ?+ "<h2 style=\"font-size: 20px; margin: 0px 0px 14px;\">\r\n" + " ? ?多窗口编辑\r\n" + "</h2>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; margin-bottom: 0px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?写邮件、写记事、写日程都可收起为浮窗\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?编辑内容的同时不错过重要邮件\r\n" + "</p>\r\n" + "<p>\r\n" ?? ??? ??? ??? ?+ " ? ?<img src=\"https://rescdn.qqmail.com/qqmail/images/edit.png\" alt=\"\"/>\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<h1 style=\"font-size: 30px; margin: 0px;\">\r\n" + " ? ?04\r\n" + "</h1>\r\n" ?? ??? ??? ??? ?+ "<h2 style=\"font-size: 20px; margin: 0px 0px 14px;\">\r\n" + " ? ?富文本信息一键添加\r\n" + "</h2>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; margin-bottom: 0px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?日程邀请、地理位置、记事本、在线文档\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?这些重要内容可以一键加入邮件正文\r\n" + "</p>\r\n" + "<p>\r\n" ?? ??? ??? ??? ?+ " ? ?<img src=\"https://rescdn.qqmail.com/qqmail/images/language.png\" alt=\"\"/>\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<h1 style=\"font-size: 30px; margin: 0px;\">\r\n" + " ? ?05\r\n" + "</h1>\r\n" ?? ??? ??? ??? ?+ "<h2 style=\"font-size: 20px; margin: 0px 0px 14px;\">\r\n" + " ? ?灵活管理日程\r\n" + "</h2>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; margin-bottom: 0px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?全新日历功能、日程列表更清晰,重要事件不错过\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<p style=\"line-height: 22.1px; font-size: 13px; color: rgb(142, 142, 147);\">\r\n" ?? ??? ??? ??? ?+ " ? ?还可以邀请伙伴共同参与,提升合作效率\r\n" + "</p>\r\n" + "<p>\r\n" ?? ??? ??? ??? ?+ " ? ?<img src=\"https://rescdn.qqmail.com/qqmail/images/routin.png\" alt=\"\"/>\r\n" + "</p>\r\n" ?? ??? ??? ??? ?+ "<p>\r\n" ?? ??? ??? ??? ?+ " ? ?<img class=\"bottomErweima\" src=\"https://rescdn.qqmail.com/qqmail/images/emailDownloadErweima.png\" alt=\"\"/>\r\n" ?? ??? ??? ??? ?+ "</p>\r\n" + "<p>\r\n" + " ? ?<br/>\r\n" + "</p>"; ?? ??? ?MailPublish.sendMail("1047082173@qq.com,zhangliangqi@git.com.cn",null, null, new Date()+"【测试邮件】", content, ?? ??? ??? ??? ?"C:\\upload\\20220323"); ?? ??? ?// System.out.println(checkEmail("104708;2173@qq.com")); ?? ?} }
功能比较全,细节有待完善。
|