• 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;
  • 相关阅读:
    oracle11g 新特性
    RMAN 报:ORA-19504 ORA-27038
    ORACLE-用户常用数据字典的查询使用方法
    oracle
    收缩 表空间
    oracle 配置 oem
    索引大小及占表的空间
    Oracle 11g Windows 迁移至 Linux
    Python:列表生成式
    Python:字符串处理函数
  • 原文地址:https://www.cnblogs.com/tu13/p/java_springboot_mail.html
Copyright © 2020-2023  润新知