gpt4 book ai didi

JavaMail - javax.mail.MessagingException

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:47 24 4
gpt4 key购买 nike

我正在尝试编写一个简单的邮件发件人类,该类将接收一堆参数并使用这些参数将使用我们的 Exchange 2010 服务器发送电子邮件。虽然身份验证等似乎工作正常,但当代码实际尝试发送电子邮件时(我认为),我收到以下异常。我已确保身份验证正常工作,并且我从 session 中获得了传输,但它仍然失败。任何人都可以对我做错或遗漏的事情有所了解吗?谢谢。

异常:

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.

为什么会是 session 认证成功的时候?

最佳答案

谢谢亚历克斯和比尔。我通过启用以下属性让它工作。

props.put("mail.smtp.starttls.enable", "true");

关于JavaMail - javax.mail.MessagingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12306685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com