AuthenticationFailedException: [EOF] when sending mail without authentication to SMTP server

StackOverflow https://stackoverflow.com/questions/19459152

  •  01-07-2022
  •  | 
  •  

Pergunta

I'm trying to figure out why I'm getting an AuthenticationFailedException when the smtp.auth property is set to false. From the debug logs it still attempts to auth login. I've tested the code against a localhost smtp server running on the server which works perfectly fine, but fails when sent to a 3rd party server.

I've tried manually sending an email through telnet, without using authentication and the emails were sent successfully through the 3rd party smtp server so I don't believe its a restriction by the server.

I'm using javamail-1.4.7.

DEBUG OUTPUT

I don't understand why it's attempting to authenticate even though it's disabled

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

Code

Note that even though the mail.smtp.auth is false by default, I tried declaring it explicitly and still did not help.

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");
}
Foi útil?

Solução

It's authenticating because you're calling the connect method with a username and password.

It's a bit confusing, but the reason to set mail.smtp.auth to true is to force it to call back to your Authenticator if you call the connect method without a username and password. Setting it to false avoids the callback in that case. But if you explicitly pass in a username and password, as you're doing, it assumes you did that on purpose and that you want to use them to authenticate.

Outras dicas

you can better use SMTPTransport

make it like

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

to use Socket you need to import java.net.Socket

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top