• 使用spring boot发送邮件


    1.添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>2.1.8.RELEASE</version>
    </dependency>

    2.配置

    开启POP3/SMTP服务

    设置授权码

    application.properties的配置

    spring.mail.host=smtp.126.com
    spring.mail.username= XXX@XX.com #邮箱
    spring.mail.password= XXXXXXXX #授权码
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true

    3.发送邮件的Service

    SendEmailService.java

    package com.tydt.bim.service;
    
    import com.tydt.bim.common.Tasks;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    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;
    
    @Service
    
    public class SendEmailService {
        @Autowired
        JavaMailSender jms;
        private final Logger logger = LoggerFactory.getLogger(SendEmailService.class);
        public String send(String sender,String receiver,String title,String text){
            try{
                //建立邮件消息
                MimeMessage message = jms.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message,true);
                //发送方
                helper.setFrom(sender);
                //接收方
                helper.setTo(receiver);
                //发送的标题
                helper.setSubject(title);
                //发送的内容
                helper.setText(text,true);
                jms.send(message);
            }catch (MessagingException e){
                logger.error(e.getMessage());
            }
            return "success";
        }
    }

    4.测试

    package com.tydt.bim.controller;
    
    import com.tydt.bim.service.SendEmailService;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    public class DemoController {
    
        @Resource
        private SendEmailService sendEmailService;
        
        @GetMapping("/sendMail")
        public String send() {
    
            String sender = "chaoyue_@126.com";
            String receiver = "344381715@qq.com";
            String title = "服务提醒";
            String text = "";
            StringBuffer textTmp = new StringBuffer();
            textTmp.append("<html>
    ");
            textTmp.append("<body>
    ");
            textTmp.append("<h2>XXXXXXXXXX</h2>
    ");
            textTmp.append("XXXXXXXXXXXXXXXXXXXX
    ");
            textTmp.append("</body>
    ");
            textTmp.append("</html>");
            text = textTmp.toString();
            String result = sendEmailService.send(sender, receiver, title, text);
            return result;
        }
    
    }
  • 相关阅读:
    编程实现SQL Server数据库导入导出操作
    C#正则表达式入门
    Winform实现窗体抖动的效果代码
    js获取当前日期,格式为YYYYMMDD
    XML基本知识及其技术指南
    WPF学习心得
    脚本调试
    标题:VS2008简体中文专业版
    使用 XML Schema 定义元素的基本知识2
    C#操作xml文件入门
  • 原文地址:https://www.cnblogs.com/baby123/p/13405435.html
Copyright © 2020-2023  润新知