SpringBoot邮件发送
邮件的发送也是使用较多的,下面进行说明。
1.准备工作
这里使用QQ邮箱进行发送邮件,所以要先开启POP3/SMTP服务或IMAP/SMTP服务,登陆网页版QQ邮箱,在设置中找到账户,在下面开启服务即可。
1)新建一个SpringBoot的项目,导入mail依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2)配置邮件的基本信息
spring: mail: #邮件服务器的地址 host: smtp.qq.com #邮件服务器的端口号,465或587 port: 465 #用户账号 username: 1916008067@qq.com #用户密码,这个密码是开启服务后系统显示的密码 password: zysferigqzobxqkfcej default-encoding: UTF-8 properties: mail: #配置SSL连接 smtp: socketFactory.class: javax.net.ssl.SSLSocketFactory #开启dubug debug: true
2.简单邮件
简单邮件只包含文字信息。
第一步:创建一个MailService类
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @Component public class MailService { @Autowired JavaMailSender javaMailSender; //简单邮件 public void sendSimpleMail(String sender,String recipient,String cc,String title,String content){ SimpleMailMessage message=new SimpleMailMessage(); message.setFrom(sender);//发送者 message.setTo(recipient);//收件人 message.setCc(cc);//抄送人 message.setSubject(title);//邮件主题 message.setText(content);//邮件内容 javaMailSender.send(message); } }
第二步:在测试目录创建一个测试类Demo2ApplicationTests
package com.example.demo; import freemarker.template.Configuration; import freemarker.template.Template; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import java.io.File; import java.io.StringWriter; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest class Demo2ApplicationTests { @Autowired private MailService mailService; @Test public void sendSimpleMail(){ mailService.sendSimpleMail( "1916008067@qq.com","1953090026@qq.com", "1953090026@qq.com","通知","今天加班"); } }
第三步:运行测试方法,即可在收件人的邮箱看到邮件的信息。
3.带附件的邮件
第一步:在MailService中添加一个方法
//带附件的邮件 public void sendAttachFileMail(String sender, String recipient, String cc, String title, String content, File file) { try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender);//发送者 helper.setTo(recipient);//收件人 helper.setCc(cc);//抄送人 helper.setSubject(title);//邮件主题 helper.setText(content);//邮件内容 helper.addAttachment(file.getName(),file);//文件 javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } }
第二步:在测试类中添加方法
@Test public void sendAttachFileMail(){ mailService.sendAttachFileMail( "1916008067@qq.com","1953090026@qq.com", "1953090026@qq.com","通知","今天加班", new File("D:\\test\1.txt")); }
第三步:运行测试方法,即可在收件人的邮箱看到邮件的信息和附件。
4.带图片的附件
第一步:在MailService中添加一个方法
//带图片的邮件 public void sendMailWithImage(String sender, String recipient, String cc,String title, String content, String[] srcPath,String[] resId){ if(srcPath.length!=resId.length){ System.out.println("发送失败"); return; } try{ MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender);//发送者 helper.setTo(recipient);//收件人 helper.setCc(cc);//抄送人 helper.setSubject(title);//邮件主题 helper.setText(content,true);//邮件内容,设置为true说明正文是html格式 for (int i = 0; i <srcPath.length ; i++) { FileSystemResource res=new FileSystemResource(new File(srcPath[i])); helper.addInline(resId[i],res); } javaMailSender.send(message); }catch (MessagingException e){ e.printStackTrace(); System.out.println("发送失败"); } }
第二步:在测试类中添加方法
@Test public void sendMailWithImage(){ mailService.sendMailWithImage( "1916008067@qq.com","1953090026@qq.com", "1953090026@qq.com","带图片的邮件", "<div><img src='cid:p01'/><img src='cid:p02'/></div>", new String[]{"D:\\1.png","D:\\2.png"},new String[]{"p01","p02"}); }
第三步:运行测试方法,即可在收件人的邮箱看到邮件的信息。
5.使用Freemarker模板的邮件
第一步:导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
第二步:在MailService中添加一个方法
//模板邮件 public void sendHtmlMail(String sender, String recipient, String cc, String title, String content) { try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender);//发送者 helper.setTo(recipient);//收件人 helper.setCc(cc);//抄送人 helper.setSubject(title);//邮件主题 helper.setText(content,true);//邮件内容 javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } }
第三步:创建邮件模板
在资源目录下的static目录下新建一个名字为mailtemplate.ftl的模板文件
<div>邮箱激活</div> <div>你的注册信息 <table> <tr> <td>用户名</td> <td>${username}</td> </tr> <tr> <td>用户性别</td> <td>${sex}</td> </tr> </table> <div><a href="http://www.baidu.com">点击激活邮箱</a></div> </div>
第四步:创建实体类User
package com.example.demo; public class User { private String username; private String sex; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
第五步:在测试类中添加方法
@Test public void sendHtmlMail() { try { Configuration configuration = new Configuration(Configuration.VERSION_2_3_0); ClassLoader loader = Demo2Application.class.getClassLoader(); configuration.setClassLoaderForTemplateLoading(loader,"static"); Template template = configuration.getTemplate("mailtemplate.ftl"); StringWriter writer = new StringWriter(); User user=new User(); user.setSex("男"); user.setUsername("张三哈哈哈"); template.process(user,writer); mailService.sendHtmlMail( "1916008067@qq.com","1953090026@qq.com", "1953090026@qq.com","模板文件",writer.toString() ); } catch (Exception e) { e.printStackTrace(); } }
第六步:运行测试方法,即可在收件人的邮箱看到邮件的信息。
6.使用Thymeleaf模板的邮件
第一步:导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
第二步:创建模板文件
在资源目录的templates目录下新建一个名为mailtemplate.html的模板文件
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.com"> <head> <meta charset="UTF-8"> <title>邮件</title> </head> <body> <div>邮箱激活</div> <div>你的注册信息 <table> <tr> <td>用户名</td> <td th:text="${username}"></td> </tr> <tr> <td>用户性别</td> <td th:text="${sex}"></td> </tr> </table> <div><a href="http://www.baidu.com">点击激活邮箱</a></div> </div> </body> </html>
第三步:在测试类中添加方法
这里没有在MailService类中添加方法,而是直接使用了上述sendHtmlMail方法。
@Autowired private TemplateEngine templateEngine; @Test public void sendHtmlMail2(){ Context context = new Context(); context.setVariable("username","赵柳"); context.setVariable("sex","女"); String mail=templateEngine.process("mailtemplate.html",context); mailService.sendHtmlMail( "1916008067@qq.com","1953090026@qq.com", "1953090026@qq.com","模板文件2",mail ); }
第四步:运行测试方法,即可在收件人的邮箱看到邮件的信息。
就是这么简单,你学废了吗?感觉有用的话,给笔者点个赞吧 !