English

Mail Java API Overview

App Engine applications can send email messages on behalf of the app's administrators, and on behalf of users with Google Accounts. Apps use the Mail service to send email messages.

Sending Mail with the JavaMail API

The Mail service Java API supports the JavaMail (javax.mail) interface for sending email messages.

When creating a JavaMail Session, you do not need to provide any SMTP server configuration. App Engine will always use the Mail service for sending messages.

import java.util.Properties;
import javax.mail.AddressException;
import javax.mail.InternetAddress;
import javax.mail.MessagingException;
import javax.mail.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;

// ...
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "...";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("admin@example.com"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("user@example.com", "Mr. User"));
            msg.setSubject("Your Example.com account has been activated");
            msg.setText(msgBody);
            Transport.send(msg);
    
        } catch (AddressException e) {
            // ...
        } catch (MessagingException e) {
            // ...
        }

Email Messages

The Mail service can send email messages to one or more recipients. The message contains a subject, a plaintext body, and an optional HTML body. It can also contain file attachments.

For security purposes, the sender address of a message must be the email address of an administrator for the application, or the Google Account email address of the current user who is signed in. The email address can include a "reply to" address, which must also meet these restrictions.

If you want to send email on behalf of the application but do not want to use a single administrator's personal Google Account as the sender, you can create a new Google Account for the application using any valid email address, then add the new account as an administrator for the application. To add an account as an administrator, see the "Developers" section of the Admin Console.

You can use any email address for a recipient. A recipient can be in the message's "to" field or the "cc" field, or the recipient can be hidden from the message header (a "blind carbon copy" or "bcc").

Attachments

An email message can have zero or more file attachments.

An attachment has a filename and file data. The file data can come from any source, such as an application data file or the datastore. The MIME type of the attachment is determined from the filename.

For security purposes, a file attached to an email messages must be of one of the allowed file types, and its filename must end with an extension that corresponds with its type.

The following is a list of MIME types and their corresponding filename extensions allowed for file attachments to an email message.

MIME TypeFilename Extensions
image/x-ms-bmpbmp
text/csscss
text/comma-separated-valuescsv
image/gifgif
text/htmlhtm html
image/jpegjpeg jpg jpe
application/pdfpdf
image/pngpng
application/rss+xmlrss
text/plaintext txt asc diff pot
image/tifftiff tif
image/vnd.wap.wbmpwbmp
text/calendarics
text/x-vcardvcf

Sending Mail

When an application calls the Mail service to send a message, the message is queued and the call returns immediately. The Mail service uses standard procedures for contacting each recipient's mail server, delivering the message, and retrying if the mail server cannot be contacted.

If the Mail service cannot deliver a message, or if an recipient's mail server returns a bounce message (such as if there is no account for that address on that system), the error message is sent by email to the address of the sender for the message. The application itself does not receive any notification about whether delivery succeeded or failed.

Mail and the Development Server

When an application running in the development server calls the Mail service to send an email message, the message is printed to the log. The Java development server does not send the email message.

Quotas and Limits

Each Mail service request counts toward the Mail API Calls quota.

Each recipient email address for an email message counts toward the Recipients Emailed (adjustable) quota. Each recipient that is an administrator for the application also counts toward the Admins Emailed quota.

Data sent in the body of an email message counts toward the following quotas:

  • Outgoing Bandwidth (adjustable)
  • Message Body Data Sent

Each attachment included with an email message counts toward the Attachments Sent quota.

Data sent as an attachment to an email message counts toward the following quotas:

  • Outgoing Bandwidth (adjustable)
  • Attachment Data Sent

For more information on quotas, see Quotas, and the "Quota Details" section of the Admin Console.

In addition to quotas, the following limits apply to the use of the Mail service:

Limit Amount
maximum size of message, including attachments 1 megabyte
maximum size of message when an administrator is a recipient 16 kilobytes