700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 解决qq邮箱发送邮件失败javax.mail.AuthenticationFailedException

解决qq邮箱发送邮件失败javax.mail.AuthenticationFailedException

时间:2018-05-10 19:14:59

相关推荐

解决qq邮箱发送邮件失败javax.mail.AuthenticationFailedException

问题原因:

QQ邮箱的SSL加密连接已经默认开启,SSL协议(Secure Socket Layer,安全套接层)是由网景(Netscape)公司推出的一种安全通信协议,是对计算机之间整个会话进行加密的协议。

解决办法:

注意JavaMail的版本,要包含com.sun.mail.util.MailSSLScoketFactory类,1.4.2和其以上的版本应该可以。

import java.io.UnsupportedEncodingException;import java.security.GeneralSecurityException;import java.util.Date;import java.util.Properties;import javax.mail.Address;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import com.sun.mail.util.MailSSLSocketFactory;public class EmailSendTool {// 邮箱服务器private String host;// 这个是你的邮箱用户名private String username;// 你的邮箱密码private String password;private String mail_head_name = "this is head of this mail";private String mail_head_value = "this is head of this mail";private String mail_to;private String mail_from;private String mail_subject = "this is the subject of this test mail";private String mail_body = "this is the mail_body of this test mail";private String personalName = "";public EmailSendTool() {}public EmailSendTool(String host, String username, String password,String mailto, String subject, String text, String name,String head_name, String head_value) {this.host = host;this.username = username;this.mail_from = username;this.password = password;this.mail_to = mailto;this.mail_subject = subject;this.mail_body = text;this.personalName = name;this.mail_head_name = head_name;this.mail_head_value = head_value;}/*** 此段代码用来发送普通电子邮件* * @throws MessagingException* @throws UnsupportedEncodingException* @throws GeneralSecurityException */public void send() throws MessagingException, UnsupportedEncodingException, GeneralSecurityException {Properties props = new Properties();Authenticator auth = new Email_Autherticator(); // 进行邮件服务器用户认证props.put("mail.smtp.host", host);props.put("mail.smtp.auth", "true");props.put("mail.transport.protocol", "smtp");MailSSLSocketFactory sf = new MailSSLSocketFactory();//ssl设置sf.setTrustAllHosts(true);props.put("mail.smtp.ssl.enable", "true");props.put("mail.smtp.ssl.socketFactory", sf);Session session = Session.getDefaultInstance(props, auth);// 设置session,和邮件服务器进行通讯。MimeMessage message = new MimeMessage(session);// message.setContent("foobar, "application/x-foobar"); // 设置邮件格式message.setContent(mail_body , "text/html;charset=utf-8");message.setSubject(mail_subject); // 设置邮件主题//message.setText(mail_body); // 设置邮件正文message.setHeader(mail_head_name, mail_head_value); // 设置邮件标题message.setSentDate(new Date()); // 设置邮件发送日期Address address = new InternetAddress(mail_from, personalName);message.setFrom(address); // 设置邮件发送者的地址Address toAddress = new InternetAddress(mail_to); // 设置邮件接收方的地址message.addRecipient(Message.RecipientType.TO, toAddress);Transport.send(message); // 发送邮件}/*** 用来进行服务器对用户的认证*/public class Email_Autherticator extends Authenticator {public Email_Autherticator() {super();}public Email_Autherticator(String user, String pwd) {super();username = user;password = pwd;}public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}}public String getHost() {return host;}public void setHost(String host) {this.host = host;}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 getMail_head_name() {return mail_head_name;}public void setMail_head_name(String mail_head_name) {this.mail_head_name = mail_head_name;}public String getMail_head_value() {return mail_head_value;}public void setMail_head_value(String mail_head_value) {this.mail_head_value = mail_head_value;}public String getMail_to() {return mail_to;}public void setMail_to(String mail_to) {this.mail_to = mail_to;}public String getMail_from() {return mail_from;}public void setMail_from(String mail_from) {this.mail_from = mail_from;}public String getMail_subject() {return mail_subject;}public void setMail_subject(String mail_subject) {this.mail_subject = mail_subject;}public String getMail_body() {return mail_body;}public void setMail_body(String mail_body) {this.mail_body = mail_body;}public String getPersonalName() {return personalName;}public void setPersonalName(String personalName) {this.personalName = personalName;}public static void main(String[] args) {EmailSendTool sendEmail = new EmailSendTool("","XXX@", "XXX", "XXX@","主题", "邮件内容", "发送人名称", "", "");System.out.println("main");try {sendEmail.send();} catch (Exception e) {e.printStackTrace();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。