美文网首页
springboot集成javaMail发送邮件

springboot集成javaMail发送邮件

作者: 菜鸟要逆袭 | 来源:发表于2018-02-13 13:50 被阅读0次

    背景:

    boss想做一个数据库定时查询,如果查询出异常结果就将异常信息都发送到指定邮箱中,于是用到了javaMail,下面就是利用springboot整合javaMail实现该需求。(为了能让大家更快的完成javaMail的集成,后续的集成步骤中有关db操作均跳过)

    相关源码下载

    集成步骤:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.dhcc</groupId>
        <artifactId>mail_demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>mail_demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.10.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!--Thymeleaf-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
            <!--javaMail-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    
    
    • 3 项目配置
      application.properties中进行如下配置
    server.port=9888
    
    #javaMail配置(下面的 spring.mail.host 为发送邮箱的邮箱服务器)
    spring.mail.host=smtp.163.com
    spring.mail.username=xxx@163.com
    spring.mail.password=password
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true
    

    ps:上面提到的spring.mail.host为发送邮箱的邮箱服务器

    • 4 编写发送邮件的工具类SendMail
    public class SendMail {
        //发送普通文本格式邮件
        public static void sendTextMail(MimeMessage message,String username,String recipient,String subject,List<String> content){
            try {
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setFrom(username);
                helper.setTo(recipient);
                helper.setSubject(subject);
                StringBuffer sb = new StringBuffer();
                sb.append(content);
                helper.setText(sb.toString());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    
        //发送html格式邮件
        public void sendHtmlMail(MimeMessage message,String username,String recipient, String subject, String content) {
            try {
                //true表示需要创建一个multipart message
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
                helper.setFrom(username);
                helper.setTo(recipient);
                helper.setSubject(subject);
                helper.setText(content, true);
                System.out.println("html格式邮件发送中...");
            } catch (MessagingException e) {
                System.out.println("html格式邮件发送失败!");
            }
        }
    }
    }
    
    • 5 编写service层,调用发送邮件方法
    public interface MailService {
        /**
         * @Author: hobbes
         * @Description: 获得错误领导项目,并发送提示邮件
         * @Date: 创建于 11:53 2018/2/12 0012
         * @Param:
         * @return
         */
        String checkProject();
    }
    
    @Service
    public class MailServiceImpl implements MailService {
        
        @Autowired
        private JavaMailSender javaMailSender;
    
        @Override
        public String checkProject(){
            try {
                SendMail sendMail = new SendMail();
                MimeMessage message = javaMailSender.createMimeMessage();
                String content = "<html><head><style>td{border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;} table{border:solid #add9c0; border-width:1px 0px 0px 1px;}</style></head><body style=''><h3>截止 " + format.format(new Date()) + " 业绩核算系统中错误领导项目,项目信息如下:</h3>" +
                        "<table><tr><th>合同号</th><th>项目名称</th><th>客户名称</th><th>合同总金额(元)</th><th>合同实收款(元)</th><th>签订日期</th><th>项目所属领导</th><th>提交人</th></tr>" +
                        "<tr><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td></tr>" +
                        "</table></body></html>";
                sendMail.sendHtmlMail(message, "邮件发送者邮箱", "邮件接收者邮箱", "邮件标题", content);
                javaMailSender.send(message);
            }catch(Exception e){
                //跳转至邮件发送失败页面
                return "CheckError";
            }
            //跳转至邮件发送成功页面
            return "CheckSuccess";
        }
    }
    

    ps:此处只给出了调用发送html格式邮件的方法,普通文本格式邮件的调用更简单,可以对照着写

    • 6 controller层调用发送邮件的service
    @Controller
    @RequestMapping("/mail")
    public class MailController {
        @Autowired
        private JavaMailSender javaMailSender;
        @Autowired
        private MailService mailService;
    
        @RequestMapping("/checkProject")
        public String checkProject(){
            return mailService.checkProject();
        }
    
    }
    
    • 7 启动项目,访问localhost:9888/mail/checkProject


      运行成功.png

    总结:

    springboot和javaMail的集成很简单,主要就是获得发送者邮箱的smtp服务器地址和授权密码,做配置之后,调用自己手动封装好的发送邮件方法即可。

    相关文章

      网友评论

          本文标题:springboot集成javaMail发送邮件

          本文链接:https://www.haomeiwen.com/subject/ogjjtftx.html