最近使用Java基於JavaMail實現向QQ郵箱發送郵件總是發送失敗,在網上搜了下,終於找到緣由,記錄下。java
參考:http://www.jb51.net/article/78405.htmapache
網上還有種說法,把 smtp.qq.com 換成 smtp.exmail.qq.com也不須要 SSL加密,可是沒有run成功。因此仍是老老實實加上 SSL 加密吧。服務器
下面是代碼:工具
Mail類:測試
package com.xjz.mail; import java.io.Serializable; public class Mail implements Serializable { /** * */ private static final long serialVersionUID = 1L; public static final String ENCODEING = "UTF-8"; private String host; // 服務器地址 private String sender; // 發件人的郵箱 private String receiver; // 收件人的郵箱 private String name; // 發件人暱稱 private String username; // 帳號 private String password; // 密碼 private String subject; // 主題 private String message; // 信息(支持HTML) public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
郵件工具類:this
package com.xjz.mail; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.slf4j.Logger; import org.slf4j.impl.Log4jLoggerFactory; /** * 郵件發送工具實現類 * @author Administrator * */ public class MailUtil { protected final Logger logger = new Log4jLoggerFactory().getLogger("loggerSlfj"); public boolean send(Mail mail) { // 發送email HtmlEmail email = new HtmlEmail(); try { email.setSSLOnConnect(true); // 這裏是SMTP發送服務器的名字:163的以下:"smtp.163.com" email.setHostName(mail.getHost()); // 字符編碼集的設置 email.setCharset(Mail.ENCODEING); // 收件人的郵箱 email.addTo(mail.getReceiver()); // 發送人的郵箱 email.setFrom(mail.getSender(), mail.getName()); // 若是須要認證信息的話,設置認證:用戶名-密碼。分別爲發件人在郵件服務器上的註冊名稱和密碼 email.setAuthentication(mail.getUsername(), mail.getPassword()); // 要發送的郵件主題 email.setSubject(mail.getSubject()); // 要發送的信息,因爲使用了HtmlEmail,能夠在郵件內容中使用HTML標籤 email.setMsg(mail.getMessage()); // 發送 email.send(); if (logger.isDebugEnabled()) { logger.debug(mail.getSender() + " 發送郵件到 " + mail.getReceiver()); } return true; } catch (EmailException e) { e.printStackTrace(); logger.info(mail.getSender() + " 發送郵件到 " + mail.getReceiver() + " 失敗"); return false; } } }
測試代碼:編碼
package com.xjz.mail; import org.junit.Test; public class TestMail { @Test public void testMail() { Mail mail = new Mail(); mail.setHost("smtp.qq.com"); // 設置郵件服務器,若是不用163的,本身找找看相關的 mail.setSender("xxxxxx@qq.com"); // 發送人 mail.setReceiver("xxxxxx@qq.com"); // 接收人 mail.setUsername("xxxxxx@qq.com"); // 登陸帳號,通常都是和郵箱名同樣吧 mail.setPassword("xxxxxx"); // 受權碼 mail.setSubject("test"); mail.setMessage("test"); new MailUtil().send(mail); } }