Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail with MS Exchange: No authentication mechansims supported by both server and client

I've been trying for days now to send mail from Grails application and unsuccessfully. I'm using:

  • grails 1.3.7
  • mail 1.0 plugin
  • spring-security-core 1.2.6 plugin
  • tomcat 7.0.23

Specifficaly I'm trying to send mail with Exchange from application deployed on Tomcat server trought port 25 with no authentication, no SSL.

I've tried to send message with telnet from the VMWare virtual machine on which the app is deployed and it got trough.

This is my class for sending mails:

public boolean sendMessage(String to, String msgSubject, String msgText) 
{
    String host = "mail.mydomain.com";
    String username = "[email protected]"; // your authsmtp username
    String password = "mypassword" // your authsmtp password
    String from = "[email protected]";

    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", username);
    props.put("mail.smtp.password", password);
    props.put("mail.smtp.port", "25"); // thish is the port recommended by authsmtp
    props.put("mail.smtp.auth", "false");

    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress to_address = new InternetAddress(to);
    message.addRecipient(Message.RecipientType.TO, to_address);

    message.setSubject(msgSubject);
    message.setText(msgText);
    Transport transport = session.getTransport("smtp");
    transport.connect(host, username, password);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    return true;
}

This is error stack trace:

javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client

at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:590)

at javax.mail.Service.connect(Service.java:291)

at javax.mail.Service.connect(Service.java:172)

at javax.mail.Service$connect.call(Unknown Source)

at org.helpdesk.MymailService.sendMessage(MymailService.groovy:37)

at org.helpdesk.MymailService$sendMessage.call(Unknown Source)

at org.helpdesk.RequestController$_closure13.doCall(RequestController.groovy:247)

at org.helpdesk.RequestController$_closure13.doCall(RequestController.groovy)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

I've read few dozen posts considering problems like this but I've still havent manage to solve the problem. Any help is appreciated.

*EDIT:*Is it possible that there are some problems sending mails using javaMail with Exchange server SMTP when there is no authentication?

like image 306
drago Avatar asked Dec 23 '11 11:12

drago


4 Answers

in my case, I had to set the property

"mail.smtp.ehlo"

to "false"

(in addition to adding to setting the property "mail.smtp.auth" to "false" which however seems to be the default according to this link)

Before setting "mail.smtp.ehlo" to "false" I saw the following debug output (enabled by setting the property "mail.debug" to "true"):

DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: mechanism LOGIN not supported by server
DEBUG SMTP: mechanism PLAIN not supported by server
DEBUG SMTP: mechanism DIGEST-MD5 not supported by server
DEBUG SMTP: mechanism NTLM not supported by server

and then getting the same javax.mail.AuthenticationFailedException.

(in this case, the SMTP server was a Microsoft one)

like image 179
Andre Holzner Avatar answered Oct 20 '22 10:10

Andre Holzner


If you're trying to connect to your mail server without authentication, call the connect method that doesn't take a username and password. If you pass it a username and password, it thinks you really want to authenticate, and since it can't find an authentication mechanism that the server supports, it fails.

like image 32
Bill Shannon Avatar answered Oct 20 '22 10:10

Bill Shannon


Well it looks that I had few problems. At first, Exchange wasn't setup correctly. And then it seems I've tried all possible configurations but the right one. This works:

class MymailService 
{
    boolean transactional = false

    public sendMessage(String to, String cc, String msgSubject, String msgText) 
    {
        String host = "mail.mailserver.com";
        String username = "[email protected]"; 
        String password = "xxx"; 
        String from = "[email protected]";
        String port = "25";

        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port); 
        props.put("mail.smtp.auth", "false");

        Transport transport = null;

        try{
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));

        InternetAddress to_address = new InternetAddress(to);
        message.addRecipient(Message.RecipientType.TO, to_address);

        InternetAddress cc_address = new InternetAddress(cc);
        message.addRecipient(Message.RecipientType.CC, cc_address);

        message.setSubject(msgSubject);
        message.setText(msgText);

        transport = session.getTransport("smtp");
        transport.connect();
        transport.sendMessage(message, message.getAllRecipients());
    } finally {
        if (transport != null) try { transport.close(); } catch (MessagingException logOrIgnore){}
    }
}
}

The final clue was Bill Shannon's post. Thanks Bill!

like image 31
drago Avatar answered Oct 20 '22 11:10

drago


  1. Check if the server you are hitting mandates an authentication or not. more on this in the code.

  2. Do put a mail.debug in the properties to know what exactly is going on between your code and the mailserver. more on this in the code.

Here is a simple code that works well for me with my company's mail server:

 package com.datereminder.service;

 import java.util.Properties;
 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 ReminderDaemonService2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "mail.mycompany123.com");
 // this mandates authentication at the mailserver
        props.put("mail.smtp.auth", "true");
 // this is for printing debugs

        props.put("mail.debug", "true");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("[email protected]","xxxxxxxxxxx");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("[email protected]"));
            message.setSubject("Testing Subject");
            message.setText("Dear Friend," +
                    "\n\n This is a Test mail!");

            Transport.send(message);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
 }
like image 43
Sadique Khan Avatar answered Oct 20 '22 09:10

Sadique Khan