fhp-yw
Last Updated: April 21, 2017
·
8.783K
· marti1125
Yo

Send email with Play Framework

check in application.conf "mail.smtp=mock" this uncommented

create class that extend class Mailer, like this
app/controllers/Mails.java

package controllers;

import org.apache.commons.mail.*;

import play.*;
import play.libs.Mail;
import play.mvc.*;
import java.util.*;

public class Mails extends Mailer {

    public static void message(String address, String message) {
        try {

            SimpleEmail email = new SimpleEmail();
            email.setFrom(address);
            email.addTo("marti1125@gmail.com");
            email.setSubject("Question");
            email.setMsg(message);
            Mail.send(email);
            System.out.println("Send message was successful");

        } catch (Exception e) {
            System.out.println("Error...");
            System.out.println(e);
       }

    }

}

add a method in the class Application
app/controllers/Application.java

public static void contact(String address, String message) {  
    Mails mail = new Mails();
    mail.message(address, message);
    render();
}

add view view/Application/contact.html

#{extends 'otherpage.html' /}
#{set title:'Contact' /}

#{form @contact()}
<fieldset>
  <legend>Contáctenos</legend> 
<p>
    <label for="username">Email: </label> <input type="text" name="address" id="address" maxlength="30"  value="${params.address}" />
</p>
<p>
    <label for="password">Message: </label><textarea id="message" name="message">${params.message}</textarea>
</p>
<p>
    <input type="submit" id="send" value="Send" />
</p>
 </fieldset>
 #{/form}

in routes add this:
GET /contact Application.contact

Say Thanks
Respond

4 Responses
Add your response

6484

i will try it, but i think it's gonna work, thank's after all :)

over 1 year ago ·
6495
Yo

what version do you use?

over 1 year ago ·
6500

@marti1125 play framework 1.2.5 , do you know about velocity email template? i want to create email using velocity and play framework, but i have no idea what i must to do, iam a newbie in play framework. i have google it but i think it's more complex. :)

i think i know the answer here take a look if you have time :)
https://coderwall.com/p/rgsmuw

over 1 year ago ·
28822

hi,

Can you help me? Cannot find symbol class Mailer...

++++build.sbt++++
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-mailer" % "3.0.1",
cache,
javaWs
)

++++controller/Mails.java++++
package controllers;
import org.apache.commons.mail.;
import play.
;
import play.libs.;
import play.mvc.
;
import java.io.;
import java.util.
;

++++app.conf++++
mail.smtp=mock

about 2 months ago ·