首页 技术 正文
技术 2022年11月11日
0 收藏 398 点赞 3,213 浏览 4828 个字

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("https://img.zhankr.net/djg202xvwmm94501.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.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289