AuthenticationFailedException:将未经身份验证的邮件发送到SMTP服务器时的[EOF]

时间:2013-10-18 21:12:54

标签: smtp javamail smtp-auth

我想弄清楚为什么当smtp.auth属性设置为false时我收到AuthenticationFailedException。从调试日志中,它仍然尝试授权登录。我已经针对在服务器上运行的localhost smtp服务器测试了代码,该服务器工作正常,但在发送到第三方服务器时失败。

我尝试通过telnet手动发送电子邮件,而不使用身份验证,电子邮件通过第三方smtp服务器成功发送,所以我不相信服务器的限制。

我正在使用javamail-1.4.7。

DEBUG OUTPUT

我不明白为什么它正在尝试进行身份验证,即使它已被禁用

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
Oct 18, 2013 11:59:25 PM javax.mail.Session getProvider
FINE: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport protocolConnect
FINE: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "mail.server.com", port 25, isSSL false
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport openServer
FINE: trying to connect to host "mail.server.com", port 25, isSSL false
Oct 18, 2013 11:59:25 PM com.sun.mail.util.SocketFetcher getSocket
FINER: getSocket, host mail.server.com, port 25, prefix mail.smtp, useSSL false
220 ded1081-lin-96-181.server.net ESMTP
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 220 ded1081-lin-96-181.server.net ESMTP
DEBUG SMTP: connected to host "mail.server.com", port: 25

Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport openServer
FINE: connected to host "mail.server.com", port: 25

EHLO menez-bpm.server.com
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: EHLO menez-bpm.server.com
250-ded1081-lin-96-181.server.net
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-ded1081-lin-69-81.server.net
250-AUTH=LOGIN CRAM-MD5 PLAIN
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-AUTH=LOGIN CRAM-MD5 PLAIN
250-AUTH LOGIN CRAM-MD5 PLAIN
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-AUTH LOGIN CRAM-MD5 PLAIN
250-STARTTLS
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-STARTTLS
250-PIPELINING
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250-PIPELINING
250 8BITMIME
Oct 18, 2013 11:59:25 PM com.sun.mail.util.LogOutputStream log
FINEST: 250 8BITMIME
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "CRAM-MD5 PLAIN"
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "AUTH=LOGIN", arg "CRAM-MD5 PLAIN"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN CRAM-MD5 PLAIN"
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "AUTH", arg "LOGIN CRAM-MD5 PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport ehlo
FINE: Found extension "8BITMIME", arg ""
        DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
        Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport authenticate
        FINE: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
Oct 18, 2013 11:59:25 PM com.sun.mail.smtp.SMTPTransport$Authenticator authenticate
FINE: AUTH LOGIN command trace suppressed
DEBUG SMTP: EOF: [EOF]
Oct 18, 2013 11:59:26 PM com.sun.mail.smtp.SMTPTransport readServerResponse
FINE: EOF: [EOF]
DEBUG SMTP: AUTH LOGIN failed
Oct 18, 2013 11:59:26 PM com.sun.mail.smtp.SMTPTransport$Authenticator authenticate
FINE: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: [EOF]
    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:295)
    at com.compressus.medx.domain.service.AlertDomainService.sendEmailNotification(AlertDomainService.java:324)
    at com.compressus.medx.domain.service.AlertDomainService.sendEmailNotification(AlertDomainService.java:224)
    at com.compressus.medx.domain.service.UserDomainService.sendEmailNotificationByRole(UserDomainService.java:377)
    at com.compressus.medx.domain.service.MailTest.main(MailTest.java:38)
[ERROR] Bad credentials or smtp server information supplied. Check configuration. [Ljava.lang.StackTraceElement;@394b394b

代码

请注意,即使mail.smtp.auth默认为false,我也尝试明确声明它,但仍然没有帮助。

MailServer mailServer = MailServer.getInstance();

if (mailServer != null)
{

    Properties mailProps = new Properties();

    mailProps.put("mail.smtp.host", mailServer.getSmtpServer());
    mailProps.put("mail.smtp.port", mailServer.getSmtpPort());
    //mailProps.put("mail.smtp.auth.login.disable", "true");
    //mailProps.put("mail.smtp.auth", "false");
    mailProps.put("mail.debug", "true");
    /*
     * Authentication is true if the email account name is set.
     */

    if (mailServer.getSmtpAccount() != null && !mailServer.getSmtpAccount().equals(""))
    {
    mailProps.put("mail.smtp.auth", "true");
    //mailProps.put("mail.smtp.auth.login.disable", "false");
    mailProps.put("mail.smtp.auth.user", mailServer.getSmtpAccount());
    mailProps.put("mail.smtp.auth.password", mailServer.getSmtpPassword());
    }

    if (mailServer.getSmtpEncryptionType().equals("TLS"))
    {
    mailProps.put("mail.smtp.starttls.enable", "true");
    }
    else if (mailServer.getSmtpEncryptionType().equals("SSL"))
    {
    mailProps.put("mail.smtp.socketFactory.port", mailServer.getSmtpPort());
    mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }

    try
    {
    Session session = Session.getInstance(mailProps);

    // create a message
    Message msg = new MimeMessage(session);
    // msg.setFrom(new InternetAddress(from));
    // log.debug("setting from ... success:" + from);
    InternetAddress[] addresses = null;

    String[] addressList = toUserEmailAddress.split(",");
    addresses = new InternetAddress[addressList.length];
    for (int i = 0; i < addressList.length; i++)
        addresses[i] = new InternetAddress(addressList[i]);

    msg.setRecipients(Message.RecipientType.BCC, addresses);
    msg.setSubject(subjectText);
    msg.setSentDate(new java.util.Date());

    if (from == null || from.trim().equals(""))
    {
        msg.setFrom(new InternetAddress("no-reply@compressus.com", "MEDxConnect"));
    }
    else
    {
        msg.setFrom(new InternetAddress("no-reply@compressus.com", from));
    }

    Multipart mainPart = new MimeMultipart();
    // Add body to the email
    BodyPart bodyTextPart = new MimeBodyPart();
    bodyTextPart.setContent(bodyText, "text/html");
    mainPart.addBodyPart(bodyTextPart);

    // add attachment to the email
    if (attachmentPath != null && !attachmentPath.isEmpty())
    {
        BodyPart attachPart = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(attachmentPath);
        attachPart.setDataHandler(new DataHandler(fds));
        attachPart.setFileName(fds.getName());
        attachPart.setHeader("Content-Type", "application/pdf");
        attachPart.setHeader("Content-Transfer-Encoding", "BASE64");
        mainPart.addBodyPart(attachPart);
    }
    msg.setContent(mainPart);
    Transport transport;
    if (!mailServer.getSmtpEncryptionType().equals("None"))
        transport = session.getTransport("smtps");
    else
        transport = session.getTransport("smtp");
    transport.connect(mailServer.getSmtpServer(), mailServer.getSmtpPort(), mailServer.getSmtpAccount(), mailServer.getSmtpPassword());

    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    log.info("Mail " + subjectText + " sent to " + toUserEmailAddress);
    retValue = true;
    }
    catch (AuthenticationFailedException authEx)
    {
    java.util.logging.Logger l = java.util.logging.Logger.getLogger(AlertDomainService.class.getName());
    l.setLevel(Level.FINEST);
    log.error("Bad credentials or smtp server information supplied. Check configuration. " + authEx.getStackTrace());
    }
    catch (Exception ex)
    {
    log.error("Error occurred while sending email: " + ex);

    do
    {
        if (ex instanceof SendFailedException)
        {
        String errormsg = "";
        SendFailedException sfex = (SendFailedException) ex;
        Address[] invalid = sfex.getInvalidAddresses();
        if (invalid != null && invalid.length > 0)
        {
            errormsg = "Invalid Addresses: ";
            for (int i = 0; i < invalid.length; i++)
            {
            errormsg += invalid[i] + " ";

            }
            log.error(errormsg);
        }
        Address[] validUnsent = sfex.getValidUnsentAddresses();
        if (validUnsent != null && validUnsent.length > 0)
        {
            errormsg = "ValidUnsent Addresses: ";

            for (int i = 0; i < validUnsent.length; i++)
            {
            errormsg += validUnsent[i] + " ";

            }
            log.error(errormsg);

        }
        Address[] validSent = sfex.getValidSentAddresses();
        if (validSent != null && validSent.length > 0)
        {
            errormsg = "ValidSent Addresses: ";

            for (int i = 0; i < validSent.length; i++)
            {
            errormsg += validSent[i] + " ";

            }
            log.error(errormsg);

        }
        }
        if (ex instanceof MessagingException)
        {
        ex = ((MessagingException) ex).getNextException();
        }
        else
        {
        ex = null;
        }
    } while (ex != null);
    }
}
else
{
    log.error("No mail server information found. Email notification aborted");
}

2 个答案:

答案 0 :(得分:3)

它正在验证,因为您使用用户名和密码调用connect方法。

这有点令人困惑,但将mail.smtp.auth设置为true的原因是,如果您在没有用户名和密码的情况下调用connect方法,则强制它回调您的Authenticator。将其设置为false可避免在这种情况下进行回调。但是,如果您明确传入用户名和密码,就像您正在做的那样,它假定您是故意这样做的,并且您希望使用它们进行身份验证。

答案 1 :(得分:0)

您可以更好地使用SMTPTransport

使其像

SMTPTransport m_transport;

if(mail.smtp.auth is set to false)
{
    m_transport.connect(new Socket(getSMTPHost, getSMTPPort));
)else{
    m_transport.connect(SMTPHost,SMTPPort,AuthUserAddress,Password);
}

使用Socket你需要导入java.net.Socket