1. 邮件发送效果图
2. 邮件开发准备工作
3. springboot引入mail服务
4. 启动应用,开始4种邮件发送测试
1. 效果发送效果图
连续发送了四封邮件:普通文本邮件,带附件的邮件,内容包含图片的邮件,发送html邮件。
普通文本邮件截图
带附件的邮件截图
内容包含图片的邮件截图(图片太大,就截取一部分)
发送html邮件截图
2. 邮件开发准备工作
org.springframework.boot
spring-boot-starter-mail
spring.mail.host=smtp.qq.com
spring.mail.port=587
[email protected]
spring.mail.password=邮箱授权码,非邮箱登入密码
from,即为邮件发送者,一般设置在配置文件中
to,邮件接收者,此参数可以为数组,同时发送多人
subject,邮件主题
content,邮件的主体
邮件发送者 from 一般采用固定的形式写到配置文件中。
在qq邮箱中开启收发邮件步骤
3. springboot引入mail服务
/**
* @author jackdking
* @date 2018/5/3 22:07
*/
@Component
public class MailServiceImpl implements IMailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String mailFrom;
......
/**
* 发送简单邮件
*
* @param to
* @param subject
* @param content
*/
@Override
public void sendSimpleEmail(String to,String subject,String content) {
SimpleMailMessage message = new SimpleMailMessage();
subject="简单文本邮件";
content="你好,我是空白";
to = "[email protected]";//我自己的邮箱
message.setFrom(mailFrom);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
/**
* 发送html邮件
*
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
subject="发送html邮件";
content="\n" +
"\n" +
" hello world !你好,我是空白 ,发送html邮件!
\n" +
"\n" +
"";
to = "[email protected]";//我自己的邮箱
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送带附件的邮件
*
* @param to
* @param subject
* @param content
* @param filepath
*/
@Override
public void sendFileMail(String to, String subject, String content, String filepath) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
subject="发送带附件的邮件";
content="你好,我是空白";
to = "[email protected]";//我自己的邮箱
filepath="D:\\微信图片_20200524230149.jpg";
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
FileSystemResource file = new FileSystemResource(new File(filepath));
String fileName = filepath.substring(filepath.lastIndexOf(File.separator));
helper.addAttachment(fileName,file);
mailSender.send(mimeMessage);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void sendPictureMail(String to, String subject, String content, String picturepath) {
// TODO Auto-generated method stub
String Id = "jackdking1314";
content="图片邮件:";
String imgPath = "D:\\微信图片_20200524230149.jpg";
to = "[email protected]";//我自己的邮箱
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject("这是有图片的邮件");
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(imgPath));
helper.addInline(Id, res);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
4. 启动应用,开始4种邮件发送测试
@SpringBootApplication
public class SpringbootMailApplication implements ApplicationRunner
{
@Autowired
IMailService mailService;
public static void main( String[] args )
{
SpringApplication.run(SpringbootMailApplication.class, args);
}
//启动应用后直接发送邮件
@Override
public void run(ApplicationArguments args) throws Exception {
// TODO Auto-generated method stub
mailService.sendSimpleEmail(null, null, null);
mailService.sendHtmlMail(null, null, null);
mailService.sendFileMail(null, null, null,null);
mailService.sendPictureMail(null, null, null,null);
}
}
转载这篇文章需要标注作者和出处:空白-bittechblog
完整的demo项目,请关注公众号“前沿科技bot“并发送"邮件系统"获取。