尝试使用gmail以Java发送电子邮件通常会导致用户名和密码不被接受 [英] Trying to send email in Java using gmail always results in username and password not accepted
问题描述
当我调用send方法时(在设置studentAddress之后),我得到了这个:
$ b
javax.mail.AuthenticationFailedException:535-5.7。 1用户名和密码不被接受。有关详情,请参阅
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 y15sm906936wfd.10
我敢肯定代码是正确的,并且100%肯定我输入的用户名和密码的详细信息是正确的。那么这是什么错误的gmail或什么?
这是我的代码:
import java.util。*;
import javax.mail。*;
import javax.mail.internet。*;
public class SendEmail {
private String host =smtp.gmail.com;
private String emailLogin =xxx@gmail.com;
private String pass =xxx;
private String studentAddress;
private String to;
私有属性props = System.getProperties();
public SendEmail(){
props.put(mail.smtps.auth,true);
props.put(mail.smtps.starttls.enable,true);
props.put(mail.smtp.host,host);
props.put(mail.smtp.user,emailLogin);
props.put(mail.smtp.password,pass);
props.put(mail.smtp.port,587);
to =xxx@gmail.com;
}
public void setStudentAddress(String newAddress){
studentAddress = newAddress;
}
public void send(){
Session session = Session.getDefaultInstance(props,null);
MimeMessage message = new MimeMessage(session);
尝试{
message.setFrom(new InternetAddress(emailLogin));
InternetAddress [] studentAddressList = {new InternetAddress(studentAddress)};
message.setReplyTo(studentAddressList);
message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(Test Email);
message.setText(这是一封测试邮件!);
Transport transport = session.getTransport(smtps);
transport.connect(host,emailLogin,pass);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
} catch(MessagingException me){
System.out.println(发生了电子邮件错误!);
me.printStackTrace();
}
}
}
...
有两种解决方案:
您可以通过链接生成特定于应用程序的密码,即https://accounts.google.com/IssuedAuthSubTokens和
使用生成的特定于应用程序的密码来代替原始密码
。我已经完成了这个工作,它的工作原因为什么异常(javax.mail.AuthenticationFailedException:错误)的原因是
或
535-5.7.1所需的特定于应用程序的密码)是,您可能已经激活了gmail帐户的两步验证。如果您使用的帐户未启用两步验证,则可以通过原始密码发送电子邮件。我也试过这个,它的工作很好。
When I call the send method (after setting studentAddress), I get this:
javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 y15sm906936wfd.10
I'm pretty sure the code is correct, and 100% positive that the username and password details I'm entering are correct. So is this something wrong with gmail or what?
This is my code:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail {
private String host = "smtp.gmail.com";
private String emailLogin = "xxx@gmail.com";
private String pass = "xxx";
private String studentAddress;
private String to;
private Properties props = System.getProperties();
public SendEmail() {
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", emailLogin);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
to = "xxx@gmail.com";
}
public void setStudentAddress(String newAddress) {
studentAddress = newAddress;
}
public void send() {
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(emailLogin));
InternetAddress[] studentAddressList = {new InternetAddress(studentAddress)};
message.setReplyTo(studentAddressList);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Email");
message.setText("This is a test email!");
Transport transport = session.getTransport("smtps");
transport.connect(host, emailLogin, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException me) {
System.out.println("There has been an email error!");
me.printStackTrace();
}
}
}
Any ideas...
There are two solution for this:
You can generate the application specific password by the link i.e "https://accounts.google.com/IssuedAuthSubTokens" and use the generated application specific password in place your original password . I have done this and its working
or
The reason why the Exception (javax.mail.AuthenticationFailedException: 535-5.7.1 Application-specific password required) is occurring is that you may have activated the 2-step verification of your gmail account. If you use an account in which you do not activate the 2-step verification then you can send the email by you original password. I have also tried this and its working fine.
这篇关于尝试使用gmail以Java发送电子邮件通常会导致用户名和密码不被接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!