简体   繁体   English

JavaMail - javax.mail.MessagingException

[英]JavaMail - javax.mail.MessagingException

I am trying to write a simple mail sender class that would receive a bunch of arguments and using those will send an email out using our Exchange 2010 server. 我正在尝试编写一个简单的邮件发件人类,它将接收一堆参数,并使用这些参数将使用我们的Exchange 2010服务器发送电子邮件。 While authentication etc. seem to work fine, I am getting the following exception when the code is actually trying to send the email (I think). 虽然身份验证等似乎工作正常,但当代码实际上尝试发送电子邮件时,我得到以下异常(我认为)。 I have ensured that the authentication is working and I get a transport back from the session, but still it fails. 我已确保身份验证正在运行,我从会话中获取传输,但仍然失败。 Could anyone shed some like on what I am doing wrong or missing? 任何人都可以对我做错了什么或丢失了吗? Thanks. 谢谢。

Exception: 例外:

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)

Relevant code: 相关代码:

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 );
    }
}

EDIT: I turned on debugging and it says 编辑:我打开调试,它说

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. 

Why would that be when the session authentication succeded? 当会话认证成功时,为什么会这样?

Thanks Alex and Bill. 谢谢亚历克斯和比尔。 I got it working by enabling the following property. 我通过启用以下属性来实现它。

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

Two possibilities: 两种可能性:

  1. It's not actually trying to authenticate because of your use of Session.getDefaultInstance . 由于您使用了Session.getDefaultInstance,它实际上并没有尝试进行身份验证。
  2. It's not authenticating because it's not finding any authentication mechanisms that both the client and server support, possibly because the server wants you to use SSL before sending any authentication information. 它没有进行身份验证,因为它没有找到客户端和服务器都支持的任何身份验证机制,可能是因为服务器希望您在发送任何身份验证信息之前使用SSL。 Try setting "mail.smtp.starttls.enable" to "true". 尝试将“mail.smtp.starttls.enable”设置为“true”。

If we could see more of your debug output, we could tell you which one of these is the case. 如果我们能看到更多的调试输出,我们可以告诉你哪一个是这种情况。

I also had this error trace my correction was: 我也有这个错误跟踪我的更正是:

For OS centos7 : go to vi /etc/hosts and verify that the IP address is correct 对于OS centos7:转到vi / etc / hosts并验证IP地址是否正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM