1.pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--邮件的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.properties代码;
spring.mail.host=smtp.qq.com
spring.mail.username=//自己邮箱账号(发件人)
spring.mail.password=ladgtaxnwdotbbeg //为授权码,比如若账号是自己qq邮箱,这里的密码不是qq密码而是授权码
spring.mail.default-encoding=UTF-8
service层代码:
package com.feng.email.service;
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.stereotype.Service;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class EmailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
/**
* 发送文本邮件
* @param to 接收人
* @param subject 主题
* @param content 邮件内容
*/
public void sendSimpleMail(String to,String subject,String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(from);
mailSender.send(message);
}
/**
* 发送HTML邮件
* @param to
* @param subject
* @param content
*/
public void sendHtmlMail(String to,String subject,String content) throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(mimeMessage);
}
/**
* 发送带副本的邮件
* @param to
* @param subject
* @param content
*/
public void sendAttachmentMail(String to,String subject,String content,String filepath) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
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 = file.getFilename();
//可以发送多个
helper.addAttachment(filename,file);
// helper.addAttachment(filename+"_test",file);
//进行发送
mailSender.send(message);
}
/**
* 发送图片邮件
*
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
* @throws Exception
*/
public void sendImageMail(String to,String subject,String content,String rscPath,String rscId){
logger.info("发送静态邮件开始: {},{},{},{},{}",to,subject,content,rscPath,rscId);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try{
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
FileSystemResource file = new FileSystemResource(new File(rscPath));
helper.addInline(rscId,file);
mailSender.send(message);
logger.info("发送静态图片邮件成功!");
}catch (Exception e){
logger.error("发送静态邮件失败!",e);
}
}
}
测试类 EmailApplicationTests
package com.feng.email;
import com.feng.email.service.EmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailApplicationTests {
@Resource
EmailService mailService;
/**
* 简单文本邮件发送
*/
@Test
public void contextLoads() {
mailService.sendSimpleMail("dpfenglei@dfl.com.cn","简单文本邮件","这是我的第一封邮件,哈哈...");
}
/**
* HTML邮件发送
*
* @throws Exception
*/
@Test
public void sendHtmlMailTest() throws Exception{
String content = "<html>\n"+
"<body>\n" +
"<h1 style=\"color: red\"> hello world , 这是一封HTML邮件</h1>"+
"</body>\n"+
"</html>";
mailService.sendHtmlMail("dpfenglei@dfl.com.cn","Html邮件发送",content);
}
/**
* 发送副本邮件
*
* @throws Exception
*/
@Test
public void sendAttachmentMailTest() throws Exception{
String filepath = "C:\\Users\\95892\\Desktop\\笔记.txt";
mailService.sendAttachmentMail("dpfenglei@dfl.com.cn","发送副本","这是一篇带附件的邮件",filepath);
}
/**
* 发送图片邮件
*
* @throws Exception
*/
@Test
public void sendImageMailTest() throws Exception{
//发送多个图片的话可以定义多个 rscId,定义多个img标签
String filePath = "C:\\Users\\95892\\Pictures\\Saved Pictures\\8800276_184927469000_2.png";
String rscId = "ligh001";
String content = "<html><body> 这是有图片的邮件: <img src=\'cid:"+rscId+"\'> </img></body></html>";
mailService.sendImageMail("dpfenglei@dfl.com.cn","这是一个带图片的邮件",content,filePath,rscId);
}
}
友情提示:
如采qq邮箱当发件人这需要qq邮箱开通:(进入qq邮箱,点击设置->进入账户开通即可)