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("[email protected]");
email.addTo("[email protected]");
email.setSubject("subject");
email.setMsg("Message");
Mail.send(email);

An HTML e-mail:

HtmlEmail email = new HtmlEmail();
email.addTo("[email protected]");
email.setFrom([email protected]", "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 <[email protected]>");
      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 <[email protected]>");
      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.

时间: 2024-10-14 05:24:01

Sending e-mail的相关文章

Dell iDRAC sending trap mail settings rac0225

How to setup sending mail option for DELL iDRAC when problem generate: 第一: 设置触发的条件: 第二: 设置要触发的电子邮件地址(我用的是 阿里云 上的邮箱) 第三,如果不行下面的设置会出现一个错误是 rac0225, 所以要进行下面的设置: 设置完毕测试即可成功 这个可能是Dell的一个bug,dell现在真心的....... 原文地址:http://blog.51cto.com/zhangfang526/2160396

【未完】mail邮件报警系统搭建

参考文档: Linux5中使用mailx利用SMTP发送邮件 http://blog.sina.com.cn/s/blog_4d22b97201019yav.html 2.sending of mail (smtp) - connection refused - but smtp server isrunning! http://bytes.com/topic/python/answers/32889-sending-mail-smtp-connection-refused-but-smtp-s

【IOS笔记】Creating Custom Content View Controllers

Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of your app. You use them to present your app’s unique content. All apps need at least one custom content view controller. Complex apps divide the workl

smtp

Network Working Group J. Klensin, EditorRequest for Comments: 2821 AT&T LaboratoriesObsoletes: 821, 974, 1869 April 2001Updates: 1123Category: Standards Track Simple Mail Transfer Protocol Status of this Memo This document specifies an Internet stand

规矩: python模块

ref:Python 编程入门经典 1.定义应用与模块的错误和异常. 2.定义模块中要输出的项.这定义了模块的公共API. 3.为模块编写文档. 4.测试模块. 5.在模块作为程序执行的情况下提供一个回退函数(这个不太理解 回退是啥意思,指的 __name__=='__main__'?) demo: smptlib.py #! /usr/bin/python2.7 '''SMTP/ESMTP client class. This should follow RFC 821 (SMTP), RFC

读书笔记--Java核心技术--高级特征

第一章--流与文件---------------------------------------------- 流 读写字节 java.io.InputStream 1.0 abstract int read() //从数据中读入一个字节,并返回该字节,在碰到流的结尾时返回-1 int read(byte[] b) //读入一个字节数组,并返回实际读入的字节数,或者在碰到流的结尾时返回-1 int read(byte[] b, int off, int len) //读入一个字节数组.这个rea

python 收发邮件

今天记录一下如何使用python收发邮件,知识要点在python内置的poplib和stmplib模块的使用上. 1. 准备工作 首先,我们需要有一个测试邮箱,我们使用新浪邮箱,而且要进行如下设置: 在新浪邮箱首页的右上角找到设置->更多设置,然后在左边选择"客户端/pop/imap/smtp": 最后,将Pop3/smtp服务的服务状态打开即可: 2. poplib接收邮件 首先,介绍一下poplib登录邮箱和下载邮件的一些接口: self.popHost = 'pop.sin

Python_Imaging_Library中文手册、PIL中文手册、python图像处理

Python Imaging Library 中文手册 这是PIL的官方手册,2005年5月6日发布.这个版本涵盖 PIL 1.1.5的全部内容.本中文手册来自Woodpecker.org.cn 啄木鸟社区 你可以在PythonWare library找到改文档其它格式的版本以及先前的版本. 原版出处:http://www.pythonware.com/library/pil/handbook/ 目录 Python Imaging Library 中文手册 第一部分:介绍 概览 介绍 图像归档处

python PIL学习

http://www.pythonware.com/products/pil/index.htm 文档 例子:http://www.cnblogs.com/way_testlife/archive/2011/04/17/2019013.html    http://onlypython.group.iteye.com/group/wiki/1371-python-graphics-library-pil-python-image-library-introduction http://wenku

WHM使用手册by lin

WebHost Manager 11使用手册(WHM使用手册) 本手册翻译自cpanel官方文档. 本翻译中文版本版权归美国主机侦探所有,未经允许,禁止复制. Overview(概述) 本用户手册主要目的是让新用户熟悉WebHost Manager Interface(WebHost Manager界面):并给老用户补充点额外的知识.本手册将着重介绍如何使用WebHost Manager来安装,配置和管理你的服务器以满足虚拟主机的需要. 如果你是刚刚接触服务器管理和虚拟主机,那么本手册中出现的很