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!