欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot集成E-mail发送各种类型邮件

程序员文章站 2023-12-13 10:23:10
springboot 集成 e-mail发送邮件,供大家参考,具体内容如下 jdk本身有自带发送邮件api,加上springboot在进行封装,使得现在使用起来十分快速简...

springboot 集成 e-mail发送邮件,供大家参考,具体内容如下

jdk本身有自带发送邮件api,加上springboot在进行封装,使得现在使用起来十分快速简洁。
话不多说,参考纯洁的微笑博客,更改jar版本为2.0.4 开干,基本没什么坑。
就是配置邮箱账号密码是,如果是qq邮箱,需要开启po30和stmp服务,并且获取临时授权码。

开启服务链接:

https://mail.qq.com/cgi-bin/frame_html?sid=a5zsbreenm9phyl1&r=a83225170e94773c650a460c10f7a05c 

SpringBoot集成E-mail发送各种类型邮件

与springboot集成

导入jar包

compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.4.release'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:2.0.4.release'

配置邮箱

#邮箱服务器地址,各大运营商不同
spring.mail.host=smtp.qq.com
#用户名
spring.mail.username=9118542413@qq.com
#密码,如果是qq的,要申请临时授权码
spring.mail.password=faw124awfawfawg
spring.mail.default-encoding=utf-8
#以谁来发送邮件
mail.frommail.addr=9118542413@qq.com

发送各种类型的邮件

@service
@slf4j
public class mailserviceimpl implements mailservice {
 @autowired
 private javamailsender mailsender;
 /**
  * the template engine.
  */
 @autowired
 templateengine templateengine;
 @value("${mail.frommail.addr}")
 private string from;

 @override
 public void sendsimplemail(string to, string subject, string content) {
  simplemailmessage message = new simplemailmessage();
  message.setfrom(from);
  message.setto(to);
  message.setsubject(subject);
  message.settext(content);

  try {
   mailsender.send(message);
   log.info("简单邮件已经发送。");
  } catch (exception e) {
   log.error("发送简单邮件时发生异常!", e);
  }
 }

 @override
 public void sendhtmlmail(string to, string subject, string content) {
  mimemessage message = mailsender.createmimemessage();

  try {
   //true表示需要创建一个multipart message
   mimemessagehelper helper = new mimemessagehelper(message, true);
   helper.setfrom(from);
   helper.setto(to);
   helper.setsubject(subject);
   helper.settext(content, true);
   mailsender.send(message);
   log.info("html邮件发送成功");
  } catch (messagingexception e) {
   log.error("发送html邮件时发生异常!", e);
  }
 }

 @override
 public void sendattachmentsmail(string to, string subject, string content, string filepath) {
  mimemessage message = mailsender.createmimemessage();
  try {
   mimemessagehelper helper = new mimemessagehelper(message, true);
   helper.setfrom(from);
   helper.setto(to);
   helper.setsubject(subject);
   helper.settext(content, true);
   filesystemresource file = new filesystemresource(new file(filepath));
   string filename = filepath.substring(filepath.lastindexof(file.separator) + 1);
   helper.addattachment(filename, file);

   mailsender.send(message);
   log.info("带附件的邮件已经发送。");
  } catch (messagingexception e) {
   log.error("发送带附件的邮件时发生异常!", e);
  }
 }

 @override
 public void sendinlineresourcemail(string to, string subject, string content, string rscpath, string rscid) {
  mimemessage message = mailsender.createmimemessage();
  try {
   mimemessagehelper helper = new mimemessagehelper(message, true);
   helper.setfrom(from);
   helper.setto(to);
   helper.setsubject(subject);
   helper.settext(content, true);

   filesystemresource res = new filesystemresource(new file(rscpath));
   helper.addinline(rscid, res);

   mailsender.send(message);
   log.info("嵌入静态资源的邮件已经发送。");
  } catch (messagingexception e) {
   log.error("发送嵌入静态资源的邮件时发生异常!", e);
  }
 }

 @override
 public void sendtemplatemail(string to, string subject, string template, context context) {
  string emailcontent = templateengine.process(template, context);
  sendhtmlmail("15017263512@163.com", "主题:这是模板邮件", emailcontent);
 }
}

测试类:

@runwith(springrunner.class)
@springboottest
public class emailservicetest {
 @autowired
 mailservice mailservice;

 @test
 public void sendsimplemailtest() {
  mailservice.sendsimplemail("15017263512@163.com","test simple mail"," hello this is simple mail");
 }

 @test
 public void sendhtmlmailtest(){
  string content="<html>\n" +
    "<body>\n" +
    " <h3>hello world ! 这是一封html邮件!</h3>\n" +
    "</body>\n" +
    "</html>";
  mailservice.sendhtmlmail("15017263512@163.com","test html mail",content);
 }

 @test
 public void sendattachmentsmail(){
  string filepath="e:\\var\\log\\elktest\\error\\2018-11-30.log";
  mailservice.sendattachmentsmail("15017263512@163.com", "主题:带附件的邮件", "有附件,请查收!", filepath);
 }

 @test
 public void sendinlineresourcemail() {
  string rscid = "neo006";
  string content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscid + "\' ></body></html>";
  string imgpath = "c:\\users\\admin\\pictures\\camera roll\\9499189867_1476052069.jpg";

  mailservice.sendinlineresourcemail("15017263512@163.com", "主题:这是有图片的邮件", content, imgpath, rscid);
 }

 @test
 public void sendtemplatemail() {
  //创建邮件正文
  context context = new context();
  context.setvariable("id", "006");
  mailservice.sendtemplatemail("15017263512@163.com","主题:这是模板邮件",
    "emailtemplate",context);
 }
}

上面的邮箱和密码是我乱填的,注意自己更改。

项目源码以上传至github

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: