SpringBoot系列—简单的邮件系统

1. 效果发送效果图

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

普通文本邮件截图

alt

带附件的邮件截图

alt

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

alt

发送html邮件截图

alt

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邮箱中开启收发邮件步骤

进入邮件开启页面

alt

alt

alt

点击开启,并发送短信

alt

确认发送,邮件收发开启

alt

alt

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=http://www.likecs.com/\'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种邮件发送测试

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyywff.html