我第一次使用javamail,並有一些例外,我不明白我在這裏也看到了其他問題中的一些錯誤,但他們的答案並沒有幫助我。這是我的代碼。使用outlook在公司中的Javamail
final String username = "[email protected]";
final String password = "**********";
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "10.1.136.26");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.user" , username);
props.put("mail.smtp.password" , password);
Session session = Session.getInstance(props,
new SmtpAuthenticator(username, password)
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
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);
}
}
SmtpAuthenticator
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
class SmtpAuthenticator extends Authenticator {
String user;
String pw;
public SmtpAuthenticator (String username, String password)
{
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
}
錯誤日誌是
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 250-CDC-HO-CAS1.cdcpk.com Hello [10.1.34.74]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XRDST
at org.cdc.eipo.bean.investorsetup.EmailController.main(EmailController.java:64)
Caused by: javax.mail.AuthenticationFailedException: 250-CDC-HO-CAS1.cdcpk.com Hello [10.1.34.74]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XRDST
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at org.cdc.eipo.bean.investorsetup.EmailController.main(EmailController.java:59)
任何幫助非常感謝
我的殺毒軟件是mcafee,防火牆安裝在網絡上,所以我不能干預它。我已經發布了整個日誌.....但會嘗試通過mail.debug.quote – Anas
做到這一點當mail.debug.quote設置爲true時出現相同的日誌 – Anas
您使用的是哪個版本的JavaMail?你可以發佈整個調試輸出嗎?在發佈之前應該有額外的調試輸出,例如,顯示正在使用的JavaMail的版本並顯示屬性。您可以嘗試關閉反病毒軟件,看看是否有所作爲。郵件服務器在防火牆「內部」,對吧? –