Java Mail郵件發送如何實現簡單封裝
首先每次發送需要配置的東西很多,包括發件人的郵箱和密碼、smtp服務器和SMTP端口號等信息。其次,沒有將發送和郵件內容相分離。按照單一職責原則,應該有且僅有一個原因引起類的變更[1]。最后一個問題是,我們的代碼不僅自己用,也很可能讓別人調用。別人調用的時候不想去了解郵件發送的細節,調用的人只想傳盡量少的參數獲得預期的效果。因此讓Demo變成可以使用的代碼需要我們重新設計代碼的結構。
從Demo中我們可以抽象出兩種類型的POJO,也就是發件人和郵件。你可能會問收件人怎么辦?收件人可以跟郵件POJO放在一起嗎?
仔細思考下我們就知道,郵件和收件人應該是分開的。因為如果郵件和收件人放在一起,那么就意味著我的一封郵件只能發送給特定的人了,而實際上我們會把相同的郵件發送給不同的收件人。因此收件人只要作為發送時的參數就可以了。
1.發件人POJO
/** * @Title: MailAuthenticator * @author: ykgao * @description: * @date: 2017-10-11 下午04:55:37 */import javax.mail.Authenticator;import javax.mail.PasswordAuthentication; /** * 服務器郵箱登錄驗證 * * @author MZULE * */public class MailAuthenticator extends Authenticator { /** * 用戶名(登錄郵箱) */ private String username; /** * 密碼 */ private String password; /** * 初始化郵箱和密碼 * * @param username 郵箱 * @param password 密碼 */ public MailAuthenticator(String username, String password) { this.username = username; this.password = password; } String getPassword() { return password; } @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } String getUsername() { return username; } public void setPassword(String password) { this.password = password; } public void setUsername(String username) { this.username = username; } }
2.郵件POJO
用于存儲郵件主題和內容。
/** * @Title: SimpleMail * @author: ykgao * @description: * @date: 2017-10-11 下午04:56:27 */public class SimpleMail {/** 郵件主題 */public String Subject;/** 郵件內容 */public String Content;/** * @return the subject */public String getSubject() {return Subject;}/** * @param subject * the subject to set */public void setSubject(String subject) {Subject = subject;}/** * @return the content */public String getContent() {return Content;}/** * @param content * the content to set */public void setContent(String content) {Content = content;}}
3.郵件發送
設計好了POJO,我們現在需要當然是發送郵件了。在Demo中我們需要配置SMTP服務器,但是我們使用郵箱發送郵件的時候并不需要填寫SMTP服務器。其實SMTP服務器大多數的格式是:smtp.emailType.com。此處emailType 就是你的郵箱類型也就是@后面跟的名稱。比如163郵箱就是163。不過這個方法也不是萬能的,因為outlook郵箱的smtp服務器就不是這個格式,而是smtp-mail.outlook.com ,所以我單獨為outlook郵箱寫了個例外。
我們還需要群分郵件的功能。這個設計起來很容易,只需要一個單人發送的重載方法,其收件人的參數可以是一個List。為了減少接口的參數個數,我們把SMTP端口默認為587。
import java.io.UnsupportedEncodingException;import java.security.GeneralSecurityException;import java.util.List;import java.util.Properties;import javaMailDevelopment.SimpleMail;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMessage.RecipientType;import com.sun.mail.util.MailSSLSocketFactory;/** * @Title: SimpleMailSender * @author: ykgao * @description: 郵件發送器 * @date: 2017-10-11 下午04:54:50 */public class SimpleMailSender {/** * 發送郵件的props文件 */private final transient Properties props = System.getProperties();/** * 郵件服務器登錄驗證 */private transient MailAuthenticator authenticator;/** * 郵箱session */private transient Session session;/** * 初始化郵件發送器 * * @param smtpHostName * SMTP郵件服務器地址 * @param username * 發送郵件的用戶名(地址) * @param password * 發送郵件的密碼 */public SimpleMailSender(final String smtpHostName, final String username, final String password) {init(username, password, smtpHostName);}/** * 初始化郵件發送器 * * @param username * 發送郵件的用戶名(地址),并以此解析SMTP服務器地址 * @param password * 發送郵件的密碼 */public SimpleMailSender(final String username, final String password) {// 通過郵箱地址解析出smtp服務器,對大多數郵箱都管用String smtpHostName = 'smtp.' + username.split('@')[1];if (username.split('@')[1].equals('outlook.com')) {smtpHostName = 'smtp-mail.outlook.com';}init(username, password, smtpHostName);}/** * 初始化 * * @param username * 發送郵件的用戶名(地址) * @param password * 密碼 * @param smtpHostName * SMTP主機地址 */private void init(String username, String password, String smtpHostName) {// 初始化propsprops.setProperty('mail.transport.protocol', 'smtp'); // 使用的協議(JavaMail規范要求)props.setProperty('mail.smtp.host', smtpHostName); // 發件人的郵箱的 SMTP 服務器地址props.setProperty('mail.smtp.auth', 'true'); // 需要請求認證final String smtpPort = '587';props.setProperty('mail.smtp.port', smtpPort);// props.setProperty('mail.smtp.socketFactory.class',// 'javax.net.ssl.SSLSocketFactory');props.setProperty('mail.smtp.socketFactory.fallback', 'false');props.setProperty('mail.smtp.starttls.enable', 'true');props.setProperty('mail.smtp.socketFactory.port', smtpPort);// 驗證authenticator = new MailAuthenticator(username, password);// 創建sessionsession = Session.getInstance(props, authenticator);session.setDebug(true);}/** * 發送郵件 * * @param recipient * 收件人郵箱地址 * @param subject * 郵件主題 * @param content * 郵件內容 * @throws AddressException * @throws MessagingException * @throws UnsupportedEncodingException */public void send(String recipient, String subject, Object content) throws Exception {// 創建mime類型郵件final MimeMessage message = new MimeMessage(session);// 設置發信人message.setFrom(new InternetAddress(authenticator.getUsername()));// 設置收件人message.setRecipient(RecipientType.TO, new InternetAddress(recipient));// 設置主題message.setSubject(subject);// 設置郵件內容message.setContent(content.toString(), 'text/html;charset=utf-8');// 發送Transport.send(message);}/** * 群發郵件 * * @param recipients * 收件人們 * @param subject * 主題 * @param content * 內容 * @throws AddressException * @throws MessagingException */public void send(List<String> recipients, String subject, Object content)throws AddressException, MessagingException {// 創建mime類型郵件final MimeMessage message = new MimeMessage(session);// 設置發信人message.setFrom(new InternetAddress(authenticator.getUsername()));// 設置收件人們final int num = recipients.size();InternetAddress[] addresses = new InternetAddress[num];for (int i = 0; i < num; i++) {addresses[i] = new InternetAddress(recipients.get(i));}message.setRecipients(RecipientType.TO, addresses);// 設置主題message.setSubject(subject);// 設置郵件內容message.setContent(content.toString(), 'text/html;charset=utf-8');// 發送Transport.send(message);}/** * 發送郵件 * * @param recipient * 收件人郵箱地址 @param mail 郵件對象 @throws AddressException @throws * MessagingException @throws */public void send(String recipient, SimpleMail mail) throws Exception {send(recipient, mail.getSubject(), mail.getContent());}/** * 群發郵件 * * @param recipients * 收件人們 * @param mail * 郵件對象 * @throws AddressException * @throws MessagingException */public void send(List<String> recipients, SimpleMail mail) throws AddressException, MessagingException {send(recipients, mail.getSubject(), mail.getContent());}}
4.測試代碼
代碼寫完了,現在需要測試下代碼是否可行。
import java.util.ArrayList;import java.util.List;/** * @Title: testMail * @author: ykgao * @description: * @date: 2017-10-11 下午02:13:02 * */public class testMail {public static void main(String[] args) throws Exception { /** 創建一個郵件發送者*/SimpleMailSender simpleMailSeJava Mail 郵件發送簡單封裝
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
