JavaMailSender怎麼發送163和qq郵件

https://blog.csdn.net/Tracycater/article/details/73441010java

 

引入Maven依賴包spring

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
1
2
3
4
163郵箱app

application.propertiesspring-boot

#####163郵箱########
spring.mail.host=smtp.163.com
spring.mail.username=*****@163.com
#163郵箱密碼
spring.mail.password=!@#$%^&*
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
1
2
3
4
5
6
7
8
運行類:測試

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;ui

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class My163MailTest {spa

@Autowired
private JavaMailSender javaMailSender;.net

@Value("${spring.mail.username}")
private String username;blog

@Test
public void testSendSimple() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(username);
message.setTo("*******@qq.com");
message.setSubject("標題:測試標題");
message.setText("測試內容部份");
javaMailSender.send(message);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
QQ郵箱和163郵箱的區別是須要設置受權碼而不是密碼,具體操做參考:
http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256it

application.properties

######qq郵箱########
spring.mail.host=smtp.qq.com
spring.mail.username=******@qq.com
#QQ郵箱受權碼
spring.mail.password=xuojxtkdojvzbhjj
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
1
2
3
4
5
6
7
8
運行類:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class MyQQMailTest {

@Autowired
private JavaMailSender javaMailSender;

@Value("${spring.mail.username}")
private String username;

@Test
public void testSendSimple() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(username);
message.setTo("******@qq.com");
message.setSubject("標題:測試標題");
message.setText("測試內容部份");
javaMailSender.send(message);
}
}
---------------------
做者:羅羅諾亞-小魚
來源:CSDN
原文:https://blog.csdn.net/Tracycater/article/details/73441010

相關文章
相關標籤/搜索