1.引入jar包 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
添加配置 application.yml 本人采用163邮箱
spring:
mail:
host: smtp.163.com #邮箱服务器地址
username: ***@163.com #用户名
password: *** #开启POP3之后设置的客户端授权码
default-encoding: UTF-8 #编码
126邮箱配置
spring.mail.host=smtp.126.com
spring.mail.username=***.126.com
spring.mail.password=*** #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8
qq邮箱配置
spring.mail.host=smtp.qq.com
spring.mail.username=***@qq.com
spring.mail.password=*** #开启POP3之后设置的客户端授权码
spring.mail.default-encoding=UTF-8
2. 邮箱 开启客户端授权码 进入163邮箱,根据提示开启客户端授权码 会让你输入客户端授权码,就是上面配置的密码。
3. 代码实现: 创建一个借口 一个普通文本方法 , 一个富文本方法
package com.juxinli.normandy.service;
import javax.mail.MessagingException;
public interface MailService {
public void sendSimpleMail(String to, String subject, String content);
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException;
}
实现接口:
package com.juxinli.normandy.service.impl;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.juxinli.normandy.service.MailService;
/**
* MailService实现类
*/
@Service
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
//简单文本
@Override
public void sendSimpleMail(String to, String subject, String content) throws MailException {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); // 邮件发送者
message.setTo(to); // 邮件接受者
message.setSubject(subject); // 主题
message.setText(content); // 内容
mailSender.send(message);
}
// 复杂文本 带图片
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
File file = new File(rscPath);
FileSystemResource res = new FileSystemResource(file);
helper.addInline(rscId, res);
mailSender.send(message);
}
}
4.测试接口使用
@Test
public void test2() throws MessagingException {
String to = "***@qq.com";
String subject = "深空之下";
// 此处为linux系统路径
String content = "天地无终极。人命若朝霞";
mailService.sendSimpleMail(to, subject, content);
System.out.println("成功了");
}
@Test
public void test3() {
String to = "***@163.com";
String subject = "今晚要加班,不用等我了";
String rscId = "img110";
String content = "<html><body><img width='250px' src=\'cid:" + rscId + "\'></body></html>";
// 此处为linux系统路径
String imgPath = "/Users/kx/WechatIMG16.jpeg";
try {
mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId);
System.out.println("成功了");
} catch (MessagingException e) {
System.out.println("失败了");
e.printStackTrace();
}
}
5,结果 :因项目只需要简单文本 这里只对简单文本进行测试,富文本没有测试过。可以自行测试,如有问题请自行改正。
收到邮件内容如下: