This is a mirror of official site: http://jasper-net.blogspot.com/

Google App Engine and sending Emails

| Sunday, February 6, 2011
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 “de.vogella.gae.feedback” and create a simple form.

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>A form</title>
</head>
<body>
<form action="feedback" method="post">

 <!-- Simple text field -->
<label for="name">Name </label>
<input type="text" name="name"/>
<br/>

 <!-- Email -->
<label for="email">Email </label>
<input type="email" name="email"/>
<br/>

 <!-- Textarea -->
<label for="description">Description </label>
<textarea  name="description" cols="50" rows="5">Type your comment here</textarea>
<br/>
...


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

package de.vogella.gae.feedback;

import java.io.IOException;
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;

@SuppressWarnings("serial")
public class FeedbackServlet extends HttpServlet {

   @Override
   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);
...

Read more:  Developer Papercuts

Posted via email from Jasper-net

0 comments: