• Spring Boot中实现发送文本、带附件和HTML邮件


    SpringBoot实现发送邮箱

    在这里插入图片描述

    引言

    在现代应用程序中,电子邮件通常是不可或缺的一部分。在Spring Boot中,你可以轻松地实现发送不同类型的邮件,包括文本、带附件和HTML邮件。本博客将向你展示如何使用Spring Boot发送这些不同类型的电子邮件。

    步骤一:导入依赖

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-mailartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    步骤二:配置邮箱信息

    mail:
      # 邮件 SMTP 服务器的主机名
      host: smtp.qq.com
      # 用于登录 SMTP 服务器的邮箱地址
      username: 1111111@qq.com
      # 用于登录 SMTP 服务器的邮箱密码或授权码
      password: abcdefghijklmnopqrstuvwxyz
      # SMTP 服务器的端口
      port: 587
      # 是否启用 SMTP 认证,通常应设置为 true
      smtpAuth: true
      # 是否启用 STARTTLS 安全传输协议,通常应设置为 true
      smtpStarttlsEnable: true
      # 是否要求使用 STARTTLS 安全传输协议,通常应设置为 true
      smtpStarttlsRequired: true
      # 默认编码
      defaultEncoding: UTF-8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    步骤三:邮箱配置类

    @Data
    //配置属性文件
    @Component
    //说明配置文件属性的头部
    @ConfigurationProperties(prefix = "mail")
    public class MailConfig {
        private String host;
        private String username;
        private String password;
        private Integer port;
        private String smtpAuth;
        private String smtpStarttlsEnable;
        private String smtpStarttlsRequired;
        private String defaultEncoding;
    
        @Bean
        public JavaMailSender javaMailSender() {
            //邮箱发送对象
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            javaMailSender.setHost(host);
            javaMailSender.setPort(port);
            javaMailSender.setUsername(username);
            javaMailSender.setPassword(password);
            javaMailSender.setDefaultEncoding(defaultEncoding);
            // 配置其他属性,如协议、调试等,根据需要
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", smtpAuth);
            properties.setProperty("mail.smtp.starttls.enable", smtpStarttlsEnable);
            properties.setProperty("mail.smtp.starttls.required", smtpStarttlsRequired);
            javaMailSender.setJavaMailProperties(properties);
            return javaMailSender;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    步骤四:创建邮箱工具类

    这里的发送人必须设置不然会报异常:501 Mail from address must be same as authorization user.

    @Component
    public class MailUtils {
        @Value("${mail.username}")
        private String username;
        @Resource
        private JavaMailSender javaMailSender;
    
        /**
         * 邮箱发送
         *
         * @param to      收信人
         * @param title   邮箱标题
         * @param content 邮箱内容
         */
        public void sendMail(String to, String title, String content) {
            //邮箱消息对象
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(username);//发送人
            simpleMailMessage.setTo(to);//收件人
            simpleMailMessage.setSubject(title);//邮箱标题
            simpleMailMessage.setText(content);//邮箱内容
            //实现发送邮箱
            javaMailSender.send(simpleMailMessage);
        }
    
        /**
         * 群发邮箱
         *
         * @param toList  收信人集合
         * @param title   邮箱标题
         * @param content 邮箱内容
         */
        public void sendEmailToMultipleRecipients(List<String> toList, String title, String content) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(username);//发送人
            message.setTo(toList.toArray(new String[0]));
            message.setSubject(title);
            message.setText(content);
            javaMailSender.send(message);
        }
    
        /**
         * 发送HTML邮箱
         *
         * @param to       收信人
         * @param title    邮箱标题
         * @param text     HTML内容
         * @param filePath 文件路径
         * @throws MessagingException 邮箱异常
         */
        public void sendEmailWithAttachment(String to, String title, String text, String filePath) throws MessagingException {
            MimeMessage message = javaMailSender.createMimeMessage();
            message.setFrom(username);//发送人
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(title);
            helper.setText(text);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            helper.addAttachment(FileUtil.getName(filePath), file);
            javaMailSender.send(message);
        }
    
        /**
         * 发送HTML邮箱
         *
         * @param to    收信人
         * @param title 邮箱标题
         * @param text  HTML内容
         * @param file  文件
         * @throws MessagingException 邮箱异常
         */
        public void sendEmailWithAttachment(String to, String title, String text, File file) throws MessagingException {
            MimeMessage message = javaMailSender.createMimeMessage();
            message.setFrom(username);//发送人
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(title);
            helper.setText(text);
            helper.addAttachment(FileUtil.getName(file), file);
            javaMailSender.send(message);
        }
    
        /**
         * 发送HTML邮箱
         *
         * @param to          收信人
         * @param title       邮箱标题
         * @param htmlContent HTML内容
         * @throws MessagingException 邮箱异常
         */
        public void sendHtmlEmail(String to, String title, String htmlContent) throws MessagingException {
            MimeMessage message = javaMailSender.createMimeMessage();
            message.setFrom(username);//发送人
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(title);
            helper.setText(htmlContent, true); // 设置为true表示HTML内容
            javaMailSender.send(message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    通过使用Spring Boot和JavaMailSender,你可以轻松地实现发送文本、带附件和HTML邮件的功能。这些示例可以帮助你在你的应用程序中集成邮件发送功能,以便满足不同类型的邮件需求

  • 相关阅读:
    Matlab多维数组漫谈教程
    docker install private registry 【docker 安装 registry & 仅证书认证】
    【srs】srs-gb28181 是独立分支
    【软件项目管理】期末复习知识点整理
    电子学会2021年3月青少年软件编程(图形化)等级考试试卷(三级)答案解析
    ML.NET相关资源整理
    uniapp微信小程序隐私保护引导新规
    基于Monkey的稳定性测试
    ensp简单入门
    java分布式锁
  • 原文地址:https://blog.csdn.net/weixin_45626288/article/details/133685920