public class SendEmailUtil {?
? ? private static final Logger logger = LoggerFactory.getLogger(SendEmailUtil.class);
? ? private static final String MYEMAILACCOUNT = "***@***.com";
? ? private static final String MYEMAILPASSWORD = "*********"; /
? ? private static final String MYEMAILSMTPHOST = "****";
? ? /**
? ? * @param subject
? ? *? ? ? ? ? ? 標(biāo)題
? ? * @param content
? ? *? ? ? ? ? ? 內(nèi)容
? ? * @param receiveMailAccount
? ? *? ? ? ? ? ? 收件人(多人用 ; 連接)
? ? * @param files
? ? *? ? ? ? ? ? 附件
? ? * @throws Exception
? ? */
? ? public static void sendEmail(String subject, String content, String receiveMailAccount, String... files)
? ? ? ? ? ? throws Exception { // NOSONAR
? ? ? ? // 1. 創(chuàng)建參數(shù)配置, 用于連接郵件服務(wù)器的參數(shù)配置
? ? ? ? Properties props = new Properties(); // 參數(shù)配置
? ? ? ? props.setProperty("mail.transport.protocol", "smtp"); // 使用的協(xié)議(JavaMail規(guī)范要求)
? ? ? ? props.setProperty("mail.smtp.host", MYEMAILSMTPHOST); // 發(fā)件人的郵箱的 SMTP
? ? ? ? props.setProperty("mail.smtp.port", "587"); // 發(fā)件人的郵箱的 SMTP
? ? ? ? props.setProperty("mail.smtp.auth", "true"); // 需要請求認(rèn)證
? ? ? ? props.setProperty("mail.smtp.starttls.enable", "true");
? ? ? ? // 2.
? ? ? ? // 根據(jù)配置創(chuàng)建會話對象,
? ? ? ? // 用于和郵件服務(wù)器交互
? ? ? ? Session session = Session.getDefaultInstance(props);
? ? ? ? session.setDebug(true); // 設(shè)置為debug模式, 可以查看詳細(xì)的發(fā)送 log
? ? ? ? // 3. 創(chuàng)建一封郵件
? ? ? ? MimeMessage message = createMimeMessage(session, subject, MYEMAILACCOUNT, receiveMailAccount, content, files);
? ? ? ? // 4. 根據(jù) Session 獲取郵件傳輸對象
? ? ? ? Transport transport = session.getTransport();
? ? ? ? // 5. 使用 郵箱賬號 和 密碼 連接郵件服務(wù)器, 這里認(rèn)證的郵箱必須與 message 中的發(fā)件人郵箱一致, 否則報錯
? ? ? ? transport.connect(MYEMAILACCOUNT, MYEMAILPASSWORD);
? ? ? ? // 6. 發(fā)送郵件, 發(fā)到所有的收件地址, message.getAllRecipients() 獲取到的是在創(chuàng)建郵件對象時添加的所有收件人,
? ? ? ? // 抄送人, 密送人
? ? ? ? transport.sendMessage(message, message.getAllRecipients());
? ? ? ? // 7. 關(guān)閉連接
? ? ? ? transport.close();
? ? }
? ? /**
? ? * 創(chuàng)建一封只包含文本的簡單郵件
? ? *
? ? * @param session
? ? *? ? ? ? ? ? 和服務(wù)器交互的會話
? ? * @param sendMail
? ? *? ? ? ? ? ? 發(fā)件人郵箱
? ? * @param receiveMail
? ? *? ? ? ? ? ? 收件人郵箱
? ? * @return
? ? * @throws Exception
? ? */
? ? private static MimeMessage createMimeMessage(Session session, String subject, String sendMail, String receiveMail,
? ? ? ? ? ? String content, String... files) throws Exception { // NOSONAR
? ? ? ? // 1. 創(chuàng)建一封郵件
? ? ? ? MimeMessage message = new MimeMessage(session);
? ? ? ? // 2. From: 發(fā)件人(昵稱有廣告嫌疑,避免被郵件服務(wù)器誤認(rèn)為是濫發(fā)廣告以至返回失敗,請修改昵稱)
? ? ? ? message.setFrom(new InternetAddress(sendMail, "***", "UTF-8"));
? ? ? ? // 3. To: 收件人(可以增加多個收件人、抄送、密送)
? ? ? ? message.setRecipients(MimeMessage.RecipientType.TO, parseAddress(receiveMail));
? ? ? ? // 4. Subject: 郵件主題(標(biāo)題有廣告嫌疑,避免被郵件服務(wù)器誤認(rèn)為是濫發(fā)廣告以至返回失敗,請修改標(biāo)題)
? ? ? ? message.setSubject(subject, "UTF-8");
? ? ? ? // 5. Content: 郵件正文(可以使用html標(biāo)簽)(內(nèi)容有廣告嫌疑,避免被郵件服務(wù)器誤認(rèn)為是濫發(fā)廣告以至返回失敗,請修改發(fā)送內(nèi)容)
? ? ? ? /* 添加正文內(nèi)容 */
? ? ? ? Multipart multipart = new MimeMultipart();
? ? ? ? BodyPart contentPart = new MimeBodyPart();
? ? ? ? contentPart.setText(content);
? ? ? ? contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
? ? ? ? multipart.addBodyPart(contentPart);
? ? ? ? /* 添加附件 */
? ? ? ? if (null != files) {
? ? ? ? ? ? for (String file : files) {
? ? ? ? ? ? ? ? File usFile = new File(file);
? ? ? ? ? ? ? ? MimeBodyPart fileBody = new MimeBodyPart();
? ? ? ? ? ? ? ? DataSource source = new FileDataSource(file);
? ? ? ? ? ? ? ? fileBody.setDataHandler(new DataHandler(source));
? ? ? ? ? ? ? ? fileBody.setFileName(MimeUtility.encodeWord(usFile.getName()));
? ? ? ? ? ? ? ? multipart.addBodyPart(fileBody);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? message.setContent(multipart);
? ? ? ? // 6. 設(shè)置發(fā)件時間
? ? ? ? message.setSentDate(new Date());
? ? ? ? // 7. 保存設(shè)置
? ? ? ? message.saveChanges();
? ? ? ? return message;
? ? }
? ? private static Address[] parseAddress(String addr) {
? ? ? ? StringTokenizer token = new StringTokenizer(addr, ";");
? ? ? ? Address[] addrArr = new Address[token.countTokens()];
? ? ? ? int i = 0;
? ? ? ? while (token.hasMoreTokens()) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? addrArr[i] = new InternetAddress(token.nextToken());
? ? ? ? ? ? } catch (AddressException e) {
? ? ? ? ? ? ? ? logger.error("parseAddress", e);
? ? ? ? ? ? ? ? return null; // NOSONAR
? ? ? ? ? ? }
? ? ? ? ? ? i++;
? ? ? ? }
? ? ? ? return addrArr;
? ? }
? ? public static void main(String[] args) {
? ? ? ? try {
? ? ? ? ? ? sendEmail("測試", "試試", "***@***.com");
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? logger.error("", e);
? ? ? ? }
? ? }
}
發(fā)郵件(JAVA)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
- 懶惰的人總有方法不讓自己去做簡單、重復(fù)的事情。 緣由 今天是3月10號,是紹興市上虞區(qū)教育體育局發(fā)布2017年紹興...
- 本文包括:1、名詞解釋2、郵件收發(fā)過程3、JavaMail 知識概要4、發(fā)送一封符合 MIME 協(xié)議的 JavaM...