I\'ve been trying for days now to send mail from Grails application and unsuccessfully. I\'m using:
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 = "myusername@mymailserver.com";
String password = "xxx";
String from = "myusername@mymailserver.com";
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!
Here's my solution, maybe it's not the best way, but it works for me...
in the mail-config.xml:
<bean id="mailSender" class="com.xxx.service.MailSender">
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<property name="protocol" value="${mail.protocol}" />
<property name="defaultEncoding" value="UTF-8" />
<property name="authRequired" value="${mail.auth}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">${mail.auth}</prop>
</props>
</property>
</bean>
and here's the setting:
mail.from=XXX Team <xxx@tricascade.com>
mail.host=exchange.xxx.com
mail.port=25
mail.protocol=smtp
mail.auth=false
mail.username=
mail.password=
and finally, the code:
package com.xxx.service;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class MailSender extends JavaMailSenderImpl {
private boolean authRequired;
@Override
public String getUsername() {
if (!authRequired) {
return null;
}
return super.getUsername();
}
@Override
public String getPassword() {
if (!authRequired) {
return null;
}
return super.getPassword();
}
public boolean isAuthRequired() {
return authRequired;
}
public void setAuthRequired(boolean authRequired) {
this.authRequired = authRequired;
}
}
Check if the server you are hitting mandates an authentication or not. more on this in the code.
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("sadique.khan@mycompany123.com","xxxxxxxxxxx");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sadique.khan@mycompany123.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("my.bestfriend@mycompany123.com"));
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);
}
}
}
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)
If you'd want your application that to login to the SMTP server (Since you are providing authentication details). Just change
props.put("mail.smtp.auth", false);
to
props.put("mail.smtp.auth", true);
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.