Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending emails from local host - Play Framework

Does anybody know the smtp settings that need to be applied within the application.conf file of a Play Framework project for sending emails on localhost?

During my unit testing I am getting the error:

A play.exceptions.MailException has been caught, Cannot send email

The developers at Play have made sending emails so easy that the only way I could be messing up is with my settings in the config file.

I have tried just using:

mail.smtp=mock

And I tried commenting out the line above and using:

mail.smtp.host=127.0.0.1

Neither of these two approaches work. I understand that this is probably a very noob question, but I have never really dealt with setting up emails before - so I am grateful for any help that can be contributed.

If it is the case that I am unable to send email as Play would not work as an SMTP server, is there any way that I can use mail.smtp=mock to 'mock send' an email and allow my tests to pass?

Useful Link

This is a link to the Play documentation for sending emails

like image 794
My Head Hurts Avatar asked Dec 21 '22 09:12

My Head Hurts


2 Answers

To better diagnose the problem, you can use the following setting (in application.conf) to give more details of the email sending process.

mail.debug=true

However, for testing purposes, I have found using GMail the easiest method for sending emails. The configuration (again in application.conf) is...

mail.smtp.host=smtp.gmail.com
mail.smtp.user=yourGmailLogin
mail.smtp.pass=yourGmailPassword
mail.smtp.channel=ssl

For full details of all the configurations, the Play Framework page has lots of information on how to do this.

http://www.playframework.org/documentation/1.2.2/emails

like image 69
Codemwnci Avatar answered Dec 26 '22 15:12

Codemwnci


I use 1.2.4. (haven't ported yet...)

There are various reasons why play.mvc.Mailer.send() may fail, however the true reason is suppressed if you look at the first level of the exception thrown since what gets thrown is (play.mvc.Mailer.java:349):

throw new MailException("Cannot send email", ex);

play.exceptions.MailException inherits from java.lang.RuntimeException. The real exception, ex, is set as the cause.

I'd recommend taking a look at the cause field. Example:

try {
    Future<Boolean> future = play.mvc.Mailer.send(...);
} catch(MailException me) {
    System.out.println(me.getCause().getMessage());
}

This may print out something more useful.

like image 30
Diego Matute Avatar answered Dec 26 '22 15:12

Diego Matute