English ▼
Skip to page content Skip to main navigation
e.g. "templates" or "datastore"

# Receiving Email

Email messages sent to your app are implemented as HTTP requests. To process incoming email messages, you associate email addresses with servlets in your app configuration, then include the servlet code with your app. Incoming email generates HTTP requests, which are passed to the appropriate servlets for handling.

## Configuring Your App to Receive Email

When you create a new app, incoming email is disabled by default. To enable the incoming email service, you must modify your app's configuration files in two ways:

1. In `appengine-web.xml`, add an `inbound-services` section that enables the incoming email service.
2. In `web.xml`, add mappings that associate URL-mapped email addresses with servlets.

To enable the incoming email service, add a short section to the root element of your `appengine-web.xml` file, like the following:

    <inbound-services>
      <service>mail</service>
    </inbound-services>

You can verify that you have enabled incoming email in your app by going to the Administration Console and checking the Application Settings section. Note that you can only check there to see if the incoming email service is enabled; you must change the `appengine-web.xml` file to actually enable or disable the service. If you don't enable incoming email by including this section in your configuration file, incoming email is disabled, and email messages sent to the app are ignored.

Your app can receive email at addresses of the following form:

      string@appid.appspotmail.com

Email messages are sent to your app as HTTP POST requests using the following URL:

      /_ah/mail/<address>

where address is a full email address, including domain name. To handle incoming email in your app, you map email URLs to servlets in the `web.xml` file:

    <servlet>
      <servlet-name>mailhandler</servlet-name>
      <servlet-class>MailHandlerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>mailhandler</servlet-name>
      <url-pattern>/_ah/mail/*</url-pattern>
    </servlet-mapping>
    <security-constraint>
      <web-resource-collection>
        <url-pattern>/_ah/mail/*</url-pattern>
      </web-resource-collection>
      <auth-constraint>
        <role-name>admin</role-name>
      </auth-constraint>
    </security-constraint>

In this example, /_ah/mail/* matches all email addressed to the app.

If you prefer, you can set up multiple handlers for different email addresses, as in the following example:

    <servlet>
      <servlet-name>handleowner</servlet-name>
      <servlet-class>HandleOwner</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>handleowner</servlet-name>
      <url-pattern>/_ah/mail/owner*</url-pattern>
    </servlet-mapping>
    <servlet>
      <servlet-name>handlesupport</servlet-name>
      <servlet-class>HandleSupport</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>handleowner</servlet-name>
      <url-pattern>/_ah/mail/support*</url-pattern>
    </servlet-mapping>
    <servlet>
      <servlet-name>catchallhandler</servlet-name>
      <servlet-class>MailCatchallServlet</servlet-class>
    </servlet>
    <servlet-mapping>
      <servlet-name>catchallhandler</servlet-name>
      <url-pattern>/_ah/mail/*</url-pattern>
    </servlet-mapping>
    <security-constraint>
      <web-resource-collection>
        <url-pattern>/_ah/mail/*</url-pattern>
      </web-resource-collection>
      <auth-constraint>
        <role-name>admin</role-name>
      </auth-constraint>
    </security-constraint>

URLs of incoming email messages are matched to this list from first to last, so if an email message URL matches more than one pattern, the first matching handler will be the one executed. This allows you to include a "catchall" handler as the last mapping.

## Handling Incoming Email

The JavaMail API includes the `MimeMessage` class which you can use to parse incoming email messages. `MimeMessage` has a constructor that accepts a `java.io.InputStream` and a JavaMail session, which can have an empty configuration.

You can create a `MimeMessage` instance like this:

    import java.io.IOException;
    import java.util.Properties;
    import javax.mail.Session;
    import javax.mail.internet.MimeMessage;
    import javax.servlet.http.*;

    public class MailHandlerServlet extends HttpServlet {
        public void doPost(HttpServletRequest req,
                           HttpServletResponse resp)
                throws IOException {
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session, req.getInputStream());

You can then use various methods to parse the `message` object:

- Call `getFrom()` to return the sender of the message.
Call `getContentType()` to extract the message content type.
- The `getContent()` method returns an object that implements the `Multipart` interface. You can then call `getCount()` to determine the number of parts and getBodyPart(int index) to return a particular body part.

Once you set up your app to handle incoming email, you can use the development server console to simulate incoming email messages. You can access the development server by going to http://localhost:8888/_ah/admin/ (or if your app is running on a port other than 8888, use that value instead). In the development server, click Inbound Mail on the left side, fill out the form that appears, and click Send Email. To learn more, including how to get the development server running, see The Java Development Server.