SpringBoot中Mail的使用
1.pom.xml文件内容如下
创新互联的客户来自各行各业,为了共同目标,我们在工作上密切配合,从创业型小企业到企事业单位,感谢他们对我们的要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。专业领域包括成都做网站、成都网站建设、成都外贸网站建设、电商网站开发、微信营销、系统平台开发。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2.属性文件配置如下
# 端口号
server.port=8082
spring.mail.host=smtp.163.com
spring.mail.username=xx@163.com
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
3.测试类如下
package com.springboot.demo.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
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.test.context.junit4.SpringRunner;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @ClassName LeaveControllerTest
* @Description TODO
* @Author yunshuodeng
* @Date 2019-05-06 11:03
* @Version 1.0
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailSendTest {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String senderUserName;
/**
* 发送带静态资源的邮件-单元测试
*/
@Test
public void sendInlineMail() throws Exception{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setFrom(senderUserName);
mimeMessageHelper.setTo("691258810@qq.com");
mimeMessageHelper.setSubject("发送带静态资源邮件");
mimeMessageHelper.setText("
带静态资源的邮件内容 图片:",true);FileSystemResource fileSystemResource = new FileSystemResource(new File("/Users/yunshuodeng/Downloads/QQ20190507-102734.png"));
mimeMessageHelper.addInline("picture",fileSystemResource);
javaMailSender.send(mimeMessage);
}
/**
* 发送带附件的邮件-单元测试
*/
@Test
public void sendAttachmentsMail() throws Exception{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setFrom(senderUserName);
mimeMessageHelper.setTo("691258810@qq.com");
mimeMessageHelper.setSubject("发送带附件邮件");
mimeMessageHelper.setText("这是一个有附件的邮件");
// 附件
FileSystemResource fileSystemResource = new FileSystemResource(new File("/Users/yunshuodeng/Downloads/leavedemo.bpmn20.xml"));
mimeMessageHelper.addAttachment("leavedemo.bpmn20.xml",fileSystemResource);
javaMailSender.send(mimeMessage);
}
/**
* 发送html邮件-单元测试
*/
@Test
public void sendHtmlMail() throws Exception{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setFrom(senderUserName);
mimeMessageHelper.setTo("2247053450@qq.com");
mimeMessageHelper.setSubject("发送html邮件");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("
大标题
");stringBuffer.append("
二标题
");stringBuffer.append("
三标题
");mimeMessageHelper.setText(stringBuffer.toString(),true);
javaMailSender.send(mimeMessage);
}
/**
* 发送简单文本邮件-单元测试
*/
@Test
public void sendSimpleTextMail() throws Exception{
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
// 发送人
simpleMailMessage.setFrom(senderUserName);
// 接收人
simpleMailMessage.setTo("691258810@qq.com");
// 标题
simpleMailMessage.setSubject("发送简单文本邮件");
// 内容
simpleMailMessage.setText("这是文本描述");
javaMailSender.send(simpleMailMessage);
}
}
当前题目:SpringBoot中Mail的使用
网站URL:http://azwzsj.com/article/jophhg.html