Sending e-mail
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的更多相关文章
- 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 ...
- 【IOS笔记】Creating Custom Content View Controllers
Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...
- 批处理协同blat自动发邮件
Blat - A Windows (32 & 64 bit) command line SMTP mailer. Use it to automatically eMail logs, the ...
- WHM使用手册by lin
WebHost Manager 11使用手册(WHM使用手册) 本手册翻译自cpanel官方文档. 本翻译中文版本版权归美国主机侦探所有,未经允许,禁止复制. Overview(概述) 本用户手册主要 ...
- linux 命令中英文对照,收集
linux 命令中英文对照,收集 linux 命令英文全文 Is Linux CLI case-sensitive? The answer is, yes. If you try to run L ...
- How to Verify Email Address
http://www.ruanyifeng.com/blog/2017/06/smtp-protocol.html 如何验证 Email 地址:SMTP 协议入门教程 https://en.wiki ...
- [转]The NTLM Authentication Protocol and Security Support Provider
本文转自:http://davenport.sourceforge.net/ntlm.html#ntlmHttpAuthentication The NTLM Authentication Proto ...
- UFW Essentials: Common Firewall Rules and Commands
Introduction UFW is a firewall configuration tool for iptables that is included with Ubuntu by defau ...
- Tomcat翻译--JNDI Resources HOW-TO
原文:http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html Introduction(介绍) Tomcat provide ...
- View Controller Programming Guide for iOS---(四)---Creating Custom Content View Controllers
Creating Custom Content View Controllers 创建自定义内容视图控制器 Custom content view controllers are the heart ...
随机推荐
- 分享一个 C# Winfrom 下的 OutlookBar 控件的使用
最近在上网的时候,发现了这个C# 下的 OutlookBar 控件,看了一下感觉还真不错,特此记录一下. using System; using System.Drawing; using Syste ...
- 了解HTML表单之input元素的23种type类型
目录 传统类型 text password file radio checkbox hidden button image reset submit 新增类型 color tel email url ...
- JSP网站开发基础总结《八》
JSP的学习总结到本篇已经八篇了,内容比较多,但都是实战,本篇最后为大家介绍一个小效果:百度分页.就是当我们遍历的数据对象较多时,这时我们就会看到了这个效果了,那他是如何实现的呢?下面我们就一起学习一 ...
- Dijkstra算法(一)之 C语言详解
本章介绍迪杰斯特拉算法.和以往一样,本文会先对迪杰斯特拉算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法 ...
- CSS3的学习--实现瀑布流
基于CSS3实现瀑布流,使用CSS3的CSS 多栏(Multi-column). 可以到github上下载源码 : https://github.com/CraryPrimitiveMan/water ...
- android布局文件中android:icon="?attr/menuIconCamera"找不到对应图标路径
如 <item android:id="@+id/camera" android:title="Camera" android:icon="?a ...
- VS2008 C++ 调用MATLAB 2009b 生成的DLL .
转载: 刚开始学习用VC++调用matlab生成的DLL,找了网上一些资料,难以找到vs2008与MATLAB2009b版本的,按照以往版本做的总是有很多错误.经过两天努力,终于调试成功,这里将经验总 ...
- 解析导航栏的url--selnium,beautifulsoup实战
前段时间做ui自动化测试的时候,导航栏菜单始终有点问题,最后只好直接获取到url,然后直接使用driver.get(url)进入页面: 包括做压测的时候,比如我要找出所有报表菜单的url,这样不可能手 ...
- 使用Graphviz绘图(一)
前言 日常开发或者编写课程论文时,除了代码文档的编写,其中一个很重要的部分就是需要绘制流程图.示意图 绘制流程图有很多工具,一般常见的就有如下几种: Word.PPT等办公软件 Viso以及开源的Di ...
- C#--声明类