1. 效果发送效果图

连续发送了四封邮件:普通文本邮件,带附件的邮件,内容包含图片的邮件,发送html邮件。

普通文本邮件截图

带附件的邮件截图

内容包含图片的邮件截图(图片太大,就截取一部分)

发送html邮件截图

2. 邮件开发准备工作

  • 引入pom文件依赖
    <!-- 邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • 在application.properties 中添加邮箱配置
    spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=jackdking@foxmail.com
spring.mail.password=邮箱授权码,非邮箱登入密码
  • from,即为邮件发送者,一般设置在配置文件中

  • to,邮件接收者,此参数可以为数组,同时发送多人

  • subject,邮件主题

  • content,邮件的主体

  • 邮件发送者 from 一般采用固定的形式写到配置文件中。

  • 在qq邮箱中开启收发邮件步骤

    • 进入邮件开启页面

    • 点击开启,并发送短信

    • 确认发送,邮件收发开启

3. springboot引入mail服务

  • MailServiceImpl类注入邮件API类
/**
* @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;
......
  • 4种邮件类型方法
    /**
* 发送简单邮件
*
* @param to
* @param subject
* @param content
*/
@Override
public void sendSimpleEmail(String to,String subject,String content) {
SimpleMailMessage message = new SimpleMailMessage(); subject="简单文本邮件";
content="你好,我是空白";
to = "jackdking@foxmail.com";//我自己的邮箱 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="<html>\n" +
"<body>\n" +
" <h3>hello world !你好,我是空白 ,发送html邮件!</h3>\n" +
"</body>\n" +
"</html>"; to = "jackdking@foxmail.com";//我自己的邮箱 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 = "jackdking@foxmail.com";//我自己的邮箱
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="<html><body>图片邮件:<img src=\'cid:" + Id + "\' ></body></html>";
String imgPath = "D:\\微信图片_20200524230149.jpg";
to = "jackdking@foxmail.com";//我自己的邮箱 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种邮件发送测试

  • SpringbootMailApplication应用启动类实现了ApplicationRunner接口,应用启动成功就执行run方法,发送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); }
}
  • 应用启动成功,并成功发送了4封邮件

 


转载这篇文章需要标注作者和出处:空白-bittechblog

完整的demo项目,请关注公众号“前沿科技bot“并发送"邮件系统"获取。

Hi,admin 如果你想 注销 ?                              

最新文章

  1. VBS非规范化参考手册(一)
  2. java调用 webservices接口实现天气预报
  3. 第八节 C#的using语句
  4. 结构类模式(四):装饰(Decorator)
  5. 不能执行已释放script的代码
  6. wampserver配置memcache
  7. Python眼睛护士改进版
  8. ARM备忘
  9. 笔记整理--Linux多线程
  10. Unity 游戏框架搭建 (二十) 更安全的对象池
  11. url路径去掉两个opencms
  12. 基于Angular和Spring WebFlux做个小Demo
  13. ubuntu16.04连接wifi
  14. http: server gave HTTP response to HTTPS client &amp; Get https://192.168.2.119/v2/: dial tcp 192.168.2.119:443: getsockopt: connection refused
  15. CANOE入门(三)
  16. python3 小工具
  17. EasyUI DataGrid自适应高度
  18. js 异步加载
  19. 开启Centos系统的SSH服务
  20. ExtJS GridPanel的ColumnModel 动态加载

热门文章

  1. golang之reflect
  2. Redis 学习笔记(一) 字符串 SDS
  3. Ubuntu 配置/etc/fstab参数实现开机自动挂载硬盘
  4. Battery Charging Specification Revision 1.2 中文版本
  5. Coda docs
  6. elasticsearch kibana的安装部署与简单使用(一)
  7. [hdu5372 Segment Game]树状数组
  8. centos下mysql 看不到mysql数据库(密码无法更改)
  9. 关于SpringBoot的外部化配置使用记录
  10. Ubuntu16.04 flask + nginx + uWSGI 部署