<—————————–start—————————–>
通過javamail發(fā)送一封郵件:
較早前,我已經(jīng)編寫好了發(fā)送郵件的工具類mailutils。有以下要點需要注意:
① 發(fā)件箱的郵件服務器地址。
② 發(fā)件箱賬號。
③ 郵箱授權(quán)碼,在所使用的郵箱后臺管理中設置。
④ 激活郵件的地址。
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”; // 使用當前賬戶 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ā)送失敗…”); } } //測試代碼:測試郵件能否發(fā)送成功 public static void main(string[] args) { sendmail(“測試郵件”, “你好,你我他學習吧”, “niwotaxuexiba_search@163.com”); } }
在sendmail(參數(shù)①,參數(shù)②,參數(shù)③)方法中設置三個參數(shù):
① 郵件主題;
② 郵件內(nèi)容;
③ 目標郵箱賬戶(發(fā)給誰)。
需要在pom.xml文件中添加javamail郵件發(fā)送工具包的坐標。添加完依賴后重新install以下。
<—————————–end—————————–>
如果,你對上面的內(nèi)容還有疑問,推薦選擇西部數(shù)碼企業(yè)云郵箱!有專人協(xié)助您解答郵箱疑問。
西部數(shù)碼企業(yè)云郵箱,采用分布式集群架構(gòu),數(shù)據(jù)多份寫入,規(guī)避單點故障,全球智能中繼,更安全穩(wěn)定。企業(yè)郵箱無限空間,極速收發(fā),支持層級子文件夾,郵件撤回,日程微信通知、郵件監(jiān)管、自定義工作報告功能、郵件歸檔、ssl部署等幾十項功能。而且價格實惠,還可以免費試用!
企業(yè)郵箱免費試用入口:https://www.west.cn/services/mail/