0
我正在使用JAVAMail API發送郵件。但驗證失敗。JAVA郵件驗證失敗
這裏是樣本輸入
private String to = "[email protected]";
private String from = "[email protected]";
private String username = "juniad_rocku";
private String password = "myactualpassword";
private String subject = "My Subject";
private String body = "Please see the attached file";
和代碼是
private void emailFile(){
String host = "relay.jangosmtp.net";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(this.subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(this.body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
for(String filePath : UniversityForm.allAttachedFilesPath) {
if(filePath != null && !(filePath.equals("")))
this.addAttachment(multipart, filePath);
}
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
錯誤
java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535 5.7.8 Authentication credentials invalid
我已經給了很好的用戶名,密碼......我不知道驗證失敗的原因。我不知道host
。我錯在哪裏?請幫忙。
感謝。但它給了我錯誤 - >'無法連接到SMTP主機:smtp.mail.yahoo.com,端口:465,響應:-1' – Junaid 2014-12-07 12:26:20
沒有mail.password屬性。你可以設置它,但它什麼都不做。有關[連接到Yahoo!的示例,請參閱JavaMail FAQ]。 Mail](http://www.oracle.com/technetwork/java/javamail/faq/index.html#yahoomail)以及[調試提示](http://www.oracle.com/technetwork/java/javamail /faq/index.html#condebug)如果你仍然得到這個錯誤。 – 2014-12-07 20:39:12
嘗試使用端口587.我可以通過我的工作配置雅虎smtp(xml配置,但它是同樣的事情) – 2014-12-10 10:37:17