javax.mail.AuthenticationFailedException:连接失败,未指定密码?
此程序尝试发送电子邮件,但引发运行时异常:javax.mail.AuthenticationFailedException:连接失败,未指定密码?,java,smtp,gmail,jakarta-mail,Java,Smtp,Gmail,Jakarta Mail,此程序尝试发送电子邮件,但引发运行时异常: javax.mail.AuthenticationFailedException: failed to connect, no password specified? 当我提供了正确的用户名和密码进行身份验证时,为什么会出现此异常 发件人和收件人都有g-mail帐户。发件人和收件人都有g-mail帐户。发送方禁用了两步验证过程 代码如下: import javax.mail.*; import javax.mail.internet.*; impor
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
当我提供了正确的用户名和密码进行身份验证时,为什么会出现此异常
发件人和收件人都有g-mail帐户。发件人和收件人都有g-mail帐户。发送方禁用了两步验证过程
代码如下:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
class tester {
public static void main(String args[]) {
Properties props = 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");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "password");
//To use SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance( props , null);
String to = "me@gmail.com";
String from = "from@gmail.com";
String subject = "Testing...";
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Working fine..!");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com" , 465 , "username", "password");
transport.send(msg);
System.out.println("fine!!");
}
catch(Exception exc) {
System.out.println(exc);
}
}
}
即使在给出密码后,我也会得到异常。为什么不进行身份验证?尝试创建javax.mail.Authenticator对象,并将其与properties对象一起发送到会话对象 编辑: 您可以修改它以接受用户名和密码,并且可以将它们存储在那里,或者存储在您想要的任何位置
public class SmtpAuthenticator extends Authenticator {
public SmtpAuthenticator() {
super();
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
String username = "user";
String password = "password";
if ((username != null) && (username.length() > 0) && (password != null)
&& (password.length () > 0)) {
return new PasswordAuthentication(username, password);
}
return null;
}
在您发送电子邮件的班级中:
SmtpAuthenticator authentication = new SmtpAuthenticator();
javax.mail.Message msg = new MimeMessage(Session
.getDefaultInstance(emailProperties, authenticator));
可能值得验证的是,由于多次登录尝试失败,gmail帐户没有被锁定,您可能需要重置密码。我和你有同样的问题,这就是解决办法。除了RMT的答案之外。我还不得不修改一下代码
public boolean send(String to, String from, String subject, String text) {
return send(new String[] {to}, from, subject, text);
}
public boolean send(String[] to, String from, String subject, String text) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", config.host);
props.put("mail.smtp.user", config.username);
props.put("mail.smtp.port", config.port);
props.put("mail.smtp.password", config.password);
Session session = Session.getInstance(props, new SmtpAuthenticator(config));
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);
message.setSubject(subject);
message.setText(text);
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
return true;
}
公共布尔发送(字符串收件人、字符串发件人、字符串主题、字符串文本){
返回send(新字符串[]{to},from,subject,text);
}
公共布尔发送(字符串[]到,字符串从,字符串主题,字符串文本){
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
put(“mail.smtp.host”,config.host);
put(“mail.smtp.user”,config.username);
put(“mail.smtp.port”,config.port);
put(“mail.smtp.password”,config.password);
Session Session=Session.getInstance(props,新的SmtpAuthenticator(config));
试一试{
Message Message=新的mimessage(会话);
message.setFrom(新的InternetAddress(from));
InternetAddress[]addressTo=新的InternetAddress[to.length];
for(int i=0;i
您需要将对象身份验证作为参数添加到会话中。比如
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"XXXX@gmail.com", "XXXXX");// Specify the Username and the PassWord
}
});
现在你不会得到这样的例外
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
您的电子邮件会话应提供一个验证器实例,如下所示
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"myemail@gmail.com", "password");
}
});
这里有一个完整的示例import java.util.Properties;
导入javax.mail.Authenticator;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.mimessage;
导入com.opensymphony.xwork2.ActionSupport;
导入com.opensymphony.xwork2.ModelDriven;
@抑制警告(“串行”)
公共类注册操作{
公共字符串execute(){
RegisterAction mailBean=新的RegisterAction();
String subject=“您的用户名和密码”;
字符串message=“Hi,”+用户名;
message+=“\n\n您的用户名是”+电子邮件;
message+=“\n\n您的密码是”+密码;
message+=“\n\n请使用您的用户名和密码登录网站。”;
消息+=“\n\n谢谢”;
消息+=“\n\n\n问候”;
//从你的邮件中得到
String[]recipients=新字符串[1];
收件人[0]=新字符串();
收件人[0]=customer.getEmail();
试一试{
sendMail(收件人、主题、消息);
返回“成功”;
}捕获(例外e){
System.out.println(“发送邮件时出错:+e”);
}
返回“失败”;
}
public void sendMail(字符串收件人[],字符串主题,字符串消息)
抛出消息异常
{
布尔调试=假;
//设置主机smtp地址
Properties props=新属性();
put(“mail.smtp.host”、“smtp.gmail.com”);
put(“mail.smtp.starttls.enable”,true);
put(“mail.smtp.auth”,true);
//创建一些属性并获取默认会话
Session Session=Session.getDefaultInstance(props,new Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码验证(
"username@gmail.com“,“5373273437543”);//指定用户名和密码
}
});
setDebug(debug);
//创建消息
Message msg=新的mimessage(会话);
InternetAddress[]addressTo=新的InternetAddress[recipients.length];
for(int i=0;iimport java.util.Properties;
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.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@SuppressWarnings("serial")
public class RegisterAction {
public String execute() {
RegisterAction mailBean = new RegisterAction();
String subject="Your username & password ";
String message="Hi," + username;
message+="\n \n Your username is " + email;
message+="\n \n Your password is " + password;
message+="\n \n Please login to the web site with your username and password.";
message+="\n \n Thanks";
message+="\n \n \n Regards";
//Getting FROM_MAIL
String[] recipients = new String[1];
recipients[0] = new String();
recipients[0] = customer.getEmail();
try{
mailBean.sendMail(recipients,subject,message);
return "success";
}catch(Exception e){
System.out.println("Error in sending mail:"+e);
}
return "failure";
}
public void sendMail( String recipients[ ], String subject, String message)
throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.auth", true);
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"username@gmail.com", "5373273437543");// Specify the Username and the PassWord
}
});
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
//msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
//send message
Transport.send(msg);
System.out.println("Message Sent Successfully");
}
}
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
final Properties props = new Properties();
props.put("mail.smtp.host", config.getSmtpHost());
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(config.getSmtpUser(), config.getSmtpPassword());
}
});
Transport.send(msg, "user", "password");
private void sendMail(){
try{
Properties prop = System.getProperties();
prop.put("mail.smtp.host", "yourHost");
Session session = Session.getInstance(prop);
Message msg = #createYourMsg(session, from, to, subject, mailer, yatta yatta...)#;
Transport.send(msg, "user", "password");
}catch(Exception exc) {
// Deal with it! :)
}
}
Transport transport = session.getTransport("smtp");
transport.connect("user","passw@rd");
Transport.send(msg, "user", "passw%rd");
...
Session session = Session.getInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("user", "pas$w@r|d");
}
});
Message message = new MimeMessage(session);
...
Transport.send(message);
mail.smtp.user
mail.stmp.user;
Session mailSession = Session.getInstance(props, new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"your_email", "your_password");// Specify the Username and the PassWord
}
});
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailSender {
public Properties mailProperties() {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.user", "your_email");
props.setProperty("mail.smtp.password", "your_password");
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtp.auth", "true");
return props;
}
public String sendMail(String from, String to, String subject, String msgBody) {
Properties props = mailProperties();
Session mailSession = Session.getInstance(props, new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"your_email", "your_password");// Specify the Username and the PassWord
}
});
mailSession.setDebug(false);
try {
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setFrom(new InternetAddress(from));
message.addRecipients(Message.RecipientType.TO, to);
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(msgBody, "text/html");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
return "SUCCESS";
} catch (NoSuchProviderException e) {
e.printStackTrace();
return "INVALID_EMAIL";
} catch (MessagingException e) {
e.printStackTrace();
}
return "ERROR";
}
public static void main(String args[]) {
System.out.println(new MailSender().sendMail("your_email/from_email", "to_email", "Subject", "Message"));
}
}