国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Simple Java Mail郵件發送實現過程解析

瀏覽:108日期:2022-08-21 14:16:48

前言

在我們日常工作中,郵件發送服務經常會用到,我們常用的java郵件服務實現方案有:java原生自帶的javamail、apache commons mail工具包、spring mail。但是個人使用這么久而言,感覺使用起來都不太順手,也略顯復雜

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

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

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

maven引入

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

例子

發送一封簡易郵件

寫法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);

寫法二 常規模式:

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結合使用

<bean 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();

支持異步發送

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

配置SSL或TLS

Email email = new Email();

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

我們也可以在初始化郵件服務器配置時聲明傳輸協議

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);

發送附件

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'));

內容嵌套圖片

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)

自定義發送頭

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

//經過使用發現,貌似只是用正則表達式去驗證郵箱是否合法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));

使用代理發送

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

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

總結

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲在线高清 | 免费高清国产 | 99国内精品久久久久久久 | 一级做性色a爰片久久毛片 一级做性色a爰片久久毛片免费 | 精品一区二区影院在线 | 久久精品国产屋 | 99爱在线视频这里只有精品 | 日韩性色 | 天天看夜夜看 | 一级毛片美国 | 国产精品久久久久久久久久一区 | www.日本高清.com | 国产91香蕉| 欧美在线一级精品 | 国产一区二区三区免费在线视频 | 鲁丝片一区二区三区免费 | 久久久久日韩精品无 | 国产一区二区三区久久精品小说 | 手机看片高清国产日韩片 | 成人免费手机在线看网站 | 日韩毛片欧美一级国产毛片 | 国产高清久久 | 成年人免费看 | 免费国产午夜高清在线视频 | 日本手机看片 | 波多野结衣一区在线观看 | 日韩免费黄色片 | 午夜爽视频| 国产人妖xxxx做受视频 | 日韩美女在线看免费观看 | 欧美一级一极性活片免费观看 | 国产孕妇孕交视频在线观看 | 末满18以下勿进色禁网站 | 免费一级a毛片免费观看欧美大片 | 国产精品yjizz视频网一二区 | 中国国产一级毛片 | 国产精品一区二区三区四区五区 | 成人精品一区二区久久久 | 91精品国产色综合久久 | 美国亚洲成年毛片 | 国产免费亚洲 |