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