Sending email is very simple in ActFramework. What you need to do is:
-
Use
Mailerannotation to tag your class that contains send mail methods -
Extend your class to
Mailer.Utilclass or add a static import statement:import static act.mail.Mailer.Util.*
package com.mycom.myprj;
import act.conf.AppConfig;
import act.job.Every;
import act.mail.Mailer;
import org.osgl.logging.*;
import org.osgl.util.E;
import org.osgl.util.S;
import javax.inject.Inject;
/**
* This class demonstrate how to write an email sender
* in act framework.
*/
@Mailer
public class PostOffice extends Mailer.Util {
public void sendWelcome(Contact who) {
to(who.getEmail());
subject("Welcome letter");
send(who);
}
public void sendBye(Contact who) {
to(who.getEmail());
send(who);
}
}Now that you have the mailer created, you can create the template corresponding to the mailer methods:
src/main/resources/rythm/com/mycom/myprj/PostOffice/sendWelcome.html:
<html>
<head></head>
<body>
@args com.mycom.myprj.Contact who
<h1>Welcome @who.getFirstName()!</h1>
<p>Blah Blah</p>
</body>
</html>src/main/resources/rythm/com/mycom/myprj/PostOffice/sendBye.html:
<html>
<head></head>
<body>
@args com.mycom.myprj.Contact who
<h1>Good bye @who.getFirstName()!</h1>
</body>
</html>You can call the mailer method directly from any where, like the following controller code:
public class MyController {
@Inject
private PostOffice postOffice;
@PostAction("/contact")
public void createContact(Contact contact) {
contactDao.save(contact);
postOffice.sendWelcome(contact);
}
@PutAction("/contact/{contactId}/signOff")
public void signOff(String contactId) {
Contact contact = contactDao.findById(contactId);
contact.signOff();
contactDao.save(contact);
postOffice.sendBye(contact);
}
}Usually executing mailer method involves IPC to external services (e.g. your SMTP server), which could be time consuming. So you would prefer to execute mailer method asynchrously and return back immediately. You can use ActFramework's event dispatching mechanism to achieve that.
-
Mark your mailer methods to be an event handler
@On(value = "contact-created", async = true) public void sendWelcome(Contact who) { to(who.getEmail()); subject("Welcome letter"); send(who); } @On(value = "contact-signed-off", async = true) public void sendBye(Contact who) { to(who.getEmail()); send(who); }
-
Trigger event instead of calling mailer method directly:
public class MyController { @Inject EventBus eventBus; @PostAction("/contact") public void createContact(Contact contact) { contactDao.save(contact); eventBus.trigger("contact-created", contact); } @PutAction("/contact/{contactId}/signOff") public void signOff(String contactId) { Contact contact = contactDao.findById(contactId); contact.signOff(); contactDao.save(contact); eventBus.trigger("contact-signed-off", contact); } }