欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Simple Java Mail郵件發(fā)送實現(xiàn)過程解析

 更新時間:2020年11月06日 09:50:50   作者:shuzihua  
這篇文章主要介紹了Simple Java Mail郵件發(fā)送實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

前言

在我們?nèi)粘9ぷ髦?,郵件發(fā)送服務(wù)經(jīng)常會用到,我們常用的java郵件服務(wù)實現(xiàn)方案有:java原生自帶的javamail、apache commons mail工具包、spring mail。但是個人使用這么久而言,感覺使用起來都不太順手,也略顯復(fù)雜

在此推薦一個簡單易用的類庫simple-java-mail

github地址: http://www.simplejavamail.org

下面我會介紹一下這個mail工具類的基本用法,不過基本都是來自于官網(wǎng),隨后我會基于這個mail工具類去封裝一個基本通用的郵件服務(wù)。

maven引入

<dependency>
  <groupId>org.simplejavamail</groupId>
  <artifactId>simple-java-mail</artifactId>
  <version>4.2.3-java6-release</version>
</dependency>

例子

發(fā)送一封簡易郵件

寫法1 Builder模式:

Email email = new EmailBuilder()
.from("Michel Baker", "m.baker@mbakery.com")
.to("mom", "jean.baker@hotmail.com")
.to("dad", "StevenOakly1963@hotmail.com")
.subject("My Bakery is finally open!")
.text("Mom, Dad. We did the opening ceremony of our bakery!!!")
.build();

new Mailer("server", 25, "username", "password").sendMail(email);

寫法二 常規(guī)模式:

Email email = new Email();

email.setFromAddress("Michel Baker", "m.baker@mbakery.com");
email.addRecipient("mom", "jean.baker@hotmail.com", RecipientType.TO);
email.addRecipient("dad", "StevenOakly1963@hotmail.com", RecipientType.TO);
email.setSubject("My Bakery is finally open!");
email.setText("Mom, Dad. We did the opening ceremony of our bakery!!!");

new Mailer("server", 25, "username", "password").sendMail(email);

和spring結(jié)合使用

<bean id="inhouseMailer" class="org.simplejavamail.mailer.Mailer">
  <constructor-arg value="server" />
  <constructor-arg value="25" />
  <constructor-arg value="username" />
  <constructor-arg value="password" />
</bean>

@Autowired 
Mailer inhouseMailer; 
inhouseMailer.sendMail(email);
inhouseMailer.sendMail(anotherEmail);

添加多個接收者

//添加多個接收者
email.addRecipients(yourRecipient1, yourRecipient2...);
//也可以通過逗號“,”分割多個抄送人
String list = "twister@sweets.com,blue.tongue@sweets.com;honey@sweets.com";
emailNormal.addRecipients(list, RecipientType.BCC);

builder模式:
.to(yourRecipient1, yourRecipient2...)
.bcc("twister@sweets.com,blue.tongue@sweets.com;honey@sweets.com")
...
.build();

支持異步發(fā)送

// 第二個參數(shù)是true則是異步發(fā)送,false則是同步發(fā)送
mailer.sendMail(email, true);

配置SSL或TLS

Email email = new Email();

mailer.sendMail(email, TransportStrategy.SMTP_PLAIN); // 此為默認值,不加嵌套任何傳輸協(xié)議
mailer.sendMail(email, TransportStrategy.SMTP_SSL);
mailer.sendMail(email, TransportStrategy.SMTP_TLS);

我們也可以在初始化郵件服務(wù)器配置時聲明傳輸協(xié)議

new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);

發(fā)送附件

Email email = new Email();

email.addAttachment("dresscode.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"));
email.addAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain");

// 當然,你可以傳輸任何文件格式的附件

email.addAttachment("invitation.pdf", new FileDataSource("invitation_v8.3.pdf"));

內(nèi)容嵌套圖片

Email email = new Email();

email.addEmbeddedImage("smiley", new FileDataSource("smiley.jpg"));

String base64String = "iVBORw0KGgoAAAANSUhEUgAAA ...snip";
email.addEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png");

// 圖片需要在html文本中通過cid:xxxx,的方式引用
<p>Let's go!</p>![](cid:thumbsup)<br/>
<p>Smile!</p>![](cid:smiley)

自定義發(fā)送頭

Email email = new Email();

email.addHeader("X-Priority", 2);
email.addHeader("X-MC-GoogleAnalyticsCampaign", "halloween_sale");
email.addHeader("X-MEETUP-RECIP-ID", "71415272");
email.addHeader("X-my-custom-header", "foo");

驗證郵箱合法性

具體使用的工具類是email-rfc2822-validator

github地址:https://github.com/bbottema/email-rfc2822-validator

//經(jīng)過使用發(fā)現(xiàn),貌似只是用正則表達式去驗證郵箱是否合法
EmailAddressValidator.isValid("your_address@domain.com",
EmailAddressCriteria.RFC_COMPLIANT);
EmailAddressValidator.isValid("your_address@domain.com",
EnumSet.of(EmailAddressCriteria.ALLOW_QUOTED_IDENTIFIERS, EmailAddressCriteria.ALLOW_PARENS_IN_LOCALPART));

使用代理發(fā)送

// anonymous proxy
new Mailer(serverConfig, new ProxyConfig("proxy.host.com", 1080));

// authenticated proxy
new Mailer(serverConfig, new ProxyConfig("proxy.host.com", 1080, "proxy username", "proxy password"));

總結(jié)

此工具類方便易用,簡潔明了,而且支持Builder模式鏈式調(diào)用。有興趣的同學(xué)可以嘗試使用,個人感覺比原生mail,spring mail等易用,更多用法請自行查看官網(wǎng)例子。至于一開始說到的封裝通用的郵件服務(wù),這個由于時間關(guān)系,我放到下一次再實現(xiàn)。謝謝大家的支持,如果此文對你有所幫助,請點個贊,謝謝。

https://github.com/bbottema/simple-java-mail/

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringCache結(jié)合Redis實現(xiàn)指定過期時間和到期自動刷新

    SpringCache結(jié)合Redis實現(xiàn)指定過期時間和到期自動刷新

    本文主要介紹了SpringCache結(jié)合Redis實現(xiàn)指定過期時間和到期自動刷新,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • Dubbo在Spring和Spring Boot中的使用詳解

    Dubbo在Spring和Spring Boot中的使用詳解

    這篇文章主要介紹了Dubbo在Spring和Spring Boot中的使用詳解,需要的朋友可以參考下
    2017-10-10
  • Spring實現(xiàn)上拉刷新和下拉加載效果

    Spring實現(xiàn)上拉刷新和下拉加載效果

    這篇文章主要為大家詳細介紹了Spring實現(xiàn)上拉刷新和下拉加載效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • SpringBoot整合MyBatis和MyBatis-Plus請求后不打印sql日志的問題解決

    SpringBoot整合MyBatis和MyBatis-Plus請求后不打印sql日志的問題解決

    本文主要介紹了SpringBoot整合MyBatis和MyBatis-Plus請求后不打印sql日志的問題解決文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • 在springboot3微項目中如何用idea批量創(chuàng)建單元測試邏輯

    在springboot3微項目中如何用idea批量創(chuàng)建單元測試邏輯

    這篇文章主要介紹了在SpringBoot3項目中使用IntelliJIDEA批量創(chuàng)建單元測試包括準備工作(確保項目配置正確,添加測試依賴),使用IntelliJIDEA創(chuàng)建測試,感興趣的朋友一起看看吧
    2024-10-10
  • @RequestAttribute和@RequestParam注解的區(qū)別及說明

    @RequestAttribute和@RequestParam注解的區(qū)別及說明

    這篇文章主要介紹了@RequestAttribute和@RequestParam注解的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java中處理I/O操作的不同方式

    Java中處理I/O操作的不同方式

    BIO、NIO和AIO是Java中處理I/O操作的三種不同方式,它們分別代表阻塞I/O、非阻塞I/O和異步I/O,本文我們結(jié)合代碼進行一個綜合演示,代碼由于是偽代碼,可能存在不足,僅供大家參考
    2024-02-02
  • SpringCloud使用Feign文件上傳、下載

    SpringCloud使用Feign文件上傳、下載

    這篇文章主要為大家詳細介紹了SpringCloud使用Feign文件上傳、下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Java原生操作JDBC連接以及原理詳解

    Java原生操作JDBC連接以及原理詳解

    這篇文章主要給大家介紹了關(guān)于Java原生操作JDBC連接以及原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java字符串split使用方法代碼實例

    Java字符串split使用方法代碼實例

    這篇文章主要介紹了Java字符串split使用方法代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07

最新評論