Sending e-mail

Source: Internet
Author: User

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:

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 idURL url = new URL("http://www.zenexity.fr/wp-content/themes/images/logo.png");String cid = email.embed(url, "Zenexity logo");// set the html messageemail.setHtmlMsg("

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 is part of the notifiers package.

Each public static method would be a e-mail sender, in a similar manner as the 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 would render the app/views/mails/welcome.html template as the e-mail message body.

The template for the LostPassword method could:

App/views/mails/lostpassword.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 would render the app/views/mails/welcome.txt template as the e-mail message body.

Welcome ${user.name},...

The template for the LostPassword method could:

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 was defined and a text template exists, then the text template would be used as an alternative message. In our previous example, if both app/views/mails/lostpassword.html andapp/views/mails/lostpassword.txt is defined, then the e-mail would be sent in text/html as defined in lostpassword.html with a alternative part as D Efined in LostPassword.txt. So your 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 as this:

@@{application.index}

If you send the mails from Jobs, you had to configure Application.baseurl in application.conf.
Application.baseurl must is a valid external BASEURL to your application.

If the website playframework.org where to send your 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=jfpmail.smtp.pass=topsecret
Channel & Ports

There is 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 Switch to SSL/TLS. Adding this configuration option:

mail.smtp.channel=starttls

Your server may also provide a SMTP-OVER-SSL (SMTPS) connector, which is the SSL socket listening on port 465. In the so 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 are to drop the connection if the remote server certific Ate is not signed by a root certificate. The 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 would override the defaults:

mail.smtp.port=2500
Using Gmail

To use Gmail's servers, use the This configuration:

mail.smtp.host=smtp.gmail.commail.smtp.user=yourGmailLoginmail.smtp.pass=yourGmailPasswordmail.smtp.channel=ssl

Continuing the discussion

Now we shall move on to testing the application.

Sending e-mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.