即最简单的邮件发送,只有标题和文本内容,就当入门。
首先引入依赖,然后在配置文件中配置,最后测试就可以了。
pom引入依赖:
org.springframework.boot
spring-boot-starter-mail
配置文件,application.properties
### 配置邮件参数
#邮件协议
spring.mail.host=smtp.qq.com
[email protected]
#这里是客户端授权码,不是客户端登陆密码
spring.mail.password=xxxxxxxxxxxxx
#编码格式
spring.mail.default-encoding=UTF-8
写邮件发送的方法:
@Service
public class FirstEmailService {
@Value("${spring.mail.username}") //在项目启动时将配置文件中的信息注入到这里
private String from;
@Autowired
private JavaMailSender javaMailSender; //用来发送邮件
//发送简单邮件
public void sendSimpleMail(String to,String title,String content){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(title);
simpleMailMessage.setText(content);
simpleMailMessage.setFrom(from);
javaMailSender.send(simpleMailMessage);
}
}
开始测试,以及运行后的结果:
@Test
public void testFirstEmail() {
emailService.sendSimpleMail("[email protected]","test--first email","测试邮件发送----第一封");
}
只需要添加方法即可:
//发送html邮件
public void sendHtmlMail(String to,String title,String content) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(title);
helper.setText(content,true);
helper.setFrom(from);
javaMailSender.send(mimeMessage);
}
然后测试:
@Test
public void testSecondEmail() throws MessagingException {
String content = "\n\n" +
"\n " +
"测试邮件发送----第二封
\n" +
"\n" +
"\n" +
"1 2 " +
" \n" +
"\n" +
"lj test " +
" \n" +
"
\n" +
"\n" +
"\n";
emailService.sendHtmlMail("[email protected]", "test--second email", content);
}
添加发送附件的方法
//发送附件邮件
public void sendAttachmentsMail(String to,String title,String content,String filepath) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(title);
helper.setText(content,true);
FileSystemResource fileSystemResource = new FileSystemResource(new File(filepath));
String filename = fileSystemResource.getFilename();//获得文件名称
helper.addAttachment(filename,fileSystemResource);
// helper.addAttachment(filename+“test”,fileSystemResource); 发送多个附件内容
helper.setFrom(from);
javaMailSender.send(mimeMessage);
}
测试
@Test
public void testThirdEmail() throws MessagingException {
String content = "\n\n" +
"\n " +
"测试邮件发送----第二封
\n" +
"\n" +
"\n" +
"1 2 " +
" \n" +
"\n" +
"lj test " +
" \n" +
"
\n" +
"\n" +
"\n";
emailService.sendHtmlMail("[email protected]", "test--second email", content);
}
添加方法:
//发送图片邮件
public void sendPictureMail(String to,String title,String content,String rscPath,String rscId) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setTo(to);
helper.setSubject(title);
helper.setText(content,true);
FileSystemResource rsc = new FileSystemResource(new File(rscPath));
helper.addInline(rscId,rsc);
helper.setFrom(from);
javaMailSender.send(mimeMessage);
}
测试:
@Test
public void testForthEmail() throws MessagingException {
String rscId = "picture001";
String content = "\n\n" +
"\n " +
"测试邮件发送----第四封
\n" +
"\n" +
"发送图片邮件\n" +
"\n"+
"
\n" +
"\n" +
"\n";
emailService.sendPictureMail("[email protected]", "test--forth email", content,"C:\\Users\\lj\\Desktop\\22.jpg",rscId);
}
在这里插入图片描述
GOOD LUCK!!!