1. jar包:html
mail.jarjava
2. java 類安全
package com.yoplore.apollo.wanwei; import java.util.*; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import com.sun.mail.util.MailSSLSocketFactory; public class SendEmail{ public void sendEmail(String senderQQ,String senderQQAuthorizationCode,String recipientQQMail,String ccListQQMail,String mailTitle,String station_name,String station_code ) throws Exception{ //拼接發件人QQ郵箱地址 String senderQQEmail = senderQQ + "@qq.com"; Properties prop = new Properties(); // 開啓debug調試,以便在控制檯查看 prop.setProperty("mail.debug", "false"); // 設置郵件服務器主機名 prop.setProperty("mail.host", "smtp.qq.com"); // 發送服務器須要身份驗證 prop.setProperty("mail.smtp.auth", "true"); // 發送郵件協議名稱 prop.setProperty("mail.transport.protocol", "smtp"); //使用SSL,企業郵箱必需! //開啓安全協議 // 開啓SSL加密,不然會失敗 MailSSLSocketFactory sf = null; try { sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); } catch (Exception e1) { e1.printStackTrace(); } prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); // // //獲取Session對象 // Session s = Session.getDefaultInstance(prop,new Authenticator() { // //此訪求返回用戶和密碼的對象 // @Override // protected PasswordAuthentication getPasswordAuthentication() { // PasswordAuthentication pa = new PasswordAuthentication("wygm@daee.cn", "Ok1234"); // return pa; // } // }); // //設置session的調試模式,發佈時取消 // s.setDebug(true); // 建立session Session session = Session.getInstance(prop); // 經過session獲得transport對象 Transport ts; try { ts = session.getTransport(); // 鏈接郵件服務器:郵箱類型,賬號,受權碼 // qq郵箱受權碼獲取:登陸QQ郵箱 → 設置 → 帳戶 → 找到IMAP/SMTP服務,點擊開啓 ,開啓後會獲取一個受權碼 // 建立郵件 Message message = new MimeMessage(session); // 指明郵件的發件人 message.setFrom(new InternetAddress(senderQQEmail)); // 指明郵件的收件人,如今發件人和收件人是同樣的,那就是本身給本身發 // Message.RecipientType.TO 收件人 // Message.RecipientType.CC 抄送人 // Message.RecipientType.BCC 暗送人 message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientQQMail)); message.setRecipient(Message.RecipientType.CC, new InternetAddress(ccListQQMail)); // 郵件的標題 message.setSubject(mailTitle); MimeBodyPart text = new MimeBodyPart(); text.setContent("<p>王工,你好~<br/></p>我是遠索科技有限公司的工程師<br/>咱們新引入了一個電站數據,電站數據以下:<br/>"+" ① 電站名稱:"+station_name+",電站編碼:"+station_code, "text/html;charset=UTF-8"); // // 準備圖片數據 // MimeBodyPart image = new MimeBodyPart(); // DataHandler dh = new DataHandler(new FileDataSource("E:\\1.png")); // image.setDataHandler(dh); // image.setContentID("xxx.jpg"); // 附件部分 // MimeBodyPart messageBodyPart = new MimeBodyPart(); // String filename = "E:\\1.png"; // DataSource source = new FileDataSource(filename); // messageBodyPart.setDataHandler(new DataHandler(source)); // messageBodyPart.setFileName(filename); // 描述數據關係 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); // mm.addBodyPart(image); // mm.addBodyPart(messageBodyPart); mm.setSubType("related"); message.setContent(mm); message.saveChanges(); // 發送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } catch (Exception e) { // TODO Auto-generated catch block // e.printStackTrace(); throw e; } } }
3. 調用java 類服務器
try { //添加成功,發送郵件通知 SendEmail email = new SendEmail(); email.sendEmail(Configs.senderQQ, Configs.senderQQAuthorizationCode, Configs.recipientQQMail, Configs.CcListQQMail, Configs.mailTitle,station_name,station_code); cmdRes = ConstantUtil.CMD_RES_SUCCESS; resFailReason = newMap.get("failreason").toString(); } catch (Exception e) { cmdRes = ConstantUtil.CMD_RES_FAIL; resFailReason = "添加成功,郵件通知失敗,請聯繫相關人員,手動發送郵件通知第三方!"; }
4. 配置變量session
public static final String senderQQ = "******"; //發件人的QQ號 public static final String senderQQAuthorizationCode = "**********"; //發件人的QQ受權碼, public static final String recipientQQMail = "*****@yeah.net"; //收件人的QQ郵箱 public static final String CcListQQMail = "******@qq.com"; //抄送人的QQ郵箱 public static final String mailTitle = "關於 引、泄水流量數據接入"; //發送郵件主題
5. 關於發件人的QQ受權碼獲取步驟socket
qq郵箱受權碼獲取:登陸QQ郵箱 → 設置 → 帳戶 → 找到IMAP/SMTP服務,點擊開啓 ,開啓後會獲取一個受權碼ide