14.发送e-mail
play使用ApacheCommons Email库来实现邮件功能。使用play.libs.Mail工具箱来发送邮件非常容易。
简单邮件示例:
SimpleEmail email = new SimpleEmail();
email.setFrom("sender@zenexity.fr");
email.addTo("recipient@zenexity.fr");
email.setSubject("subject");
email.setMsg("Message");
Mail.send(email);
HTML e-mail示例:
HtmlEmail email = new HtmlEmail();
email.addTo("info@lunatech.com");
email.setFrom(sender@lunatech.com","Nicolas");
email.setSubject("Test email with inline image");
// 获取内容ID,并嵌入图片
URL url = newURL("http://www.zenexity.fr/wp-content/themes/images/logo.png");
String cid = email.embed(url, "Zenexity logo");
//设置html消息
email.setHtmlMsg("<html>Zenexity logo -<img src=\"cid:"+cid+"\"></html>");
//设置可选消息
email.setTextMsg("Your email client does not supportHTML, too bad :(");
更多信息见 Commons Email documentation。
Mail 和MVC 集成
使用标准的模板机制和语法,也可发送复杂的和动态的邮件。
首先在应用程序里定义一个Mailer notifier。mailer notifier必须是play.mvc.Mailer的子类,而且包名必须是notifiers或其子包。
其次,每个e-mail sender的方法都必须是public static的,这个MVC控制的动作相似:
package notifiers;
import play.*;
import play.mvc.*;
import java.util.*;
public class Mails extends Mailer {
public staticvoid welcome(User user) {
setSubject("Welcome%s", user.name);
addRecipient(user.email);
setFrom("Me <me@me.com>");
EmailAttachment attachment = new EmailAttachment();
attachment.setDescription("A pdf document");
attachment.setPath(Play.getFile("rules.pdf").getPath());
addAttachment(attachment);
send(user);
}
public staticvoid lostPassword(User user) {
Stringnewpassword = user.password;
setFrom("Robot <robot@thecompany.com>");
setSubject("Your password has been reset");
addRecipient(user.email);
send(user,newpassword);
}
}
text/html e-mail
调用send方法将会渲染app/views/Mails/welcome.html模板作为邮件消息体。
<html><body><p>Welcome<b>${user.name}</b>, </p>
...
</html>
lostPassword方法的模板代码:
app/views/Mails/lostPassword.html
<html>
<body><head>...</head><body>
<img src="mycompany.com/images"/>
<p>
Hello${user.name}, Your new password is <b>${newpassword}</b>.
</p>
</body>
</html>
text/plain e-mail
如果没有定义HTML模板,那么text/plain邮件将使用text模板进行发送。
调用send方法将会渲染app/views/Mails/welcome.txt模板作为邮件消息体。
Welcome ${user.name},
...
lostPassword方法的模板应该是这个样子: app/views/Mails/lostPassword.txt
Hello ${user.name},
Your new password is ${newpassword}.
text/html e-mail with text/plain alternative
如果HTML模板已经定义,同时又存在text模板,那么text模板将用于可选消息。在上一个示例里,如果app/views/Mails/lostPassword.html和app/views/Mails/lostPassword.txt都已定义,那么在发送邮件时默认将用lostPassword.html模板以text/html方式发送,同时可自由选择lostPassword.txt模板。因此你可以向好朋友用友好的HMTL模板,而对待讨厌的人可以选择使用text模板。
在应用程序里链接到邮件
使用如下语法可在应用程序里链接到邮件:
@@{application.index}
如果要从job里发送邮件,就必须为应用程序设置application.baseUrl为一个有效的基于URL的地址。
比如,从运行于play站点的一个Job发送邮件,其配置应该是这样的:
application.baseUrl=http://www.playframework.org/
SMTP配置
邮件功能可通过配置多个 mail configuration 属性开启:
- SMTP server - mail.smtp.host
- SMTP server 验证 - mail.smtp.user 和 mail.smtp.pass
- 加密通道 - mail.smtp.channel
- JavaMail SMTP 登录事务 - mail.debug.
下面两个配置用于让你重载默认的行为:
默认情况下,在DEV模式时,邮件将打印到控制台,在PROD模板下,将会真正发送到真实的SMTP server。在DEV模式下通过注释下面的配置也可改变默认的行为:
# Default is to use a mock 模拟Mailer
mail.smtp=mock
为了使用Gmail的服务器,你需要作如下配置:
mail.smtp.host=smtp.gmail.com
mail.smtp.user=yourGmailLogin
mail.smtp.pass=yourGmailPassword
mail.smtp.channel=ssl