1. 程式人生 > >SpringBoot整合郵件傳送

SpringBoot整合郵件傳送

1.郵件的用途

  • 在現在的程式中web進行資訊的驗證基本都是郵箱,圖片驗證隨著人工只能的發展似乎變得易於破解,而簡訊驗證則需要一些費用,而對於個人的小網站還是用郵件進行傳送驗證碼更好。

2.簡單郵件的傳送

  • 只進行一些文字的傳送可以用這個實現,但是太過於簡單,而基本的都是進行啟用和驗證碼強調所以這種基本解決不了驗證的問題。
	//	傳送的程式碼:
	public void sendSimpleMail(String to,String subject,String content){
			/**
			 *
			 * 功能描述: 傳送簡單的郵件
			 * 		to : 目標郵箱
			 * 		subject:標題
			 * 		content:內容
			 * 		from:傳送人郵箱
			 *
			 * @param: [to, subject, content]
			 * @return: void
			 * @auther: ZhangJiahao
			 * @date: 2018/10/2 19:48
			 */
SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setSubject(subject); simpleMailMessage.setTo(to); simpleMailMessage.setText(content); simpleMailMessage.setFrom(from); mailSender.send(simpleMailMessage); }
	@Test
	public void MailServiceTest() {
		/**
		 *
		 * 功能描述: 測試簡單郵件的傳送
		 *
		 * @param: []
		 * @return: void
		 * @auther: ZhangJiahao
		 * @date: 2018/10/2 19:55
		 */
mailService.sendSimpleMail("[email protected]", "程式碼發個郵件", "測試"); }

3.HTML郵件的傳送

  • 對於web中的驗證碼和點選連結進行啟用用HTML郵件的傳送基本能夠解決。
	// 傳送的方法
	public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
		/**
		 *
		 * 功能描述: 傳送HTML郵件
		 * 		to : 目標郵箱
		 * 		subject:標題
		 * 		content:內容
		 * 		from:傳送人郵箱
		 * @param: [to, subject, content]
		 * @return: void
		 * @auther: ZhangJiahao
		 * @date: 2018/10/2 19:49
		 */
MimeMessage mimeMailMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMailMessage,true); helper.setTo(to); helper.setSubject(subject); helper.setFrom(from); // 設定內容是HTML helper.setText(content,true); mailSender.send(mimeMailMessage); }
	@Test
	public void sendHtmlMailTest() {
		/**
		 *
		 * 功能描述: 測試HTML郵件的傳送
		 *
		 * @param: []
		 * @return: void
		 * @auther: ZhangJiahao
		 * @date: 2018/10/2 19:56
		 */
		String content = "<html><body><h3>第一個HTML郵件</h3></body></html>";
		try {
			mailService.sendHtmlMail("[email protected]", "HTML郵件測試", content);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

4.帶附件的郵件的傳送

  • 帶附件的連結很少傳送,一般都是帶有推廣推銷等的。
	// 傳送方法
	public void sendAttachmentsMail(String to, String subject, String content,String filePath) throws MessagingException {
			/**
			 *
			 * 功能描述: 傳送帶有附件的郵件
			 *		to:收件人的郵箱
			 *		subject:主題
			 *		content:內容
			 *		filePath:附件的檔案地址 目前網路路徑失敗
			 * @param: [to, subject, content, filePath]
			 * @return: void
			 * @auther: ZhangJiahao
			 * @date: 2018/10/2 19:51
			 */
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
			helper.setFrom(from);
			helper.setTo(to);
			helper.setText(content,true);
			helper.setSubject(subject);
			FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
			String fileName = fileSystemResource.getFilename();
			helper.addAttachment(fileName,fileSystemResource);
			mailSender.send(mimeMessage);
	
		}
	@Test
	public void sendAttachmentsMailTest() {
		/**
		 *
		 * 功能描述: 測試帶有附件多的郵件的傳送
		 *
		 * @param: []
		 * @return: void
		 * @auther: ZhangJiahao
		 * @date: 2018/10/2 19:56
		 */
		try {
			mailService.sendAttachmentsMail("z[email protected]", "帶有附件的郵件", "測試帶有附件的郵件", "C:\\Users\\96109\\Pictures" +
					"\\timg.jpg");
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}

5.圖片郵件的傳送

  • 圖片郵件的傳送一般是為了美觀,例如朋友間在QQ郵箱發郵件就可以選擇背景圖片,這就相當於那樣。
	public void sendInlinResourceMail(String to, String subject, String content,String rscPath,String rsId) throws MessagingException {
		/**
		 *
		 * 功能描述: 傳送圖片郵件
		 * 		to:收件人的郵箱
		 * 		subject:主題
		 * 		content:內容(包含圖片的內容即:HTML格式,可以用thymeleaf模版)
		 * 		rscPath: 圖片的路徑
		 * 		rsId:圖片的編號,用於生成個content
		 *
		 * @param: [to, subject, content, rscPath, rsId]
		 * @return: void
		 * @auther: ZhangJiahao
		 * @date: 2018/10/2 19:52
		 */
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
		helper.setTo(to);
		helper.setFrom(from);
		helper.setText(content,true);
		helper.setSubject(subject);
		FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
		helper.addInline(rsId,fileSystemResource);
		mailSender.send(mimeMessage);

	}
	@Test
	public void sendInlinResourceMailTest() throws MessagingException {
		/**
		 *
		 * 功能描述: 測試圖片郵件的傳送
		 *
		 * @param: []
		 * @return: void
		 * @auther: ZhangJiahao
		 * @date: 2018/10/2 19:56
		 */
		String rscPath = "C:\\Users\\96109\\Pictures" +"\\timg.jpg";
		String rsId = "img001";
		String content = "<html><body>這是一個帶圖片的郵件:<img src=\'cid:"+rsId+"\'></img></body></html>";
		mailService.sendInlinResourceMail("[email protected]","圖片郵件",content,rscPath,rsId);
	}

6.模版郵件的傳送

  • 模版郵件的傳送其實和HTML郵件的傳送一直,只不過傳送的郵件不再是自己手動拼成的字串了,而是利用thymeleaf的模版頁面,利用TemplateEngine進行獲取頁面資訊並且動態插入資料。這裡引用了thymeleaf,所以需要引入相關的包,建立TemplateEngine屬性。
	@Test
	public void sendTemplatesMail() throws MessagingException {
		/**
		 *
		 * 功能描述: 測試根據thymeleaf模版傳送HTML郵件
		 *
		 * @param: []
		 * @return: void
		 * @auther: ZhangJiahao
		 * @date: 2018/10/2 19:56
		 */
		Context context = new Context();
		context.setVariable("id","100");
		String emailContent = templateEngine.process("templatesMail",context);
		mailService.sendHtmlMail("[email protected]", "這是一個模版郵件", emailContent);
	}

thymeleaf模版檔案,檔名為:templatesMail.html

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta content="text/html;charset=UTF-8"/>
    <meta name="viewport" content="width=device-width,initial-scale=1"/>
    <title>啟用</title>
</head>
<body>
歡迎註冊<br>
<a href="#" th:href="@{http://www.baidu.com/{id}(id=${id})}">啟用帳號</a>

</body>
</html>

7.相關配置

  • application.properties中的配置內容:
# 根據不同的郵箱選擇不同的smtp  QQ: smtp.qq.com
spring.mail.host=smtp.163.com
# 郵箱號
[email protected]
# 可以在相關多的郵箱設定頁面獲取相關的安全碼 並不是登入密碼
spring.mail.password=**
# 設定編碼方式utf-8
spring.mail.default-encoding=utf-8

最後:QQ郵箱傳送帶有連結的郵件似乎會被攔截,不能傳送,本人用163郵箱給QQ郵箱傳送會直接放到垃圾箱中,如果測試成功卻沒收到郵件,則可能給遮蔽了,換一個郵箱在測試一下應該就沒問題了。