<—————————–start—————————–>
通過(guò)javamail發(fā)送一封郵件:
較早前,我已經(jīng)編寫(xiě)好了發(fā)送郵件的工具類(lèi)mailutils。有以下要點(diǎn)需要注意:
① 發(fā)件箱的郵件服務(wù)器地址。
② 發(fā)件箱賬號(hào)。
③ 郵箱授權(quán)碼,在所使用的郵箱后臺(tái)管理中設(shè)置。
④ 激活郵件的地址。
public class mailutils { private static string smtp_host = “smtp.163.com”; // 網(wǎng)易 private static string username = “niwotaxuexiba_search@163.com”; // 郵箱賬戶 private static string password = “niwotaxuexiba123”; // 郵箱授權(quán)碼 private static string from = “niwotaxuexiba_search@163.com”; // 使用當(dāng)前賬戶 public static string activeurl = “http://localhost:9003/bos_fore/customer_activemail”; public static void sendmail(string subject, string content, string to) { properties props = new properties(); props.setproperty(“mail.smtp.host”, smtp_host); props.setproperty(“mail.transport.protocol”, “smtp”); props.setproperty(“mail.smtp.auth”, “true”); session session = session.getinstance(props); message message = new mimemessage(session); try { message.setfrom(new internetaddress(from)); message.setrecipient(recipienttype.to, new internetaddress(to)); message.setsubject(subject); message.setcontent(content, “text/html;charset=utf-8”); transport transport = session.gettransport(); transport.connect(smtp_host, username, password); transport.sendmessage(message, message.getallrecipients()); } catch (exception e) { e.printstacktrace(); throw new runtimeexception(“郵件發(fā)送失敗…”); } } //測(cè)試代碼:測(cè)試郵件能否發(fā)送成功 public static void main(string[] args) { sendmail(“測(cè)試郵件”, “你好,你我他學(xué)習(xí)吧”, “niwotaxuexiba_search@163.com”); } }
在sendmail(參數(shù)①,參數(shù)②,參數(shù)③)方法中設(shè)置三個(gè)參數(shù):
① 郵件主題;
② 郵件內(nèi)容;
③ 目標(biāo)郵箱賬戶(發(fā)給誰(shuí))。
需要在pom.xml文件中添加javamail郵件發(fā)送工具包的坐標(biāo)。添加完依賴(lài)后重新install以下。
<—————————–end—————————–>
如果,你對(duì)上面的內(nèi)容還有疑問(wèn),推薦選擇西部數(shù)碼企業(yè)云郵箱!有專(zhuān)人協(xié)助您解答郵箱疑問(wèn)。
西部數(shù)碼企業(yè)云郵箱,采用分布式集群架構(gòu),數(shù)據(jù)多份寫(xiě)入,規(guī)避單點(diǎn)故障,全球智能中繼,更安全穩(wěn)定。企業(yè)郵箱無(wú)限空間,極速收發(fā),支持層級(jí)子文件夾,郵件撤回,日程微信通知、郵件監(jiān)管、自定義工作報(bào)告功能、郵件歸檔、ssl部署等幾十項(xiàng)功能。而且價(jià)格實(shí)惠,還可以免費(fèi)試用!
企業(yè)郵箱免費(fèi)試用入口:https://www.west.cn/services/mail/