以QQ邮箱为例。
导入JavaMail都maven坐标。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
进入自己(发送邮件方)QQ邮箱。
找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务区域,如下:
点击开启POP3/SMTP服务,并按要求发送短信。
发送成功后,点击“我已发送”会得到如下授权码(保存好密码配置application.yml时会用),然后点击确定。
在application中做如下配置。
host处如果是qq邮箱就写smtp.qq.com如果是126邮箱就写smtp.126.com依此类推。
package com.xxx.springboot_mail.service;
public interface SendMailService {
//发送邮件
void sendMail();
}
1、发送人,配置文件中的username写的什么,这里就写什么。
2、接收人,给谁发就写谁的。
3、可根据具体要求完善代码,这里只做简单测试。
package com.xxx.springboot_mail.service.impl;
import com.xxx.springboot_mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class SendMailServiceImpl implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;//注入JavaMailSender
//发送人
private String from="xxxxxx.qq.com";
//接收人
private String to="yyyyyy.qq.com";
//标题
private String subject = "测试邮件";
//正文
private String context="测试邮件正文内容";
@Override
public void sendMail() {
SimpleMailMessage message = new SimpleMailMessage();
//谁发的
message.setFrom(from);
//message.setFrom(from+"(abc)");abc将代替from
//发给谁
message.setTo(to);
//邮件标题
message.setSubject(subject);
//邮件内容
message.setText(context);
//调用JavaMailSender的send方法即可发送邮件
javaMailSender.send(message);
}
}
package com.xxx.springboot_mail;
import com.xxx.springboot_mail.service.SendMailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootMailApplicationTests {
@Autowired
private SendMailService sendMailService;
@Test
void contextLoads() {
sendMailService.sendMail();
}
}
正文内容支持html
如下:
发送链接
发送图片(src中为网络文件链接)
private String context="<img src='https://img.zcool.cn/community/01775d57c3d1cd0000018c1bcb4ade.jpg@1280w_1l_2o_100sh.jpg'>百度</a>";//发送图片
注意设置setText参数为true
package com.xxx.springboot_mail.service.impl;
import com.xxx.springboot_mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Service
public class SendMailServiceImpl02 implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;//注入JavaMailSender
//发送人
private String from="xxx.qq.com";
//接收人
private String to="yyy.qq.com";
//标题
private String subject = "测试邮件";
//正文(支持html)setText设置参数true
private String context="测试邮件正文内容";
//private String context="<a href='http://www.baidu.com'>百度</a>";发送链接
//private String context="<img src='https://img.zcool.cn/community/01775d57c3d1cd0000018c1bcb4ade.jpg@1280w_1l_2o_100sh.jpg'>百度</a>";发送图片
@Override
public void sendMail() {
try{
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
//谁发的
helper.setFrom(from);
//message.setFrom(from+"(abc)");abc将代替from
//发给谁
helper.setTo(to);
//邮件标题
helper.setSubject(subject);
//邮件内容
helper.setText(context,true);
javaMailSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
在上面基础上设置如下。
1、创建MimeMessageHelper对象时增加true参数。
2、添加附件
注意addAttachment第一个参数的后缀一定要与你原文件的后缀相同。
package com.xxx.springboot_mail.service.impl;
import com.xxx.springboot_mail.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
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 SendMailServiceImpl02 implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;//注入JavaMailSender
//发送人
private String from="xxxxxx@qq.com";
//接收人
private String to="yyyyyy@qq.com";
//标题
private String subject = "测试邮件";
//正文(支持html)setText设置参数true
//private String context="测试邮件正文内容";
//private String context="<a href='http://www.baidu.com'>百度</a>";//发送链接
private String context="<img src='https://img.zcool.cn/community/01775d57c3d1cd0000018c1bcb4ade.jpg@1280w_1l_2o_100sh.jpg'>百度</a>";//发送图片
@Override
public void sendMail() {
try{
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
//谁发的
helper.setFrom(from);
//message.setFrom(from+"(abc)");abc将代替from
//发给谁
helper.setTo(to);
//邮件标题
helper.setSubject(subject);
//邮件内容
helper.setText(context,true);
File f1 = new File("E:\\xxxx\\a2.png");
File f2 = new File("E:\\xxxx\\b1.jar");
helper.addAttachment("文件名1.png",f1);//文件名叫什么(注意后缀),文件
helper.addAttachment("文件名2.jar",f2);//文件名叫什么(注意后缀),文件
javaMailSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}