今日研究了一下java發送郵件的程序,有兩種方法,一種是用java.mail;一種是用commons-email。
一、JAVA Mail是很常用的用于發送郵件的包,我們可以從這里獲取,或者在maven中添加如下配置:
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.5</version></dependency>示例代碼如下:package cn.mail.test;import java.util.PRoperties;import javax.mail.Authenticator;import javax.mail.Message.RecipientType;import javax.mail.MessagingException;import javax.mail.PassWordAuthentication;import javax.mail.session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class EmailTest { private static boolean send_QQmail(String strMail, String strTitle, String strText){ boolean bret = false; try { final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.qq.com"); //你自己的郵箱 props.put("mail.user", "xxxxxxx@qq.com"); //你開啟pop3/smtp時的驗證碼 props.put("mail.password", "xxxxxxxx"); props.put("mail.smtp.port", "25"); props.put("mail.smtp.starttls.enable", "true"); Authenticator authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用環境屬性和授權信息,創建郵件會話 Session mailSession = Session.getInstance(props, authenticator); // 創建郵件消息 MimeMessage message = new MimeMessage(mailSession); // 設置發件人 String username = props.getProperty("mail.user"); InternetAddress form = new InternetAddress(username); message.setFrom(form); InternetAddress to = new InternetAddress(strMail); message.setRecipient(RecipientType.TO, to); // 設置郵件標題 message.setSubject(strTitle); // 設置郵件的內容體 message.setContent(strText, "text/html;charset=UTF-8"); // 發送郵件 Transport.send(message); bret = true; } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } return bret; } private static boolean send_163mail(String strMail, String strTitle, String strText){ boolean bret = false; try { final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.163.com"); // 發件人的賬號 props.put("mail.user", "xxxxxxxx@163.com"); //發件人的密碼 props.put("mail.password", "xxxxxxx"); // 構建授權信息,用于進行SMTP進行身份驗證 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 用戶名、密碼 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用環境屬性和授權信息,創建郵件會話 Session mailSession = Session.getInstance(props, authenticator); // 創建郵件消息 MimeMessage message = new MimeMessage(mailSession); // 設置發件人 String username = props.getProperty("mail.user"); InternetAddress form = new InternetAddress(username); message.setFrom(form); // 設置收件人 InternetAddress to = new InternetAddress(strMail); message.setRecipient(RecipientType.TO, to); // 設置郵件標題 message.setSubject(strTitle); // 設置郵件的內容體 message.setContent(strText, "text/html;charset=UTF-8"); // 發送郵件 Transport.send(message); bret = true; } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } return bret; } public static void main(String[] args) { if (send_qqmail("xxxxxx@qq.com", "測試QQ郵箱發送", "<body><p>你們好嗎</p></body>")) System.out.println("QQ郵件發送成功"); if (send_163mail("xxxxxx@163.com", "測試網易郵箱發送", "<body><p>你們好嗎</p></body>")) System.out.println("網易郵件發送成功"); }}這里的時候,在用QQ郵箱時,發送郵件的郵箱記得開通pop3/smtp服務。二、使用JAVA Mail我們可以完成郵件的發送,但是我們可以發現實現過程略顯復雜,比較繁瑣,所以我們可以借助
commons-email
簡化發送郵件的過程,commons-email可以從這里下載,或在maven中添加如下配置:<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.4</version></dependency>示例代碼如下:package com.mail.test;import java.util.Date;import org.apache.commons.mail.SimpleEmail;public class CommonsEmailTest{ /** * 發送文本郵件 * * @throws Exception */ public static boolean sendTextMail(String strMail, String strTitle, String strText) throws Exception{ boolean bret = false; SimpleEmail mail = new SimpleEmail(); // 設置郵箱服務器信息 mail.setSslSmtpPort("25"); mail.setHostName("smtp.163.com"); // 設置密碼驗證器 mail.setAuthentication("XXXXXX@163.com", "XXXXXX(授權密碼)"); // 設置郵件發送者 mail.setFrom("XXXXXX@163.com"); // 設置郵件接收者 mail.addTo(strMail); // 設置郵件編碼 mail.setCharset("UTF-8"); // 設置郵件主題 mail.setSubject(strTitle); // 設置郵件內容 mail.setMsg(strText); // 設置郵件發送時間 mail.setSentDate(new Date()); // 發送郵件 mail.send(); return bret; } public static void main(String[] args) { try { if (sendTextMail("YYYYYY@qq.com", "測試QQ郵箱發送", "你們好嗎???")) System.out.println("QQ郵件發送成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}這里用common-email時,我選擇用163郵箱往QQ郵箱上發送郵件,發送成功,代碼量也比用mail是簡單多了注意事項:
在用java程序發送郵件時,大部分問題是郵箱沒有開通pop3/smtp,會報javax.mail.AuthenticationFailedException,遇到這個問題首先要先確定pop3/smtp服務是否開啟,詳情參考:http://blog.csdn.net/zouxucong/article/details/60578824
這里,多謝兩位大神的文章可供參看
參考文章一:http://blog.csdn.net/jianggujin/article/details/51253129
參考文章二:http://blog.csdn.net/zhang_ruiqiang/article/details/50754615
新聞熱點
疑難解答