英文对照 介绍Play Framework 框架 邮件

lyuehh 发布于 2010/08/08 13:49
阅读 936
收藏 3

使用Play发送邮件

h1. Sending e-mail with Play

 

你可以使用play.libs.Mail工具类容易地发送邮件。

You can use the play.libs.Mail utility to send e-mail very easily:

 

bc. Mail.send("sender@zenexity.fr","recipient@zenexity.fr","Subject","Message");

 

Mail和MVC

h2. <a>Mail and MVC integration</a>

 

你还可以发送复杂的,动态的邮件,使用调准的模板和语法。

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

 

首先,在你的应用中定义一个Mail notifier,你的mail notifier必须是play.mvc.Mailer的子类,而且必须在notifiers文件夹中。

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

 

每一个public static的方法都会是一个邮件发送器,就像MVC控制器中的actions一样。

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

 

例如:

For example:

 

bc. 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>");

      addAttachment(Play.getFile("rules.pdf"));

      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);

   }

 

}

 

基于html的模板

h3. text/html e-mail 

 

发送器会使用app/views/Mails/welcome.html模板作为邮件信息的内容。

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

 

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

...

</html>

 

lostPassword方法的模板会像这样一样:

The template for the lostPassword method could look like this:

 

app/views/Mails/lostPassword.html

 

bc. <html><body><head>...</head><body><img src="mycompany.com/images"/><p>Hello ${user.name},<br/>

Your new password is <b>${newpassword}</b>.

</p>

</body>

</html>

 

自定义的邮件

h3. text/plain e-mail

 

如果没有定义HTML模板,那么自定义的文本邮件会被发送,使用text模板。

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

 

发送器会使用app/views/Mails/welcome.txt模板作为邮件信息的内容。

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

 

bc. Welcome ${user.name},

...

lostPassword方法的模板会像这样一样:

The template for the lostPassword method could look like this:

 

app/views/Mails/lostPassword.txt

 

bc. Hello ${user.name},

 

Your new password is ${newpassword}.

 

从html模板中和自定义文本模板中选择

h3. text/html e-mail with text/plain alternative

 

如果html模板和文本模板都存在,那么文本模板会被一起使用,在你前面的例子中,如果两者都存在,那么会使用定义的HTML模板,并部分使用文本模板。

所以你可以发送友好的HTML邮件,取悦那些还在使用mutt的极客朋友。

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

 

and 

 

app/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 ;)

 

SMTP配置

h2. <a>SMTP configuration</a>

 

首先,你需要定义要使用的SMTP服务器

First of all, you need to define the SMTP server to use:

 

bc. mail.smtp.host=smtp.taldius.net

 

如果你的SMTP服务器需要认证,使用下面的属性

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

 

bc. mail.smtp.user=jfp

mail.smtp.pass=topsecret

 

频道和端口

h3. Channel & ports

 

有2种方法在加密的频道中发送邮件,如果你的服务器支持**starttls**命令,你可以在端口25使用一个清晰的链接,它会切换到SSL/TLS,这些你可以通过增加配置项做到。

There are two ways to send the e-mail over an encrypted channel. If your server supports the **starttls** command (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:

 

bc. mail.smtp.channel=starttls

 

你的服务器也许提供一个SMTP-over-SSL连接,那么SSL Socket会在端口465监听,你可以告诉Play使用下面的配置。

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

 

bc. mail.smtp.channel=ssl

 

更多的配置项

h2. <a>More about configuration</a>

 

在背后,Play使用JavaMail去发送实际的SMTP事务,如果你需要知道实际上发生了什么,试一下:

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

 

bc. mail.debug=true

 

当你使用JavaMail通过SSL连接,如果远程服务器的证书没有被认证的话,默认的SSL行为是断开连接。这是使用自认证的特殊行为。Play的默认行为时跳过那个检查。你可以使用下面的配置控制这些。

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:

 

bc. mail.smtp.socketFactory.class

 

如果你需要使用非标准的端口连接到服务器,那么下面的属性会覆盖默认的配置。

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

 

bc. mail.smtp.port=2500

 

使用Gmail

h2. <a>Using Gmail</a>

 

为了使用Gmail的服务,使用下面的配置。

To use Gmail’s servers, use this configuration: 

 

bc. mail.smtp.host=smtp.gmail.com

mail.smtp.user=yourGmailLogin

mail.smtp.pass=yourGmailPassword

mail.smtp.channel=ssl

 

加载中
0
OTooo
OTooo

支持~!

0
玉良
玉良

都是翻译的原文 我试了试 有问题

Unexpected error : Model models.Message is not managed by any plugin

我看到有人说这样做@MappedSuperclass  对于Message 类 标示了,也没用 Application 的类继承Mailer了   application.conf 的配置mail.smtp.host=smtp.gmail.com
 mail.smtp.user=tuangou591@Gmail.com   mail.smtp.pass=****    mail.smtp.channel=ssl   mail.debug=true

谁能告诉我原因

返回顶部
顶部