springboot 发送,简单,html格式,带本地附件,带远程附件邮件详解
发布日期:2021-06-28 18:30:42 浏览次数:3 分类:技术文章

本文共 4230 字,大约阅读时间需要 14 分钟。

1、导入相应的jar包

org.springframework.boot
spring-boot-starter-mail

2、配置参数

mail:    host: smtp.qq.com #发送邮件服务器    username: xxx@qq.com #发送邮件的邮箱地址    password:  xxxx #客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的    properties.mail.smtp.port: 465 #端口号465或587    from: xxx@qq.com # 发送邮件的地址,和上面username一致可以任意    properties.mail.smtp.starttls.enable: true    properties.mail.smtp.starttls.required: true    properties.mail.smtp.ssl.enable: true    default-encoding: utf-8

3、检查相应的邮件服务器是否开启服务

4、编写相应的工具

package top.cfl.cflwork.util;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Component;import javax.activation.FileDataSource;import javax.activation.URLDataSource;import javax.mail.internet.MimeMessage;import java.io.File;import java.net.URL;/** * @Author: fly 275300091 * @Description: 邮件工具 * @Date: Create in 2019/08/24 */@Componentpublic class MailUtil {    /**     * Spring Boot 提供了一个发送邮件的简单抽象,使用的是下面这个接口,这里直接注入即可使用     */    @Autowired    private JavaMailSender mailSender;    /**     * 配置文件中我的qq邮箱     */    @Value("${spring.mail.from}")    private String from;    /**     * 简单文本邮件     * @param to 收件人     * @param subject 主题     * @param content 内容     */    public void sendSimpleMail(String to, String subject, String content) {        try {            //创建SimpleMailMessage对象            SimpleMailMessage message = new SimpleMailMessage();            //邮件发送人            message.setFrom(from);            //邮件接收人            message.setTo(to);            //邮件主题            message.setSubject(subject);            //邮件内容            message.setText(content);            //发送邮件            mailSender.send(message);        }catch (Exception e){            e.printStackTrace();        }    }    /**     * html邮件     * @param to 收件人     * @param subject 主题     * @param content 内容     */    public void sendHtmlMail(String to, String subject, String content) {        //获取MimeMessage对象        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper messageHelper;        try {            messageHelper = new MimeMessageHelper(message, true);            //邮件发送人            messageHelper.setFrom(from);            //邮件接收人            messageHelper.setTo(to);            //邮件主题            message.setSubject(subject);            //邮件内容,html格式            messageHelper.setText(content, true);            //发送            mailSender.send(message);            //日志信息            System.out.println("邮件已经发送。");        } catch (Exception e) {            e.printStackTrace();            System.err.println("发送邮件时发生异常!");        }    }    /**     * 带附件的邮件     * @param to 收件人     * @param subject 主题     * @param content 内容     * @param filePath 附件     */    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {        MimeMessage message = mailSender.createMimeMessage();        try {            MimeMessageHelper helper = new MimeMessageHelper(message, true);            helper.setFrom(from);            helper.setTo(to);            helper.setSubject(subject);            helper.setText(content, true);            //本地或项目资源            //FileSystemResource file = new FileSystemResource(new File(filePath));            //FileDataSource fds = new FileDataSource(filePath);            //远程资源            URLDataSource uds=new URLDataSource(new URL(filePath));            helper.addAttachment(uds.getName(), uds);            mailSender.send(message);            //日志信息            System.out.println("邮件已经发送成功,带有附件。");        } catch (Exception e) {            System.err.println("发送邮件时发生异常!");        }    }}

5、测试

public static void main(String[] args) {        MailUtil mailUtil = new MailUtil();//        mailUtil.sendSimpleMail("xxx@163.com","主题:简单邮件测试","内容:简单邮件测试");        mailUtil.sendAttachmentsMail("xxx@163.com","主题:我是带有附件的,带有html格式","

内容:我是带有附件的

","http://www.xxx.com/upload/avatar//2019/0623/6b50262a7be6442d.jpg"); }

 

转载地址:https://blog.csdn.net/xljx_1/article/details/100057073 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java读取SHP格式文件,解决中文乱码
下一篇:Codeforces Round #726 (Div. 2)

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月06日 14时09分43秒