这篇文章介绍springboot的邮件发送。
由于很简单就没有分出server和imp之类,只是在controller简单写个方法进行测试。
首先pom文件加入spring-boot-starter-mail,代码如下:
复制代码 4.0.0 com.dalaoyang springboot_mail 0.0.1-SNAPSHOT jar springboot_mail springboot_mail org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-mail org.springframework.boot spring-boot-maven-plugin
接下来是application.properties,里面包含发送邮箱的地址,账号密码,接信地址,由于只是测试,所以都是固定写死的,如果项目中需要,可以根据自行情况进行修改。
##端口号server.port=8888##这里根据自己的情况填写##邮箱服务器地址##QQ smtp.qq.com##sina smtp.sina.cn##aliyun smtp.aliyun.com##163 smtp.163.comspring.mail.host=smtp.aliyun.com##邮箱用户名spring.mail.username=dalaoyang@aliyun.com##邮箱密码(注意:qq邮箱应该使用独立密码,去qq邮箱设置里面获取)spring.mail.password=******##编码格式spring.mail.default-encoding=UTF-8##发送邮件地址mail.fromMail.sender=dalaoyang@aliyun.com##接收邮件地址mail.fromMail.receiver=yangyang@dalaoyang.cn复制代码
然后先展示一下controller的完整代码:
package com.dalaoyang.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email yangyang@dalaoyang.cn * @date 2018/4/10 */@RestControllerpublic class MailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${mail.fromMail.sender}") private String sender; @Value("${mail.fromMail.receiver}") private String receiver; @Autowired private JavaMailSender javaMailSender; /* * * @Description http://localhost:8888/sendMail * @author dalaoyang * @email 397600342@qq.com * @method 发送文本邮件 * @date * @param * @return */ @RequestMapping("/sendMail") public String sendMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(sender); message.setTo(receiver); message.setSubject("大老杨"); message.setText("你好你好你好!"); try { javaMailSender.send(message); logger.info("简单邮件已经发送。"); } catch (Exception e) { logger.error("发送简单邮件时发生异常!", e); } return "success"; } /* * * @Description http://localhost:8888/sendHtmlMail * @author dalaoyang * @email 397600342@qq.com * @method 发送html邮件 * @date * @param * @return */ @RequestMapping("/sendHtmlMail") public String testHtmlMail() { String content="\n" + "\n" + "hello world ! 这是一封Html邮件!
\n" + "\n" + ""; MimeMessage message = javaMailSender.createMimeMessage(); try { //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("html mail"); helper.setText(content, true); javaMailSender.send(message); logger.info("html邮件发送成功"); } catch (MessagingException e) { logger.error("发送html邮件时发生异常!", e); } return "success"; } /* * * @Description http://localhost:8888/sendFilesMail * @author dalaoyang * @email 397600342@qq.com * @method 发送附件邮件 * @date * @param * @return */ @RequestMapping("/sendFilesMail") public String sendFilesMail() { String filePath="/Users/dalaoyang/Downloads/article_tag.sql"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("附件邮件"); helper.setText("这是一封带附件的邮件", true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); javaMailSender.send(message); logger.info("带附件的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送带附件的邮件时发生异常!", e); } return "success"; } /* * * @Description http://localhost:8888/sendInlineResourceMail * @author dalaoyang * @email 397600342@qq.com * @method 发送图片邮件 * @date * @param * @return */ @RequestMapping("/sendInlineResourceMail") public String sendInlineResourceMail() { String Id = "dalaoyang12138"; String content="这是有图片的邮件:"; String imgPath = "/Users/dalaoyang/Downloads/dalaoyang.jpeg"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("这是有图片的邮件"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(Id, res); javaMailSender.send(message); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常!", e); } return "success"; }}复制代码
然后一点一点来,本文分4种情况: 1.发送简单的文本邮件
/* * * @Description http://localhost:8888/sendMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送文本邮件 * @date * @param * @return */ @RequestMapping("/sendMail") public String sendMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(sender); message.setTo(receiver); message.setSubject("大老杨"); message.setText("你好你好你好!"); try { javaMailSender.send(message); logger.info("简单邮件已经发送。"); } catch (Exception e) { logger.error("发送简单邮件时发生异常!", e); } return "success"; }复制代码
访问http://localhost:8888/sendMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功
2.发送html邮件
/* * * @Description http://localhost:8888/sendHtmlMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送html邮件 * @date * @param * @return */ @RequestMapping("/sendHtmlMail") public String testHtmlMail() { String content="\n" + "\n" + "hello world ! 这是一封Html邮件!
\n" + "\n" + ""; MimeMessage message = javaMailSender.createMimeMessage(); try { //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("html mail"); helper.setText(content, true); javaMailSender.send(message); logger.info("html邮件发送成功"); } catch (MessagingException e) { logger.error("发送html邮件时发生异常!", e); } return "success"; }复制代码
访问http://localhost:8888/sendHtmlMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功
3.发送附件邮件
/* * * @Description http://localhost:8888/sendFilesMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送附件邮件 * @date * @param * @return */ @RequestMapping("/sendFilesMail") public String sendFilesMail() { String filePath="/Users/dalaoyang/Downloads/article_tag.sql"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("附件邮件"); helper.setText("这是一封带附件的邮件", true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); javaMailSender.send(message); logger.info("带附件的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送带附件的邮件时发生异常!", e); } return "success"; }复制代码
访问http://localhost:8888/sendFilesMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功
4.发送图片邮件
/* * * @Description http://localhost:8888/sendInlineResourceMail * @author dalaoyang * @email yangyang@dalaoyang.cn * @method 发送图片邮件 * @date * @param * @return */ @RequestMapping("/sendInlineResourceMail") public String sendInlineResourceMail() { String Id = "dalaoyang12138"; String content="这是有图片的邮件:"; String imgPath = "/Users/dalaoyang/Downloads/dalaoyang.jpeg"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("这是有图片的邮件"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(Id, res); javaMailSender.send(message); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常!", e); } return "success"; }复制代码
访问http://localhost:8888/sendInlineResourceMail,可以看到页面上显示success,在去看邮箱可以看到如下图,证明发送成功