springboot发邮件

springboot发邮件
公司有墙,连接不了!!!!

先获取授权码,该为密码
在这里插入图片描述

依赖


        <!--***************************************************************************************************-->
        <!--        <dependency >-->
        <!--            <groupId >com.sun.mail</groupId >-->
        <!--            <artifactId >javax.mail</artifactId >-->
        <!--            <version >1.5.4</version >-->
        <!--        </dependency >-->
        <dependency>
            <groupId>activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.3</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <!--***************************************************************************************************-->

代码:

package com.xxcq.testemali;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

/**
 * @author :chenqiwei
 * @date :Created in 2020/11/12 15:59
 */

@SpringBootTest
public class TestEmail {


    private static String content = "<!DOCTYPE html>"
            + "<html>"
            + "<head>"
            + "<title>测试邮件2</title>"
            + "<meta name=\"content-type\" content=\"text/html; charset=UTF-8\">"
            + "</head>"
            + "<body>"
            + "<p style=\"color:#0FF\">这是一封测试邮件~</p>"
            + "</body>"
            + "</html>"; // 可以用HTMl语言写

    @Autowired
    JavaMailSender mailSender;//自动注入


    @Test
    public void sendEmail() {
        try {
            MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom("xxx@qq.com");//设置发信人,发信人需要和spring.mail.username配置的一样否则报错
            String name = "xxx@qq.com";                    //补全地址
            message.setTo(name);                //设置收信人
            message.setSubject("测试邮件主题");    //设置主题
            message.setText(content, true);    //第二个参数true表示使用HTML语言来编写邮件
//            FileSystemResource file = new FileSystemResource(
//            new File("src/main/resources/static/image/picture.jpg"));
//            helper.addAttachment("图片.jpg", file);//添加带附件的邮件
//            helper.addInline("picture",file);//添加带静态资源的邮件
            this.mailSender.send(mimeMessage);

            System.out.println("sucesss");
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("error");
        }
    }

}

applecation.proprties文件

server.port=8888
#邮箱信息
#QQ邮件服务地址,smtp.qq.com对应的ip
spring.mail.host=183.3.225.42
#用户名
spring.mail.username=xxx@qq.com
#密码(授权码)
spring.mail.password=xxx
##############################################################3
##默认编码UTF-8
#spring.mail.default-encoding=UTF-8
##端口
#spring.mail.port=587
###邮件发送者
##spring.mail.mailFrom=xxx@qq.com
###邮件发送者名称
##spring.mail.mailFromName=xxx@qq.com
#spring.mail.properties.mail.smtp.auth=true
#spring.mail.properties.mail.smtp.starttls.enable=true
#spring.mail.properties.mail.smtp.starttls.required=true
##SSL证数
#spring.mail.properties.mail.smtp.ssl.enable=true
#spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
#spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
#spring.mail.properties.mail.debug=true
#spring.mail.default-encoding=UTF-8
#############################################################
#spring.mail.port=587
#spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true


spring.mail.port=465
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# SSL 配置
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值