![](/img/trans.png)
[英]javax.mail.MessagingException while sending mail using JavaMail API
[英]JavaMail - javax.mail.MessagingException
我正在嘗試編寫一個簡單的郵件發件人類,它將接收一堆參數,並使用這些參數將使用我們的Exchange 2010服務器發送電子郵件。 雖然身份驗證等似乎工作正常,但當代碼實際上嘗試發送電子郵件時,我得到以下異常(我認為)。 我已確保身份驗證正在運行,我從會話中獲取傳輸,但仍然失敗。 任何人都可以對我做錯了什么或丟失了嗎? 謝謝。
例外:
javax.mail.MessagingException: [EOF]
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481)
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1512)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:140)
at com.ri.common.mail.util.MailSender.sendHTMLEmail(MailSender.java:75)
at com.ri.common.mail.util.MailSender.main(MailSender.java:106)
相關代碼:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSender
{
public static void sendHTMLEmail( String fromEmailId, String toEmailId, String host, String hostUserName,
String hostPassword, String mailSubject, String mailBody )
{
// Get system properties.
Properties props = System.getProperties();
// Setup mail server
props.put( "mail.transport.protocol", "smtp" );
props.put( "mail.smtp.host", host );
props.put( "mail.smtp.auth", "true" );
final String hostUName = hostUserName;
final String hPassword = hostPassword;
Authenticator authenticator = new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication( hostUName, hPassword );
}
};
// Get the default Session object.
Session session = Session.getDefaultInstance( props, authenticator );
try
{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage( session );
// Set From: header field of the header.
message.setFrom( new InternetAddress( fromEmailId ) );
// Set To: header field of the header.
message.addRecipient( Message.RecipientType.TO, new InternetAddress( toEmailId ) );
// Set Subject: header field
message.setSubject( mailSubject );
// Send the actual HTML message, as big as you like
message.setContent( mailBody, "text/html" );
// Send message
Transport.send( message, message.getAllRecipients() );
System.out.println( "Sent message successfully...." );
}
catch( Exception mex )
{
mex.printStackTrace();
}
}
public static void main( String[] args )
{
String to = "someCorrectEmailID@xyz.com";
String from = "someCorrectEmailID@xyz.com";
String host = "correctHostForExch2010";
String user = "correctUser";
String password = "CorrectPassword";
String subject = "Test Email";
String body = "Hi there. This is a test email!";
MailSender.sendHTMLEmail( from, to, host, user, password, subject, body );
}
}
編輯:我打開調試,它說
MAIL FROM:<someCorrectEmailID@xyz.com> 530 5.7.1 Client was not authenticated
DEBUG SMTP: got response code 530, with response: 530 5.7.1 Client was not authenticated.
當會話認證成功時,為什么會這樣?
謝謝亞歷克斯和比爾。 我通過啟用以下屬性來實現它。
props.put("mail.smtp.starttls.enable", "true");
兩種可能性:
如果我們能看到更多的調試輸出,我們可以告訴你哪一個是這種情況。
我也有這個錯誤跟蹤我的更正是:
對於OS centos7:轉到vi / etc / hosts並驗證IP地址是否正確
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.