0
我使用java郵件從服務器發送郵件。我使用下面的代碼,但我得到這個錯誤..顯示授權失敗的Java郵件
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException
at mail.mailSSL.main(mailSSL.java:53)
Caused by: 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 mail.mailSSL.main(mailSSL.java:48)
我的代碼:
public class mailSSL {
public static void main(String args[]){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
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,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","[email protected]");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("SFTS MAIL TESING");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我怎樣才能解決這個問題?
你可能想要在上面的帖子中用「*」來編輯和替換你的密碼。您是否嘗試過簡單而簡單的mail.smtp.user或mail.smtp.password? – aishwarya