我正在尝试从我的应用程序发送电子邮件,但是一次又一次地获取身份验证失败错误。我已经尝试了给定的解决方案,但它们没有工作,因此我想把我的代码。请查看下面的代码并告诉我是否缺少重要的内容。
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 = "toUser@gmail.com";
String from = "abcd@gmail.com";
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)
答案 0 :(得分:2)
这是我解决错误的方法。请找到下面的代码,刚才我发送了一封测试电子邮件。
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 ="pan54321@gmail.com";
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("pan54321@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("vi@gmail.com"));
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);
}
}
}
我只使用了两个罐子:
和我的JDK版本是JDK6.0。