Sending e-mails with Grails can be quite complicated (yeap, I know about the Mail Plugin, but I also know many people who had some problems with it AND it’s a Grails only alternative).
If you don´t want to use a plugin, you can always count with the Java Mail API, or even the Spring Framework support for it, right? Yeah, right: but at least for me, it seems like a to complex solution for a simple need. So, I started my quest for a simple way to do it. The solution I found was right in front of me all the time (so usual…): Commons E-mail, which is part of the Apache Commons project!
To show how you can use it, here is a small snippet:
import org.apache.commons.mail.SimpleEmail class MailService { boolean transactional = false // I added the configuration on the same class just for the sake of simplicity (you should NEVER do this on a real case) String host= "your.mail.ver" String username = "your.login" String password = "your.password" String from = "me@me.com" Integer port = 465 def send(String subject, String msg, String to) { //SimpleEmail is the class which will do all the hard work for you SimpleEmail email = new SimpleEmail() email.setHostName(host) email.addTo(to) email.setFrom(from) email.setSubject(subject) email.setMsg(msg) //If you need authentication, this is the method email.setAuthentication(username,password) // If you need to specify your smtp port, this is how to email.setSmtpPort(port) // So all you need to do is call the method send() and your e-mail will be delivered (at least in theory) email.send() } }
Just like I wanted: simple!
Dependencies
All you need is JAF, which may be downloaded on the following link: http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp
Where to download Commons E-mail: http://commons.apache.org/email/
Deixe uma resposta