1、导入jar包
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.5</version> </dependency>
2、配置邮件相关信息
public class MailInfoDTO { // 收件人 private List<String> toAddress = null; // 抄送人地址 private List<String> ccAddress = null; // 密送人 private List<String> bccAddress = null; // 附件信息 private List<EmailAttachment> attachments = null; // 邮件主题 private String subject; // 邮件的文本内容 private String content; public List<String> getToAddress() { return toAddress; } public void addToAddress(String toAddress) { this.toAddress.add(toAddress); } public void addToAddress(List<String> toAddress) { this.toAddress.addAll(toAddress); } public void addCcAddress(List<String> ccAddress) { if (null != ccAddress && ccAddress.size() > 0) this.ccAddress.addAll(ccAddress); } public List<EmailAttachment> getAttachments() { return attachments; } public void setAttachments(List<EmailAttachment> attachments) { this.attachments = attachments; } public List<String> getBccAddress() { return bccAddress; } public void setBccAddress(List<String> bccAddress) { this.bccAddress = bccAddress; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public void setToAddress(List<String> toAddress) { this.toAddress = toAddress; } public List<String> getCcAddress() { return ccAddress; } public void setCcAddress(List<String> ccAddress) { this.ccAddress = ccAddress; } }
3、调用下面方法
@Slf4j public class MailUtils { //邮箱信息 private static String mailServerHost="邮箱服务器地址"; private static String mailSenderAddress="发件人地址"; private static String mailSenderNick="发件人名字"; private static String mailSenderUsername="发件人用户名"; private static String mailSenderPermission="发件人密码"; private static String mailSenderPort="端口号"; /** * @description: 发送邮件,HTML格式,支持附件 * @throws * @author Chen Qu * @date 2019/11/13 13:33 */ public static boolean sendEmail(MailInfoDTO mailInfoDTO) { boolean flag = true; try { HtmlEmail email = new HtmlEmail(); // 配置信息 email.setHostName(mailServerHost); email.setFrom(mailSenderAddress, mailSenderNick); email.setAuthentication(mailSenderUsername, mailSenderPermission); email.setCharset("UTF-8"); email.setSubject(mailInfoDTO.getSubject()); email.setHtmlMsg(mailInfoDTO.getContent()); email.setSmtpPort(Integer.parseInt(mailSenderPort)); email.setSslSmtpPort(mailSenderPort); //email.setDebug(true); // 添加附件 List<EmailAttachment> attachments = mailInfoDTO.getAttachments(); if (null != attachments && attachments.size() > 0) { for (int i = 0; i < attachments.size(); i++) { email.attach(attachments.get(i)); } } // 收件人 List<String> toAddress = mailInfoDTO.getToAddress(); if (null != toAddress && toAddress.size() > 0) { for (int i = 0; i < toAddress.size(); i++) { email.addTo(toAddress.get(i)); } } // 抄送人 List<String> ccAddress = mailInfoDTO.getCcAddress(); if (null != ccAddress && ccAddress.size() > 0) { for (int i = 0; i < ccAddress.size(); i++) { email.addCc(ccAddress.get(i)); } } //邮件模板 密送人 List<String> bccAddress = mailInfoDTO.getBccAddress(); if (null != bccAddress && bccAddress.size() > 0) { for (int i = 0; i < bccAddress.size(); i++) { email.addBcc(ccAddress.get(i)); } } email.send(); } catch (EmailException e) { e.printStackTrace(); flag = false; log.error("发送邮件异常:"+e.getMessage()); } return flag; } }