当前位置 博文首页 > 文章内容

    使用spring boot发送邮件

    作者:慕尘 栏目:未分类 时间:2020-07-30 18:01:39

    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>\n");
            textTmp.append("<body>\n");
            textTmp.append("<h2>XXXXXXXXXX</h2>\n");
            textTmp.append("XXXXXXXXXXXXXXXXXXXX\n");
            textTmp.append("</body>\n");
            textTmp.append("</html>");
            text = textTmp.toString();
            String result = sendEmailService.send(sender, receiver, title, text);
            return result;
        }
    
    }