Google App Engine and sending Emails

One of the nice things in the Google App Engine is that is it very easy to work with emails. You can send out and receive emails in your servlets.

Lets demonstrate this by defining an application which allows the user to give some feedback. The backend servlet should receive this input and send out an email to the admin of the application.

For this we create a new App Engine project “” and create a simple form.

<!DOCTYPE html>
  <meta charset="utf-8"> 
  <title>A form</title>
 <form action="feedback" method="post">
  <!-- Simple text field -->
 <label for="name">Name </label> 
 <input type="text" name="name"/>
  <!-- Email -->
 <label for="email">Email </label> 
 <input type="email" name="email"/>

  <!-- Textarea -->
 <label for="description">Description </label> 
 <textarea  name="description" cols="50" rows="5">Type your comment here</textarea>
 <input type="submit" name="submit" value="Send Request"/>

Then we define a Servlet under the URL /feedback which will receive the input from the form and send out an email.


import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FeedbackServlet extends HttpServlet {

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("name");
		String description = req.getParameter("description");
		String email = req.getParameter("email");
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String msgBody = name + "\n" + description + "\n" + email;

		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("",
					"Your admin"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					"", "Your name

		} catch (Exception e) {
			resp.getWriter().println("Something went wrong. Please try again.");
			throw new RuntimeException(e);

				"Thank you for your feedback. An Email has been send out.");

You have to make sure that the from email is the admin of the application otherwise you will receive an “Unauthorized Sender” Exception. If you deploy your application, access index.html, enter some data and press submit the person maintained in your recipient should receive an email with the feedback.

You find an example how to receive email in my Google App Engine Tutorial. You should also follow me on Twitter.

This entry was posted in Web and tagged , . Bookmark the permalink.

3 Responses to Google App Engine and sending Emails

  1. Coder says:

    So what’s the GAE specific thing here?

  2. Lars Vogel says:

    Thats the cool thing. You use the standard javax.mail API and you can send out emails without any email server configuration.

  3. Jyothi PK says:


    I am a regular follower of your blog and I like the way concepts are presented..Neat and clear … Indeed it is a very good blog for those working on Android and I highly recommend your blog to understand the fundamentals.

Comments are closed.