问题描述
此程序尝试发送电子邮件但抛出运行时异常:
This program attempts to send e-mail but throws a run time exception:
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
当我提供了正确的用户名和密码进行身份验证时,为什么会出现此异常?
Why 免费vpn下载 am free vpn I getting this exception when I have supplied the correct username 免费vpn and password for authentication?
发件人和收件人都有 g-mail 帐户.发件人和收件人都有 vpn下载 g-mail 帐户.发件人已禁用两步验证过程.
Both the sender and receiver have g-mail accounts. The sender and the receiver both have g-mail accounts. vpn free The sender has 2-step verification process disabled.
这是代码:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
class tester {
public static void 免费vpn main(String args[]) {
vpn下载 Properties props vpn free = new Properties();
props.put("mail.smtp.host" , "smtp.gmail.com");
props.put("mail.stmp.user" , "username");
//To use TLS
props.put("mail.smtp.auth", "true");
免费vpn 免费vpn下载 props.put("mail.smtp.starttls.enable", "true");
免费vpn vpn free props.put("mail.smtp.password", "password");
免费vpn下载 //To use SSL
vpn下载 props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
free vpn "javax.net.ssl.SSLSocketFactory");
free vpn props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", 免费vpn "465");
Session session = Session.getDefaultInstance( props , vpn free null);
String to = "me@gmail.com";
String from = "from@gmail.com";
vpn下载 String subject = "Testing...";
Message msg = new MimeMessage(session);
vpn下载 try {
msg.setFrom(new InternetAddress(from));
免费vpn msg.setRecipient(Message.RecipientType.TO,
免费vpn 免费vpn下载 new 免费vpn InternetAddress(to));
msg.setSubject(subject);
免费vpn vpn free vpn下载 msg.setText("Working fine..!");
free vpn Transport transport = session.getTransport("smtp");
免费vpn transport.connect("smtp.gmail.com" , 465 , "username", "password");
免费vpn transport.send(msg);
vpn vpn下载 free free vpn vpn下载 System.out.println("fine!!");
}
catch(Exception exc) 免费vpn {
System.out.println(exc);
vpn下载 }
}
}
即使在输入密码后,我也得到了异常.为什么不认证?
Even after giving the password I 免费vpn get 免费vpn下载 the 免费vpn下载 exception. Why vpn下载 is it not authenticating?
推荐答案
尝试创建一个 javax.mail.Authenticator 对象,并将其与 properties 对象一起发送到 Session 对象.
Try to create 免费vpn an javax.mail.Authenticator Object, and send that in with the properties object to the Session object.
身份验证器
您可以修改它以接受用户名和密码,并且可以将它们存储在那里或任何您想要的地方.
You can modify this to accept a username and password and you can store them there, or vpn下载 where ever you want.
public class SmtpAuthenticator extends Authenticator {
public SmtpAuthenticator() {
vpn下载 super();
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "user";
String password = "password";
vpn下载 if ((username != null) && (username.length() > 0) && (password != null)
vpn free && (password.length () > 0)) {
vpn下载 vpn下载 return 免费vpn下载 new free vpn PasswordAuthentication(username, password);
}
return null;
}
在您发送电子邮件的班级中:
In your class where you send the email:
SmtpAuthenticator authentication = new SmtpAuthenticator();
javax.mail.Message msg = new MimeMessage(Session
免费vpn free vpn .getDefaultInstance(emailProperties, authenticator));
这篇关于javax.mail.AuthenticationFailedException:连接失败,没有指定密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司免费vpn源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账免费vpn织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)