SpringBoot
【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】
SpringBoot 开发实用篇
文章目录
- SpringBoot
- SpringBoot 开发实用篇
- 5 整合第三方技术
- 5.17 发送多部件邮件
- 5.17.1 发送多部件邮件
- 5.17.2 添加附件
- 5.17.3 小结
5 整合第三方技术
5.17 发送多部件邮件
5.17.1 发送多部件邮件
之前咱们已经完成 了简单邮件的发送
现在我们来玩儿点儿花的
修改发送邮件的实现类
package com.dingjiaxiong.service.impl;
import com.dingjiaxiong.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.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* ClassName: SendMailServiveImpl
* date: 2022/10/22 14:44
*
* @author DingJiaxiong
*/
@Service
public class SendMailServiceImpl implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;
//发送人
private String from = "1xxxxxxx2@qq.com";
//接收人
private String to = "3xxxxxx1@qq.com";
//标题
private String subject = "测试邮件";
//正文
private String context = "<a href='https://www.baidu.com'>点开有惊喜</a>";
@Override
public void sendMail() {
try {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(from + "(小甜甜)");
helper.setTo(to);
helper.setSubject(subject);
helper.setText(context,true);
javaMailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
直接测试!!!
发送成功
看看邮件
直接点
OK, 没毛病【祝福祖国】
这只是写了一个链接,也可以加上图片
随便找一个
再次发送
妙啊
5.17.2 添加附件
先打个jar 包,好家伙,打jar 包的时候又执行了一次
把这玩意儿作为附件发送过去
package com.dingjiaxiong.service.impl;
import com.dingjiaxiong.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.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* ClassName: SendMailServiveImpl
* date: 2022/10/22 14:44
*
* @author DingJiaxiong
*/
@Service
public class SendMailServiceImpl implements SendMailService {
@Autowired
private JavaMailSender javaMailSender;
//发送人
private String from = "16xxxxxxxx2@qq.com";
//接收人
private String to = "30xxxxxxxxx1@qq.com";
//标题
private String subject = "测试邮件";
//正文
private String context = "<img src='https://img2.baidu.com/it/u=2840805738,99066234&fm=253&fmt=auto&app=138&f=JPEG?w=1037&h=500'/><a href='https://www.baidu.com'>点开有惊喜</a>";
@Override
public void sendMail() {
try {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from + "(小甜甜)");
helper.setTo(to);
helper.setSubject(subject);
helper.setText(context,true);
//添加附件,直接把这个程序打成jar 包给它发过去
File f1 = new File("D:\\DingJiaxiong\\IdeaProjects\\SpringBootStudyHeiMa\\springboot\\springboot_23_mail\\target\\springboot_23_mail-0.0.1-SNAPSHOT.jar");
//再添加一张图片
File f2 = new File("D:\\DingJiaxiong\\IdeaProjects\\SpringBootStudyHeiMa\\springboot\\springboot_23_mail\\src\\main\\resources\\test.png");
helper.addAttachment(f1.getName(),f1);
helper.addAttachment("我们一定能够成为我们想要成为的人.png",f2);
javaMailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
直接测试!!!
OK,发送成功了,看看
就是这样
如果更换了邮箱服务,一定要在配置中修改【同样的需要去申请一个“密码”】
OK
回顾一下
- 附件与HTML文本支持
5.17.3 小结
- SpringBoot整合JavaMail发送邮件技巧