注:基础配置->文本邮件->HTML邮件->附件邮件->图片邮件->邮件模板->异常处理html
/** * 发送简单文本文件 * @param to 向谁发送 * @param subject 邮件主题 * @param content 邮件内容 */ public void sendSimpleMail(String to ,String subject,String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(content); message.setFrom(emailConfig.getFrom()); //发送邮件 mailSender.send(message); }
@Test public void sendSimpleMail() throws Exception { mailService.sendSimpleMail("1341933031@qq.com","文本邮件","这是一封文本邮件"); }
/** * 发送HTML邮件 * @param to 向谁发送 * @param subject 邮件主题 * @param content 邮件内容 * @throws MessagingException */ public void sendHtmlMail(String to,String subject,String content) { log.info("发送HTML邮件开始:{},{},{}",to,subject,content); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message,true); helper.setFrom(emailConfig.getFrom()); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); mailSender.send(message); log.info("发送HTML邮件成功!"); } catch (MessagingException e) { log.error("发送HTML邮件失败:",e); } }
@Test public void sendHtmlMail() throws Exception { String content = "<html>\n"+ "<body\n>"+ "<h3>hello world , 这是一封html邮件!</h3>\n"+ "</body>\n"+ "</html>\n"; mailService.sendHtmlMail(to,"html邮件",content); }
/** * 发送附件邮件 * @param to 向谁发送 * @param subject 发送主题 * @param content 内容 * @param filePath 文件路径 多文件转数组参数 * @throws MessagingException */ public void sendAttachmentsMail(String to,String subject,String content,String filePath) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(emailConfig.getFrom()); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName,file); mailSender.send(message); }
@Test public void sendAttachmentsMail() throws Exception { String filePath = "D:\\C语言\\ch7-1 让指针再也不困扰你.docx"; mailService.sendAttachmentsMail(to,"附件邮件","这是一封附件邮件",filePath); }
/** * 带图片的邮件 * @param to 向谁发送 * @param subject 主题 * @param content 内容 * @param rscPath 图片地址 * @param rscId 图片id * @throws MessagingException */ public void sendInlinResourceMail(String to,String subject,String content, String rscPath,String rscId) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(emailConfig.getFrom()); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId,res); // helper.addInline(rscId,res);多张图片 mailSender.send(message); }
@Test public void sendInlinResourceMail() throws Exception { String imgPath = "C:\\Users\\Administrator\\Pictures\\公司\\user.png"; String rscId = "new001"; String content = "<html><body>这是有图片的邮件:<img src=\'cid:"+rscId+ "\'></img></body></html>"; mailService.sendInlinResourceMail(to,"图片邮件",content,imgPath,rscId); }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>邮件模板</title> </head> <body> 你好,感谢您的注册,这是一封验证邮件,请点击下面的连接完成注册,感谢您的支持。<br/> <a href="#" th:href="@{https://github.com/UncleCatMySelf/{id}(id=${id})}">激活帐户</a> </body> </html>
@Test public void testTemplateMailTest() throws MessagingException { Context context = new Context(); context.setVariable("id","01"); String emailContent = templateEngine.process("emailTemplate",context); mailService.sendHtmlMail(to,"模板邮件",emailContent); }
若是本文对你有所帮助,欢迎关注我的技术公众号,或点赞,谢谢。java