spring-boot-starter-mail技術總結


1. spring-boot-starter-mail技術總結

1.1. 配置讀取類SMTPTransport

  1. 在application中需要配置的信息,在此類中都可以看到,可以在此類打斷點查看

1.2. 配置文件

spring.mail.host=smtp.163.com
spring.mail.username=15068610616@163.com
spring.mail.password=xxx
# 啟動ssl
spring.mail.properties.mail.smtp.ssl.enable=true

# 啟動tls
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000

1.3. 測試代碼

@RunWith(SpringRunner.class)
@SpringBootTest
public class ZookeeperApplicationTests {

	@Autowired
	private JavaMailSender mailSender;

	@Test
	public void testMail() throws MessagingException {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		//發件人
		helper.setFrom("15068610616@163.com");
		//收件人
		helper.setTo("15068610616@163.com");
		//標題
		helper.setSubject("subject");
		//文本
		helper.setText("message text");
		//附件
		helper.addAttachment("downFile",new File("D:\\cygwin64\\home\\workspace3\\learn-demo\\zookeeper\\src\\test\\java\\com\\tzxylao\\design\\ZookeeperApplicationTests.java"));
		mailSender.send(mimeMessage);
	}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM