通过gmail发送电子邮件时身份验证失败
问题描述:
我试图从我的应用程序发送电子邮件,但一次又一次地获得身份验证失败错误。我已经尝试了给定的解决方案,但他们不工作,因此我想把我的代码。请看下面的代码,并告诉我是否缺少重要的东西。通过gmail发送电子邮件时身份验证失败
package managimg.stud.data;
import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import sendmail.Sendmail;
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 SendMail {
public static void main(String...a){
SendMail s = new SendMail();
}
public SendMail(){
Authenticator authenticate = new Authenticator(){
public PasswordAuthentication getPasswrodAuthentication(){
return new PasswordAuthentication("abcd","xyz");
}
};
String to = "[email protected]";
String from = "[email protected]";
String host = "smtp.gmail.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.starttls.enable", "true") ;
properties.setProperty("mail.smtp.auth", "true") ;
properties.setProperty("mail.smtp.user", "abcd");
properties.setProperty("mail.smtp.password", "xyz");
properties.setProperty("mail.smtp.debug", "true");
Session session = Session.getDefaultInstance(properties,authenticate);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
有人能告诉我这里有什么问题吗?我错过了什么?我一次又一次得到下面的错误。我已经尝试了很多解决方案,但它不起作用。任何人都可以帮助我吗?
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at managimg.stud.data.SendMail.<init>(SendMail.java:139)
at managimg.stud.data.SendMail.main(SendMail.java:31)
答
这就是我如何解决我的错误。请找到下面的代码,刚才我发送了一封测试邮件。
package gmail.email;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import javax.mail.PasswordAuthentication;
public class GmailEmail {
final String userName ="[email protected]";
final String password="tqw12";
public static void main(String[] args) {
new GmailEmail();
}
public GmailEmail(){
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.starttls.enable", "true") ;
properties.put("mail.smtp.auth", "true") ;
Session session = Session.getInstance(properties,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName, password);
}
});
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("my First Email");
message.setContent("<h:body>You wrote first email</body>","text/html; charset=utf-8");
Transport.send(message);
}catch(MessagingException
messageException){
throw new RuntimeException(messageException);
}
}
}
我只用两个jar:
- 的mail.jar
- 的JavaEE-API-6.0.jar
,我的JDK版本是JDK6.0。
Google昨天通过gmail smtp服务器更改了发送带有远程发件人地址的电子邮件的权限评估。您可以在互联网上找到有关信息以及备选方案和解决方法。 – arkascha 2014-08-31 14:44:53
但我能ping通主机。现在关于权限可以请你提供任何我可以找到替代品的链接。 – 2014-08-31 14:59:41
我已经尝试了几乎所有在堆栈溢出中给出的解决方案来连接gmail或任何其他邮件服务器,但没有解决方案正在工作。每当我得到相同的错误。任何人都可以提供一些可行和正确的解决方案我也尝试过简单地复制和粘贴来自各个网站的互联网上提供的代码,但它们不能很好地工作。 – 2014-08-31 15:09:02