java傳送郵件基礎方法(另附部分主流郵箱伺服器地址、埠及設定方法)
阿新 • • 發佈:2019-12-17
java傳送郵件基礎方法,可通過過載簡化引數
1 import java.io.File; 2 import java.io.UnsupportedEncodingException; 3 import java.util.Properties; 4 5 import javax.activation.DataHandler; 6 import javax.activation.FileDataSource; 7 import javax.mail.Authenticator; 8 import javax.mail.BodyPart; 9 import javax.mail.Message.RecipientType; 10 import javax.mail.MessagingException; 11 import javax.mail.Multipart; 12 import javax.mail.PasswordAuthentication; 13 import javax.mail.Session; 14 import javax.mail.Transport; 15 import javax.mail.internet.InternetAddress; 16 import javax.mail.internet.MimeBodyPart; 17 import javax.mail.internet.MimeMessage; 18 import javax.mail.internet.MimeMultipart; 19 import javax.mail.internet.MimeUtility; 20 21 public class MailUtil { 22 23 /** 24 * @Description: 使用QQ郵箱傳送不帶附件的郵件,收件人郵箱型別不限。 25 * @date: 2019年12月17日 下午4:51:01 26 * @author: ggwudivs 27 * @param subject 郵件標題 28 * @param content 郵件內容 29 * @param fromUser 發件人郵箱 30 * @param fromPass 發件人郵箱密碼,應為16位SMTP口令 31 * @param tO_Recipients 收件人郵箱,多個收件人用","分隔 32 * @param openSSL 是否開啟SSL 33 * @throws UnsupportedEncodingException 34 * @throws MessagingException: 35 * @return: void 36 */ 37 public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, boolean openSSL) throws UnsupportedEncodingException, MessagingException{ 38 sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", null, openSSL); 39 } 40 41 /** 42 * @Description: 使用QQ郵箱傳送帶附件的郵件,收件人郵箱型別不限。 43 * @date: 2019年12月17日 下午5:25:14 44 * @author: ggwudivs 45 * @param subject 郵件標題 46 * @param content 郵件內容 47 * @param fromUser 發件人郵箱 48 * @param fromPass 發件人郵箱密碼,應為16位SMTP口令 49 * @param tO_Recipients 收件人郵箱,多個收件人用","分隔 50 * @param attachmentFilesPath 郵件附件路徑,多個附件用","分隔 51 * @param openSSL 是否開啟SSL 52 * @throws UnsupportedEncodingException 53 * @throws MessagingException: 54 * @return: void 55 */ 56 public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, String attachmentFilesPath, boolean openSSL) throws UnsupportedEncodingException, MessagingException{ 57 sendMessage(subject, content, fromUser, fromPass, null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", attachmentFilesPath, openSSL); 58 } 59 60 /** 61 * @Description: smtp傳送郵件 62 * @date: 2019年12月17日 下午3:22:35 63 * @author: ggwudivs 64 * @param subject 郵件標題 65 * @param content 郵件文字內容 66 * @param fromUser 發件人郵箱 67 * @param fromPass 發件人郵箱密碼,QQ郵箱應為16位SMTP口令 68 * @param nickname 發件人暱稱 69 * @param tO_Recipients 收件人郵箱,多個收件人用","分隔 70 * @param cC_Recipients 抄送人郵箱,多個抄送人用","分隔 71 * @param bCC_Recipients 密送人郵箱,多個密送人用","分隔 72 * @param smtpHost smtp伺服器地址 73 * @param smtpPort smtp伺服器埠 74 * @param attachmentFilesPath 郵件附件路徑,多個附件用","分隔 75 * @throws MessagingException 76 * @throws UnsupportedEncodingException: 77 * @return: void 78 */ 79 public static void sendMessage(String subject, String content, String fromUser, String fromPass, String nickname, String tO_Recipients, String cC_Recipients, String bCC_Recipients, String smtpHost, String smtpPort, String attachmentFilesPath, boolean openSSL) throws MessagingException, UnsupportedEncodingException { 80 //建立Properties類,用於記錄郵箱的一些屬性 81 Properties props = new Properties(); 82 //表示SMTP傳送郵件,必須進行身份驗證 83 props.put("mail.smtp.auth", "true"); 84 //SMTP伺服器地址 85 props.put("mail.smtp.host", smtpHost); 86 //是否開啟SSL 87 if(openSSL){ 88 //SMTP伺服器埠號 89 props.put("mail.smtp.port", smtpPort); 90 }else{ 91 //SMTP伺服器ssl埠號 92 props.setProperty("mail.smtp.socketFactory.port", smtpPort); 93 props.setProperty("mail.smtp.socketFactory.fallback", "false"); 94 props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 95 } 96 97 //構建授權資訊,用於進行SMTP進行身份驗證 98 Authenticator authenticator = new Authenticator() { 99 protected PasswordAuthentication getPasswordAuthentication() { 100 //發件人賬號,發件人密碼(QQ郵箱應為16位SMTP口令) 101 return new PasswordAuthentication(fromUser, fromPass); 102 } 103 }; 104 105 //使用環境屬性和授權資訊,建立郵件會話 106 Session mailSession = Session.getInstance(props, authenticator); 107 108 //建立郵件訊息 109 MimeMessage message = new MimeMessage(mailSession); 110 111 //設定發件人,有暱稱時同時設定暱稱 112 try { 113 message.setFrom((nickname==null||"".equals(nickname))?new InternetAddress(fromUser):new InternetAddress(fromUser, nickname, "UTF-8")); 114 } catch (UnsupportedEncodingException e) { 115 e.printStackTrace(); 116 } 117 118 //設定一個或多個收件人 119 message.setRecipients(RecipientType.TO, tO_Recipients); 120 121 //設定一個或多個抄送人 122 message.setRecipients(RecipientType.CC, cC_Recipients); 123 124 //設定一個或多個密送人 125 message.setRecipients(RecipientType.BCC, bCC_Recipients); 126 127 //設定郵件標題 128 message.setSubject(subject); 129 130 //設定郵件的內容 131 if(attachmentFilesPath == null || "".equals(attachmentFilesPath)){ 132 //設定郵件的正文文字 133 message.setContent(content, "text/html;charset=UTF-8"); 134 }else{ 135 //向multipart物件中新增郵件的各個部分內容,包括文字內容和附件 136 Multipart multipart = new MimeMultipart(); 137 138 //新增郵件文字內容 139 BodyPart contentBodyPart = new MimeBodyPart(); 140 contentBodyPart.setContent(content, "text/html;charset=utf-8"); 141 multipart.addBodyPart(contentBodyPart); 142 143 //新增郵件附件內容 144 BodyPart attachmentBodyPart = new MimeBodyPart(); 145 String[] attachmentFiles = attachmentFilesPath.split(","); 146 for (String attachmentFile : attachmentFiles) { 147 if (attachmentFile != null && !"".equals(attachmentFile)) { 148 attachmentBodyPart = new MimeBodyPart(); 149 150 //根據附件路徑獲取檔案, 151 FileDataSource dataSource = new FileDataSource(new File(attachmentFile)); 152 attachmentBodyPart.setDataHandler(new DataHandler(dataSource)); 153 154 //MimeUtility.encodeWord可以避免檔名亂碼 155 String strFileName=dataSource.getFile().getName(); 156 attachmentBodyPart.setFileName(MimeUtility.encodeText(strFileName)); 157 158 multipart.addBodyPart(attachmentBodyPart); 159 } 160 } 161 162 //設定郵件的正文內容 163 message.setContent(multipart); 164 } 165 166 //傳送郵件 167 Transport.send(message); 168 } 169 }
郵箱型別 | SMTP伺服器地址 | 普通埠 | SSL埠 | 伺服器配置參考地址 |
QQ郵箱 | smtp.qq.com | 587 | 465 | https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=167 |
阿里企業郵箱 | smtp.qiye.aliyun.com | 25 | 465 | http://mailhelp.mxhichina.com/smartmail/detail.vm?knoId=5871700 |
網易163免費郵箱 | smtp.163.com | 25 | 465/994 | https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac22dc0e9af8168582a |
網易企業郵箱 | smtp.qiye.163.com | 25 | 994 | https://qiye.163.com/help/client-profile.html |
網易免費企業郵箱 | smtp.ym.163.com | 25 | 994 | http://app.ym.163.com/ym/help/help.html |
tips:
QQ郵箱需先設定獨立密碼才能使用smtp功能(https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001220&&id=28)。
QQ郵箱設定方法:設定--賬戶--賬戶安全--獨立密碼。
網易163免費郵箱同QQ郵箱一樣,需先設定授權碼才能使用smtp功能(https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac24a2130dd2fad05b1)
網易163免費郵箱開啟授權碼方法:https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2cda80145a1742516
網易企業郵箱在開啟客戶端授權密碼的功能時才需要設定客戶端授權碼:https://qiye.163.com/help/3f85a9.html
網易企業郵箱開啟授權碼方法:https://qiye.163.com/help/af988e.