通過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/