java代码如何发送QQ邮件
近来想写一个qq之间互相发送邮件的工具.奈何一直报错服务错误:
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服务,这就不用说了.
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函数中填写相应参数就可以发送了.