我正在使用 JAVAMail API 发送电子邮件。但身份验证失败。
这是示例输入
private String to = "junaidbinsarfraz@yahoo.com";
private String from = "juniad_rocku@yahoo.com";
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
。我错在哪里?请帮忙。