1.准备工作
使用spring boot发送邮件,需要做一个准备工作,必须打开发送邮件的服务。这里以qq为例,到邮箱设置--> 账户设置--> 找到pop3/smpt服务,点击开启,根据提示,发送短信到官方申请开启,最后会获得一个授权码,这是我们下面需要的。
2.配置文件:application.yml
spring mail: host: smtp.qq.com password: ****** # 刚才申请的授权码 username: ****** # 你的qq邮箱 protocol: smtp test-connection: true default-encoding: utf-8 properties: mail: smtp: auth: true # 认证 ssl: enable: true # SSL starttls: # TLS需要时 enable: true required: true
3.邮件工具类
@Componentpublic class MailUtil { private static final Logger LOGGER = LoggerFactory.getLogger(MailUtil.class); @Value("${spring.mail.username}") public String fromUser; @Autowired private JavaMailSender javaMailSender; /** * 普通邮件发送 * @param email * @throws Exception */ public void sendEmail(Email email) throws Exception { LOGGER.info("发送普通邮件给:{}",email.getToEmail()); SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(fromUser); message.setTo(email.getToEmail()); message.setSubject(email.getSubject()); message.setText(email.getContent()); javaMailSender.send(message); } /** * 带图片发送 * @param email */ public void sendPicture(Email email) throws Exception { LOGGER.info("发送带图片邮件给:{}",email.getToEmail()); MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); //这里可以自定义发信名称比如:setFrom(fromUser,'gq') helper.setFrom(fromUser); helper.setTo(email.getToEmail()); helper.setSubject(email.getSubject()); helper.setText("",true); FileSystemResource file = new FileSystemResource(new File(email.getFilePath())); //pic 名称需与 cid 那里的名称对应 helper.addInline("pic", file); javaMailSender.send(message); } /** * 发送html,如 链接 * @param email */ public void sendHtml(Email email) throws Exception { LOGGER.info("发送带图片邮件给:{}",email.getToEmail()); MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(fromUser); helper.setTo(email.getToEmail()); helper.setSubject(email.getSubject()); helper.setText(email.getContent(),true); javaMailSender.send(message); } /** * 发送附件 * @param email * @throws Exception */ public void sendWithAttachments(Email email) throws Exception { LOGGER.info("发送带附件邮件给:{}",email.getToEmail()); MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8"); helper.setFrom(fromUser); helper.setTo(email.getToEmail()); helper.setSubject(email.getSubject()); helper.setText(email.getContent()); //FileSystemResource file = new FileSystemResource(new File(email.getFilePath())); File file = new File(email.getFilePath()); String fileName = email.getFilePath().substring(email.getFilePath().lastIndexOf(File.separator)); fileName = MimeUtility.encodeWord(fileName,"UTF-8","B"); //发送多个 helper.addAttachment(fileName,file); javaMailSender.send(message); }}
这个工具类的方法只是发送单一内容,复杂的内容,如 图片,附件,文字等,大家可以根据单一的自己去补充下,做个练手。
4.测试用例
@Test void contextLoads() throws Exception { /* 发送纯文本 Email email = new Email() .setToEmail(new String[]{"133******@qq.com"}) .setSubject("测试邮件发送") .setContent("hello,成功的发送了邮件"); mailUtil.sendEmail(email);*/ /*发送模板邮件 Email email = new Email() .setToEmail(new String[]{"133******@qq.com"}) .setSubject("测试模板邮件发送"); Context context = new Context(); context.setVariable("title","模板发送"); context.setVariable("content","lalala"); String process = templateEngine.process("emailTpl/emailTpl", context); email.setContent(process); mailUtil.sendEmail(email);*/ /* Email email = new Email() .setToEmail(new String[]{"133******@qq.com"}) .setSubject("测试邮件发送图片") .setFilePath("C:甥敳獲GQPicturesCamera Roll壁纸2.jpg"); mailUtil.sendPicture(email);*/ Email email = new Email() .setToEmail(new String[]{"133******@qq.com"}) .setSubject("测试邮件发送附件") .setContent("你好,欢迎查阅附件") .setFilePath("C:甥敳獲GQDownloads9月竣工时间表.xls"); mailUtil.sendWithAttachments(email); }
结果如下图:
5.小结
ok,今天的一个小demo就到这里了,大家不妨动手试下。不过要注意里面的细节,比如发送图片的cid等等,一不小心就弄错了。刚刚小编就在发送附件这里出错了,截取文件名的时候写成了(0,length)这种。好了,喜欢小编的朋友可以点个关注哦[呲牙][呲牙][呲牙]