近來想寫一個qq之間互相發送郵件的工具.奈何一直報錯服務錯誤:java
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.qq.com:25 at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1401) at org.apache.commons.mail.Email.send(Email.java:1428) at email.MailUtil.send(MailUtil.java:31) at email.MailUtil.main(MailUtil.java:52) Caused by: javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
查閱網友們的答案說是把smtp.qq.com換成smtp.exmail.qq.com也是不得而入.最終只能退而求其次.用163郵箱發送到指定的qq郵箱.騰訊的服務驗證與其餘郵箱不一樣.雖然qq郵箱不行,可是這邊的163郵箱換成sina什麼的也仍是能夠的.只要換掉服務器便可.固然還得開啓smap服務,這就不用說了.apache
Mail.java,裏面配置一些必要的參數:服務器
package email; import java.io.Serializable; @SuppressWarnings("serial") public class Mail implements Serializable { 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; } }
MailUtil.java:函數
package email; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.log4j.Logger; public class MailUtil { protected final Logger logger = Logger.getLogger(getClass()); public boolean send(Mail mail) { // 發送email HtmlEmail email = new HtmlEmail(); try { // 這裏是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; } } public static void main(String[] args) { Mail mail = new Mail(); mail.setHost("smtp.163.com"); // 設置郵件服務器,若是不用163的,本身找找看相關的 mail.setSender("***@163.com"); mail.setReceiver("***@qq.com"); // 接收人 mail.setUsername("***@163.com"); // 登陸帳號,通常都是和郵箱名同樣吧 mail.setPassword("******"); // 發件人郵箱的登陸密碼 mail.setSubject("aaaaaaaaa"); mail.setMessage("收到請回復"); new MailUtil().send(mail); } }
在main函數中填寫相應參數就能夠發送了.工具