宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產(chǎn)品動態(tài)
精選推薦

Spring Boot中使用JavaMailSender發(fā)送郵件

管理 管理 編輯 刪除

快速入門

在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依賴:


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

如其他自動化配置模塊一樣,在完成了依賴引入之后,只需要在application.properties中配置相應(yīng)的屬性內(nèi)容。

下面我們以QQ郵箱為例,在application.properties中加入如下配置(注意替換自己的用戶名和密碼):


spring.mail.host=smtp.qq.com
spring.mail.username=用戶名
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

通過單元測試來實現(xiàn)一封簡單郵件的發(fā)送:


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {

	@Autowired
	private JavaMailSender mailSender;

	@Test
	public void sendSimpleMail() throws Exception {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom("[email protected]");
		message.setTo("[email protected]");
		message.setSubject("主題:簡單郵件");
		message.setText("測試郵件內(nèi)容");

		mailSender.send(message);
	}

}

到這里,一個簡單的郵件發(fā)送就完成了,運行一下該單元測試,看看效果如何?

由于Spring Boot的starter模塊提供了自動化配置,所以在引入了spring-boot-starter-mail依賴之后,會根據(jù)配置文件中的內(nèi)容去創(chuàng)建JavaMailSender實例,因此我們可以直接在需要使用的地方直接@Autowired來引入郵件發(fā)送對象。

#進階使用

在上例中,我們通過使用SimpleMailMessage實現(xiàn)了簡單的郵件發(fā)送,但是實際使用過程中,我們還可能會帶上附件、或是使用郵件模塊等。這個時候我們就需要使用MimeMessage來設(shè)置復(fù)雜一些的郵件內(nèi)容,下面我們就來依次實現(xiàn)一下。

#發(fā)送附件

在上面單元測試中加入如下測試用例(通過MimeMessageHelper來發(fā)送一封帶有附件的郵件):


	@Test
	public void sendAttachmentsMail() throws Exception {

		MimeMessage mimeMessage = mailSender.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("[email protected]");
		helper.setTo("[email protected]");
		helper.setSubject("主題:有附件");
		helper.setText("有附件的郵件");

		FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
		helper.addAttachment("附件-1.jpg", file);
		helper.addAttachment("附件-2.jpg", file);

		mailSender.send(mimeMessage);

	}

#嵌入靜態(tài)資源

除了發(fā)送附件之外,我們在郵件內(nèi)容中可能希望通過嵌入圖片等靜態(tài)資源,讓郵件獲得更好的閱讀體驗,而不是從附件中查看具體圖片,下面的測試用例演示了如何通過MimeMessageHelper實現(xiàn)在郵件正文中嵌入靜態(tài)資源。


	@Test
	public void sendInlineMail() throws Exception {

		MimeMessage mimeMessage = mailSender.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("[email protected]");
		helper.setTo("[email protected]");
		helper.setSubject("主題:嵌入靜態(tài)資源");
		helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);

		FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
		helper.addInline("weixin", file);

		mailSender.send(mimeMessage);

	}

這里需要注意的是addInline函數(shù)中資源名稱weixin需要與正文中cid:weixin對應(yīng)起來

#模板郵件

通常我們使用郵件發(fā)送服務(wù)的時候,都會有一些固定的場景,比如重置密碼、注冊確認(rèn)等,給每個用戶發(fā)送的內(nèi)容可能只有小部分是變化的。所以,很多時候我們會使用模板引擎來為各類郵件設(shè)置成模板,這樣我們只需要在發(fā)送時去替換變化部分的參數(shù)即可。

在Spring Boot中使用模板引擎來實現(xiàn)模板化的郵件發(fā)送也是非常容易的,下面我們以velocity為例實現(xiàn)一下。

引入velocity模塊的依賴:


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-velocity</artifactId>
</dependency>

resources/templates/下,創(chuàng)建一個模板頁面template.vm


<html>
<body>
    <h3>你好, ${username}, 這是一封模板郵件!</h3>
</body>
</html>

我們之前在Spring Boot中開發(fā)Web應(yīng)用時,提到過在Spring Boot的自動化配置下,模板默認(rèn)位于resources/templates/目錄下

最后,我們在單元測試中加入發(fā)送模板郵件的測試用例,具體如下:


	@Test
	public void sendTemplateMail() throws Exception {

		MimeMessage mimeMessage = mailSender.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom("[email protected]");
		helper.setTo("[email protected]");
		helper.setSubject("主題:模板郵件");

		Map<String, Object> model = new HashedMap();
		model.put("username", "didi");
		String text = VelocityEngineUtils.mergeTemplateIntoString(
				velocityEngine, "template.vm", "UTF-8", model);
		helper.setText(text, true);

		mailSender.send(mimeMessage);
	}

嘗試運行一下,就可以收到內(nèi)容為你好, didi, 這是一封模板郵件!的郵件。這里,我們通過傳入username的參數(shù),在郵件內(nèi)容中替換了模板中的${username}變量。

#代碼示例

本文的相關(guān)例子可以查看下面?zhèn)}庫中的chapter4-5-1目錄:


請登錄后查看

CRMEB 最后編輯于2025-02-21 11:38:42

快捷回復(fù)
回復(fù)
回復(fù)
回復(fù)({{post_count}}) {{!is_user ? '我的回復(fù)' :'全部回復(fù)'}}
排序 默認(rèn)正序 回復(fù)倒序 點贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無簡介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復(fù) {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復(fù)' : '回復(fù)'}}
刪除
回復(fù)
回復(fù)
查看更多
打賞
已打賞¥{{reward_price}}
1170
{{like_count}}
{{collect_count}}
添加回復(fù) ({{post_count}})

相關(guān)推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產(chǎn)品動態(tài) 精選推薦 首頁頭條 首頁動態(tài) 首頁推薦
取 消 確 定
回復(fù)
回復(fù)
問題:
問題自動獲取的帖子內(nèi)容,不準(zhǔn)確時需要手動修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當(dāng)前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請輸入 0.1-{{reward_max_price}} 范圍內(nèi)的數(shù)值
打賞成功
¥{{price}}
完成 確認(rèn)打賞

微信登錄/注冊

切換手機號登錄

{{ bind_phone ? '綁定手機' : '手機登錄'}}

{{codeText}}
切換微信登錄/注冊
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服