• springboot发送邮件


    环境:

    jdk: openjdk11

    springboot: 2.1

    操作系统: win10教育版 1903 未激活

    1. pom.xml ( 部分 )

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>mail</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>mail</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>11</java.version>
        </properties>
    
     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

    2. application.properties

    spring.mail.host=smtp.qq.com
    spring.mail.username=649439330@qq.com
    spring.mail.password=zcwrpmfigqovbb
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true

    3. 单元测试(默认使用 application.properties的配置发送)

    ###注入对象
        @Autowired
        private JavaMailSender jmailSender;
    ### 方法调用
        @Test
        public void sendOneByAutowiredJmailSender() {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("649439330@qq.com");
            message.setTo("wangde007@outlook.com");
            message.setSubject("主题: 简单邮件");
            message.setText("这是一封简单邮件,跨越万水千山,到达这里");
    
            jmailSender.send(message);
        }

    4. 直接调用(smtp用户密码已被混淆,请使用自己的smtp账号密码, qq邮箱开启smtp方式参考 【qq邮箱开启SMTP服务】)

    ##对象声明
        private JavaMailSender mailSender;
    ##对象初始化
        @Before
        public void initSender() {
            JavaMailSenderImpl jmSender = new JavaMailSenderImpl();
    
            jmSender.setHost("smtp.qq.com");
            jmSender.setUsername("649439330@qq.com");
            jmSender.setPassword("zcwrpmfigqovbb");
    //        jmSender.setPort(465);
            jmSender.setProtocol("smtp");
            jmSender.setDefaultEncoding("UTF-8");
    
    
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth","true");
            properties.setProperty("mail.smtp.starttls.enable","true");
            properties.setProperty("mail.smtp.starttls.required","true");
    //        properties.setProperty("","");
    
            jmSender.setJavaMailProperties(properties);
            mailSender = jmSender;
        }
    ## 方法调用
        @Test
        public void sendOne() {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("649439330@qq.com");
            message.setTo("wangde007@outlook.com");
            message.setSubject("主题: 简单邮件");
            message.setText("这是一封简单邮件,跨越万水千山,到达这里");
    
            mailSender.send(message);
        }
    ###附录:
    以上使用对象的包路径如下:
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.mail.Session;
    import java.util.Properties;
  • 相关阅读:
    智能制造及其十大关键技术
    浅谈华为验厂对MES系统的要求
    aps系统切换切记“三要三不要”
    周黑鸭借力MES,推进智能生产
    MES助力伊利集团打造智慧工厂
    智能制造下一个风口:工业智能
    大规模定制模式之于MES的三点思考
    APS中生产计划排程模块的基本原理
    通读审计之wiinews
    wiinews sql注入漏洞分析
  • 原文地址:https://www.cnblogs.com/tu13/p/java_springboot_mail.html
Copyright © 2020-2023  润新知