发送邮件及错误解决,javax.mail.AuthenticationFailedException: failed to connect, no password specified?

突然想弄个邮件发送的java程序,从网上找了个例子,但写完之后运行

javax.mail.AuthenticationFailedException: failed to connect, no password specified?

从网上又搜了下,最终在https://stackoverflow.com/questions/6610572/javax-mail-authenticationfailedexception-failed-to-connect-no-password-specifi地址中找到了答案。

让一个方法继承Authenticator,并重写protected PasswordAuthentication getPasswordAuthentication() {}方法。

创建Session时,Session.getInstance(properties, Authenticator);

下面粘出代码:

package com.mail;


import java.io.UnsupportedEncodingException;
import java.util.Date;
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.InternetAddress;
import javax.mail.internet.MimeMessage;


/**
 * 邮件创建步骤:


创建一个邮件对象(MimeMessage);
设置发件人,收件人,可选增加多个收件人,抄送人,密送人;
设置邮件的主题(标题);
设置邮件的正文(内容);
设置显示的发送时间;
保存到本地。
 */
public class Mail extends Authenticator{
public static String myEmailSMTPHost = "smtp.163.com";
private static String sendAccount = "***@163.com";
private static String pa = "*****";
private static String receiveAccount = "***@163.com";
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");//使用协议
props.setProperty("mail.smtp.host", myEmailSMTPHost);//发件人邮箱服务地址
props.setProperty("mail.smtp.auth", "true");//需要请求认证
props.setProperty("mail.smtp.port", "465");//ssl端口
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getInstance(props,new Mail());
session.setDebug(true);
MimeMessage mime = createMimeMessage(session, sendAccount, receiveAccount);
Transport transport = session.getTransport();//根据session获得邮件传输对象
transport.connect(sendAccount, pa);
transport.send(mime, mime.getAllRecipients());
transport.close();
}

private static MimeMessage createMimeMessage(Session session,String sendAccount,String receiveAccount) throws MessagingException, UnsupportedEncodingException{
MimeMessage mime = new MimeMessage(session);
mime.setFrom(sendAccount);
mime.setRecipient(RecipientType.TO, new InternetAddress(receiveAccount,"hello","UTF-8"));
mime.setSubject("hello","UTF-8");
mime.setContent("test mail", "text/html; charset=UTF-8");
mime.setSentDate(new Date());
mime.saveChanges();
return mime;
}

@Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = "***@163.com";
String pa = "*****";
if(username != null && username.length()>0 && pa != null && pa.length()>0){
return new PasswordAuthentication(username, pa);
}
return null;
    }
}

猜你喜欢

转载自blog.csdn.net/a1124544556/article/details/80571266