zoukankan      html  css  js  c++  java
  • (十八)SpringBoot之发送QQ邮件

    一、引入maven依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

    二、配置application.properties

    spring.mail.host=smtp.qq.com
    spring.mail.username=邮箱名
    spring.mail.password=这里填邮箱的授权码
    spring.mail.default-encoding=UTF-8
    spring.mail.port=465
    spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    spring.mail.properties.mail.debug=true
    • 注意:
    1. PO3/SMTP服务必须开启

      2.QQ邮箱发送邮件服务器主机名为:smtp.qq.com,必须使用使用SSL(spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory),端口号465或587(spring.mail.port=465

     3. 发送邮件

    package com.shyroke.controller;
    
    import javax.mail.internet.MimeMessage;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/mail")
    public class MailController {
        @Autowired
        JavaMailSender mailSender;
    
        @ResponseBody
        @RequestMapping("/send")
        public Object sendEmail() {
            try {
                final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
                final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setFrom("qweyhj@qq.com");
                message.setTo("865386512@qq.com");
                message.setSubject("测试邮件主题");
                message.setText("测试邮件内容");
                this.mailSender.send(mimeMessage);
    
                return "sucesss";
            } catch (Exception ex) {
                ex.printStackTrace();
                return "error";
            }
        }
    }

     4. 结果

     

     

  • 相关阅读:
    可能用到的两款Linux工具tmux和tpp
    CCF技术动态第68期(云计算专题)...中国云计算发展概况与趋势...
    产生随机数//NULL大写,rand()在cstdlib,不在cmath
    28个Unix/Linux的命令行神器
    oAuth无痛入门指南
    CRM 客户关系管理和云计算...Salesforce企业级云计算的领导者...
    博客终于开通了
    传递与引用
    Java基础概念
    final、finally和finalize(二)
  • 原文地址:https://www.cnblogs.com/shyroke/p/8038899.html
Copyright © 2011-2022 走看看