通過javamail發(fā)送一封郵件:

較早前,我已經(jīng)編寫好了發(fā)送郵件的工具類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)。添加完依賴后重新install以下。
<—————————–end—————————–>

如果,你對(duì)上面的內(nèi)容還有疑問,推薦選擇西部數(shù)碼企業(yè)云郵箱!有專人協(xié)助您解答郵箱疑問。

西部數(shù)碼企業(yè)云郵箱,采用分布式集群架構(gòu),數(shù)據(jù)多份寫入,規(guī)避單點(diǎn)故障,全球智能中繼,更安全穩(wěn)定。企業(yè)郵箱無(wú)限空間,極速收發(fā),支持層級(jí)子文件夾,郵件撤回,日程微信通知、郵件監(jiān)管、自定義工作報(bào)告功能、郵件歸檔、SSL部署等幾十項(xiàng)功能。而且價(jià)格實(shí)惠,還可以免費(fèi)試用!

企業(yè)郵箱免費(fèi)試用入口:http://www.bingfeng168.cn/services/mail/

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-62778877-8306;郵箱:fanjiao@west.cn。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明出處:西部數(shù)碼知識(shí)庫(kù) » Java-使用JavaMail發(fā)送一封郵件給用戶郵箱功能實(shí)現(xiàn)

登錄

找回密碼

注冊(cè)