過去幾天,我一直對我的桌子猛撞我的頭,試圖弄清楚爲什麼當我不斷收到以下錯誤試圖實現Java代碼通過我公司的Outlook服務器發送電子郵件:嘗試通過Java以SMTP方式發送電子郵件,但得到錯誤「AUTH LOGIN失敗」
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
Exception in thread "main" javax.mail.AuthenticationFailedException: 535 5.7.0 authentication failed
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
這裏是有問題的代碼:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail3 {
public static void main(String[] args) throws Exception {
Login e = new Login();
String host = "mail.authsmtp.com";
String port = "465";
final String userName = e.getEmailUser();
final String password = e.getEmailPassword();
String toAddress = "[email protected]";
String subject = "TEST";
String message = "This is a test email";
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.debug", "true");
// properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
}
錯誤處被觸發Transport.send(MSG)。任何指導將不勝感激。
您是否打印出e.getEmailUser()和e.getEmailPassword()並驗證它們是否正確 – Solace
我知道e.getEmailPassword()是正確的事實(除非我的Outlook密碼不同我的Windows 7密碼,這意味着我一直都是錯的)。 e.getEmailUser()是「[email protected]」,但我開始認爲可能需要以不同的方式寫入。 –