Java Reference
In-Depth Information
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class MailServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String messageBody = "What do you think about the topic. You
can reply to this and I'll get it.";
try {
Message emailMessage = new MimeMessage(session);
emailMessage.setFrom(new
InternetAddress("kyle.m. roche@gmail.com", " The Author"));
emailMessage.addRecipient(Message.RecipientType.TO, new
InternetAddress("", "The Reader"));
emailMessage.setSubject("How's the topic?");
emailMessage.setText(messageBody);
Transport.send(emailMessage);
resp.getOutputStream().println("Message sent!");
} catch (AddressException e) {
} catch (MessagingException e) {
}
}
}
Let's review the code before you deploy and test the application. The application's
entire logic lives in the doGet method of the servlet's class. This means that all the
code will execute as soon as a user browses to this page of the application. Inside
the try / catch block you are creating a new instance of the Message class, and then
passing it to the Transport.send method to initiate the sending of the message. Since,
at the time of this writing, Google restricts each user to only 10 deployed applications
on App Engine, you might not want to create a new application ID and deploy this
example. However, you can always reuse an application ID from a previous chapter
to test out the Mail service in a deployed application. Figure 8-9 shows the e-mail
message sent from a deployed copy of this servlet.
 
Search WWH ::




Custom Search