E-mail functionality uses the Apache Commons Email library under the hood. You can use theplay.libs.Mail utility class to send e-mail very easily.

A simple e-mail:

SimpleEmail email = new SimpleEmail();
email.setFrom("sender@zenexity.fr");
email.addTo("recipient@zenexity.fr");
email.setSubject("subject");
email.setMsg("Message");
Mail.send(email);

An 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");
// embed the image and get the content id
URL url = new URL("http://www.zenexity.fr/wp-content/themes/images/logo.png");
String cid = email.embed(url, "Zenexity logo");
// set the html message
email.setHtmlMsg("<html>Zenexity logo - <img src=\"cid:"+cid+"\"></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML, too bad :(");

For more information see the Commons Email documentation.

Mail and MVC integration

You can also send complex, dynamic e-mail using the standard templates mechanism and syntax.

First, define a Mailer notifier in your application. Your mailer notifier must subclass play.mvc.Mailerand be part of the notifiers package.

Each public static method will be an e-mail sender, in a similar manner as actions for an MVC controller. For example:

package notifiers;

import play.*;
import play.mvc.*;
import java.util.*; public class Mails extends Mailer { public static void 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 static void lostPassword(User user) {
String newpassword = user.password;
setFrom("Robot <robot@thecompany.com>");
setSubject("Your password has been reset");
addRecipient(user.email);
send(user, newpassword);
} }

text/html e-mail

The send method call will render the app/views/Mails/welcome.html template as the e-mail message body.

<html><body><p>Welcome <b>${user.name}</b>, </p>
...
</html>

The template for the lostPassword method could look like this:

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

If no HTML template is defined, then a text/plain e-mail is sent using the text template.

The send method call will render the app/views/Mails/welcome.txt template as the e-mail message body.

Welcome ${user.name},
...

The template for the lostPassword method could look like this:

app/views/Mails/lostPassword.txt

Hello ${user.name},

Your new password is ${newpassword}.

text/html e-mail with text/plain alternative

If an HTML template is defined and a text template exists, then the text template will be used as an alternative message. In our previous example, if both app/views/Mails/lostPassword.html andapp/views/Mails/lostPassword.txt are defined, then the e-mail will be sent in text/html as defined in lostPassword.html with an alternative part as defined in lostPassword.txt. So you can send nice HMTL e-mail to your friends and still please those geeky friends that still use mutt ;)

Links to your application in e-mail

Your can include links to your application in e-mails like this:

@@{application.index}

If you send mails from Jobs you have to configure application.baseUrl in application.conf.
application.baseUrl must be a valid external baseurl to your application.

If the website playframework.org where to send you an e-mail from inside a Job, its configuration
would look like this:

application.baseUrl=http://www.playframework.org/

SMTP configuration

E-mail functionality is configured in your application’s conf/application.conf file. First of all, you need to define the SMTP server to use:

mail.smtp.host=smtp.taldius.net

If your SMTP server requires authentication, use the following properties:

mail.smtp.user=jfp
mail.smtp.pass=topsecret

Channel & ports

There are two ways to send the e-mail over an encrypted channel. If your server supports the starttlscommand (see: RFC 2487), you can use a clear connection on port 25 that will switch to SSL/TLS. You can do so by adding this configuration option:

mail.smtp.channel=starttls

Your server may also provide a SMTP-over-SSL (SMTPS) connector, that is an SSL socket listening on port 465. In that case, you tell Play to use this setup using the configuration option:

mail.smtp.channel=ssl

More about configuration

Under the hood, Play uses JavaMail to perform the actual SMTP transactions. If you need to see what’s going on, try:

mail.debug=true

When using SSL connections with JavaMail, the default SSL behavior is to drop the connection if the remote server certificate is not signed by a root certificate. This is the case in particular when using a self-signed certificate. Play’s default behavior is to skip that check. You can control this using the following property:

mail.smtp.socketFactory.class

If you need to connect to servers using non-standard ports, the following property will override the defaults:

mail.smtp.port=2500

Using Gmail

To use Gmail’s servers, use this configuration:

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

Continuing the discussion

Now we shall move on to Testing the application.

Sending e-mail的更多相关文章

  1. mailsend - Send mail via SMTP protocol from command line

    Introduction mailsend is a simple command line program to send mail via SMTP protocol. I used to sen ...

  2. 【IOS笔记】Creating Custom Content View Controllers

    Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...

  3. 批处理协同blat自动发邮件

    Blat - A Windows (32 & 64 bit) command line SMTP mailer. Use it to automatically eMail logs, the ...

  4. WHM使用手册by lin

    WebHost Manager 11使用手册(WHM使用手册) 本手册翻译自cpanel官方文档. 本翻译中文版本版权归美国主机侦探所有,未经允许,禁止复制. Overview(概述) 本用户手册主要 ...

  5. linux 命令中英文对照,收集

    linux 命令中英文对照,收集   linux 命令英文全文 Is Linux CLI case-sensitive? The answer is, yes. If you try to run L ...

  6. How to Verify Email Address

    http://www.ruanyifeng.com/blog/2017/06/smtp-protocol.html  如何验证 Email 地址:SMTP 协议入门教程 https://en.wiki ...

  7. [转]The NTLM Authentication Protocol and Security Support Provider

    本文转自:http://davenport.sourceforge.net/ntlm.html#ntlmHttpAuthentication The NTLM Authentication Proto ...

  8. UFW Essentials: Common Firewall Rules and Commands

    Introduction UFW is a firewall configuration tool for iptables that is included with Ubuntu by defau ...

  9. Tomcat翻译--JNDI Resources HOW-TO

    原文:http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html Introduction(介绍) Tomcat provide ...

  10. View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers

    Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...

随机推荐

  1. 封装WebAPI客户端,附赠Nuget打包上传VS拓展工具

    一.前言 上篇< WebAPI使用多个xml文件生成帮助文档 >有提到为什么会出现基于多个xml文件生成帮助文档的解决方案,因为定义的模型可能的用处有: 1:单元测试 2:其他项目引用(可 ...

  2. silverlight中 Storyboard(动画)的使用,实现球的上下循环移动,左右移动,及旋转功能

    话说,总结应该是个收获的心情,可现在的自己似乎没感觉到哪个喜悦的心情,说明自己做得还不够好,现在还是把一些做好的东西总结下.“总结”是学习,工作中必须的,不能偷这个懒 o(╯□╰)o 实现上下循环移动 ...

  3. Cwinux源码解析(五)

      Cwinux源码解析(五)

  4. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  5. JavaScript垃圾回收(三)——内存泄露

    一.JavaScript内存监测工具 在讨论内存泄露之前,先介绍几款JavaScript内存监测工具. IE的sIEve与JSLeaksDetector(这两个可以在下面的附件中下载),firefox ...

  6. Pillow实现图片对比

    在编写Web自动化测试用例的时候,如何写断言使新手不解,严格意义上来讲,没有断言的自动化脚本不能叫测试用例.就像功能测试一样,当测试人员做了一些操作之后必然会判断实际结果是否等于预期结果,只不过,这个 ...

  7. 纯JS实现可拖拽表单

    转载注明出处!!! 转载注明出处!!! 转载注明出处!!! 因为要用到可拖拽表单,个人要比较喜欢自己动手,不怎么喜欢在不懂实现或者原理的情况下用插件,所以查找资料实现了一个. 思路:放入:用mouse ...

  8. js每天进步一点点3

    JS之样式的改变

  9. 如何解决Android SDK中离线文档打开慢的问题

    原文:http://blog.csdn.net/hansel/article/details/39268511 Android SDK中的离线文档虽然都是本地文件,但是有很多Javascript, C ...

  10. C++程序设计之四书五经[转自2004程序员杂志]--上篇

    C++程序设计之四书五经 作者:荣耀 C++是一门广泛用于工业软件研发的大型语言.它自身的复杂性和解决现实问题的能力,使其极具学术研究价值和工业价值.和C语言一样,C++已经在许多重要的领域大获成功. ...