SpringBoot使用JavaMailSender實現(xiàn)發(fā)送郵件
用了這么久的Spring Boot,我們對Spring Boot的了解應(yīng)該也逐步進(jìn)入正軌了,這篇文章講的案例也在我們的實際開發(fā)中算是比較實用的了,畢竟我們完成注冊功能和對用戶群發(fā)消息,都可以采用到郵箱發(fā)送功能,往下看,我們來看看什么是JavaMailSender。
什么是JavaMailSender
JavaMailSender是Spring Framework中的一個接口,用于發(fā)送電子郵件。它是Spring對JavaMail API的封裝,提供了更簡單和更方便的方式來發(fā)送郵件。
JavaMailSender接口定義了一組發(fā)送郵件的方法,包括發(fā)送簡單文本郵件、發(fā)送帶附件的郵件、發(fā)送HTML格式的郵件等。它隱藏了底層JavaMail API的復(fù)雜性,使得在Spring應(yīng)用中發(fā)送郵件變得更加容易。
在Spring Boot中,你可以通過依賴注入JavaMailSender來使用它。通過配置郵件服務(wù)器的相關(guān)信息,你可以使用JavaMailSender發(fā)送郵件。
JavaMailSender接口的常用實現(xiàn)類是JavaMailSenderImpl,它是基于JavaMail API實現(xiàn)的。除了JavaMailSenderImpl,Spring還提供了其他的實現(xiàn)類,例如MockMailSender用于測試目的。
使用JavaMailSender,你可以方便地發(fā)送郵件,設(shè)置收件人、發(fā)件人、主題、正文等信息,并可以附加文件、設(shè)置抄送、密送等功能。
JavaMailSender是Spring Framework中用于發(fā)送郵件的接口,它簡化了郵件發(fā)送的過程,提供了更高級的抽象和便利性。
來看一個案例:
引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
如其他自動化配置一樣,我們在引入相關(guān)依賴后就需要在application.properties文件中進(jìn)行配置:
spring.mail.host=your-smtp-host spring.mail.port=your-smtp-port spring.mail.username=your-username spring.mail.password=your-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true
請將your-smtp-host、your-smtp-port、your-username和your-password替換為你的實際信息。
然后,在你的代碼中,注入JavaMailSender并使用它來發(fā)送郵件。以下是一個簡單的示例:
例如我們采用qq郵箱:
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
這里的密碼指你獲取的郵箱的授權(quán)碼



在這里獲取你的相關(guān)信息,
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

template.vm
<html>
<body>
<h3>你好, ${username}, 這是一封模版引擎封裝的郵箱信!</h3>
</body>
</html>
之后咱們創(chuàng)建一個測試類進(jìn)行:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ApplicationTests {
@Autowired
private JavaMailSender mailSender;
@Autowired
private VelocityEngine velocityEngine;
//簡單的郵件發(fā)送
@Test
public void sendSimpleMail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("28****70@qq.com");
message.setTo("28****70@qq.com");
message.setSubject("主題:簡單郵件");
message.setText("簡單的郵件內(nèi)容");
mailSender.send(message);
}
//攜帶附件的郵件
@Test
public void sendAttachmentsMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("28****70@qq.com");
helper.setTo("28****70@qq.com");
helper.setSubject("主題:有附件");
helper.setText("內(nèi)容:有附件的郵件");
FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
helper.addAttachment("miaow-1.jpg", file);
helper.addAttachment("miaow-2.jpg", file);
helper.addAttachment("miaow-2.jpg", file);
mailSender.send(mimeMessage);
}
//發(fā)送嵌入靜態(tài)資源
@Test
public void sendInlineMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("28****70@qq.com");
helper.setTo("28****70@qq.com");
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);
}
//發(fā)送模版附件
@Test
public void sendTemplateMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("28****70@qq.com");
helper.setTo("28****70@qq.com");
helper.setSubject("主題:模板郵件");
Map<String, Object> model = new HashedMap();
model.put("username", "miaow");
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "template.vm", "UTF-8", model);
helper.setText(text, true);
mailSender.send(mimeMessage);
}
}
在項目下放置:weixin.jpg圖片,圖片隨便你怎么玩

這樣,你就可以在Spring Boot中使用JavaMailSender發(fā)送郵件了。記得替換示例代碼中的實際信息,以便與你的郵件服務(wù)器配置相匹配。
在使用JavaMailSender發(fā)送郵件時,有一些注意事項需要注意:
郵件服務(wù)器的配置
在使用JavaMailSender發(fā)送郵件之前,你需要配置郵件服務(wù)器的相關(guān)信息,包括SMTP服務(wù)器地址、端口號、用戶名、密碼等。這些信息可以通過在配置文件中添加相關(guān)屬性來實現(xiàn)。
郵件內(nèi)容的設(shè)置
在設(shè)置郵件內(nèi)容時,你需要注意郵件的主題、正文、收件人、發(fā)件人等信息的設(shè)置。如果你需要發(fā)送HTML格式的郵件,需要將郵件內(nèi)容設(shè)置為HTML格式,并設(shè)置相應(yīng)的郵件頭信息。
郵件發(fā)送的異常處理
在發(fā)送郵件時,可能會出現(xiàn)各種異常,例如連接超時、認(rèn)證失敗等。你需要對這些異常進(jìn)行處理,以便及時發(fā)現(xiàn)和解決問題。
郵件發(fā)送的性能問題
在發(fā)送大量郵件時,可能會出現(xiàn)性能問題。你需要注意郵件發(fā)送的頻率和數(shù)量,以避免對郵件服務(wù)器造成過大的負(fù)載。
郵件發(fā)送的安全問題
在發(fā)送郵件時,需要注意郵件的安全性。你需要確保郵件內(nèi)容不包含敏感信息,并且郵件服務(wù)器的認(rèn)證和加密設(shè)置是正確的。
總之,在使用JavaMailSender發(fā)送郵件時,你需要注意郵件服務(wù)器的配置、郵件內(nèi)容的設(shè)置、異常處理、性能問題和安全問題等方面,以確保郵件發(fā)送的順利和安全。
以上就是SpringBoot使用JavaMailSender實現(xiàn)發(fā)送郵件的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot JavaMailSender發(fā)送郵件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot兩種配置文件properties和yml區(qū)別
這篇文章主要為大家介紹了java面試中常見問到的Spring Boot兩種配置文件properties和yml區(qū)別解答,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
SpringBoot配置文件方式,在線yml文件轉(zhuǎn)properties
這篇文章主要介紹了SpringBoot配置文件方式,在線yml文件轉(zhuǎn)properties,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

