palavi 0 Junior Poster in Training

Hi All,

I m trying to send a simple mail... but not able to send because of this exception

thread "main" javax.mail.AuthenticationFailedException:

and the problem is in this line

here is the simple java class for sending mail

public class SendMailUsingAuthentication
{

  private static final String SMTP_HOST_NAME = "tx.technoinfo.in";
  private static final String SMTP_AUTH_USER = "pallavi.pm";
  private static final String SMTP_AUTH_PWD  = "abcdefs";

  private static final String emailMsgTxt      = "Body:Test Mail .";
  private static final String emailSubjectTxt  = "Subject:Test Mail .";
  private static final String emailFromAddress = "pmj@sfd.com";

  // Add List of Email address to who email needs to be sent to
  private static final String[] emailList = {"pallavi.pm@techniinfo.in", "nandhini.k@techniinfo.in"};

  public static void main(String args[]) throws Exception
  {
    SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
    smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
    System.out.println("Sucessfully Sent mail to All Users");
  }

  public void postMail( String recipients[ ], String subject,
                            String message , String from) throws MessagingException
  {
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", SMTP_HOST_NAME);
     props.put("mail.smtp.auth", "true");
    
        SMTPAuthenticator auth = new SMTPAuthenticator();
    Session session = Session.getDefaultInstance(props, auth);

    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    System.out.println("before transport " +from);
    Transport.send(msg);
    System.out.println("after transport .. " +from);
 }


/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
	

    public PasswordAuthentication getPasswordAuthentication()
    {
    	System.out.println("here in smtpauthenticator");

        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        return new PasswordAuthentication(username, password);
    }
}

}

Im getting exception in return new PasswordAuthentication(username, password)


Can anybody help on this.......please

below is my exception

Exception in thread "main" javax.mail.AuthenticationFailedException: 250-tx.technoinfo.in Hello [192.168.105.82]
250-SIZE 10485760
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XEXCH50

	at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
	at javax.mail.Service.connect(Service.java:313)
	at javax.mail.Service.connect(Service.java:172)

Dani AI

Generated

The exception shown in the first post is almost always a mismatch between what the SMTP server requires and what the Java client is offering. The EHLO/SMTP capability response indicates the server wants Windows/NTLM-style authentication, while the code is providing a plain username/password via the default Authenticator. That combination causes the SMTP server to reject the client before the password is checked.

Quick troubleshooting checklist (in order):

  • Turn on the JavaMail debug to see the full SMTP conversation: session.setDebug(true).
  • Verify the correct SMTP port and whether STARTTLS/SSL is required (submission typically uses port 587 + STARTTLS).
  • Try a manual EHLO with telnet/openssl or a standard mail client to confirm which AUTH mechanisms the server actually advertises.
  • Try alternate username formats the server may expect (domain\user or user@domain).
  • If the server only offers NTLM (integrated Windows auth), a plain LOGIN/PLAIN client will fail.

Minimal JavaMail property example to try (adjust host/port and only set the NTLM mechanism if you also add an NTLM SASL provider):

Properties props = new Properties();
props.put("mail.smtp.host", "your.smtp.host");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth.mechanisms", "NTLM"); // only if you have an NTLM SASL provider
Session session = Session.getInstance(props, auth);
session.setDebug(true);

Practical remedies

  • Ask the mail/server admin to enable a standard mechanism (PLAIN or LOGIN over TLS) for SMTP AUTH, or create an SMTP submission account.
  • If you must use NTLM, add a Java SASL/NTLM provider (third-party libraries such as Waffle or jcifs variants) so JavaMail can perform NTLM SASL authentication.
  • As a workaround, use an SMTP relay that accepts standard auth and forwards to the server.

References: Jakarta Mail (JavaMail) docs for configuration and debugging, and vendor docs for SMTP/Exchange SMTP AUTH and NTLM support (consult server admin for exact server-side settings). For : enable debug and confirm the server's EHLO lines first — that will tell whether to change client settings or ask the admin for server-side changes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.