Email messages are sent to your app as HTTP requests. To process incoming email messages, you must associate email addresses with servlets in your application configuration, and then include the servlet code with your app. The incoming email generates HTTP requests that are passed to the appropriate servlets for handling.
Configuring your application to receive email
When you create a new application, incoming email is disabled by default. If you don't explicitly enable incoming email messages sent to your app are ignored.
To enable the incoming email service, modify the appengine-web.xml
and
web.xml
configuration files:
Enabling email in appengine-web.xml
Modify appengine-web.xml
by adding an inbound-services
section that
enables the incoming email service:
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. Note that even
if your app is deployed on a custom domain, your app can't receive email sent to
addresses in that domain.
Enabling email in web.xml
Modify web.xml
by mapping email URLs to servlets:
In the above snippets, /_ah/mail/*
matches all email addressed to the app.
Mail servlets run in the currently serving version of your app in App Engine.
Pattern-based dispatching of incoming messages
If your app uses pattern matching, consider using a filter-based approach based on the following code snippets.
Concrete handler
The above concrete handler is registered using the following snippet in
web.xml
:
Note that security-constraint
directives are not possible on filters;
security policies on the handler will have to be introduced some other way.
Abstract handler
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.
Create a MimeMessage
instance like this:
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. ThegetContent()
method returns an object that implements theMultipart
interface. - Call
getCount()
to determine the number of parts - Call
getBodyPart(int index)
to return a particular body part.
After you set up your app to handle incoming email, you can use the development server console to simulate incoming email messages. To learn more, including how to start the development server, see The Java Development Server. After you start your application in the local development server, you can access your application by visiting the URL http://localhost:8888/_ah/admin/, replacing the value 8888 with whatever port you are using if you don't use the default port for the local development server.
In the development server, click Inbound Mail on the left side, fill out the form that appears, and click Send Email.