Advertisement
crowquine

Email handling in Google App Engine for Java

Jun 27th, 2012
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. package com.yourappname;
  2.  
  3. import java.io.IOException;
  4. import java.text.DateFormat;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7.  
  8. import javax.mail.BodyPart;
  9. import javax.mail.Message;
  10. import javax.mail.MessagingException;
  11. import javax.mail.Multipart;
  12. import javax.mail.internet.MimeMessage;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. @SuppressWarnings("serial")
  18. public class MailHandlerServlet extends HttpServlet {
  19.     public void doPost(HttpServletRequest req, HttpServletResponse resp)
  20.             throws IOException {
  21.         try {
  22.             MimeMessage message = MimeUtils.createMimeMessage(req);
  23.  
  24.             if (processMessage(message)) {
  25.                 Debug.log("Incoming email handled");
  26.             } else {
  27.                 Debug.log("Failed to handle incoming email");
  28.             }
  29.         } catch (MessagingException e) {
  30.             Debug.log("MessagingException: " + e);
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.    
  35.     private boolean processMessage(MimeMessage message) {
  36.         String date = getMessageDate(message);
  37.         String from = "unknown";
  38.  
  39.         try {
  40.             from = message.getFrom()[0].toString();
  41.             Object content = MimeUtils.getContent(message);
  42.  
  43.             if (message.getContentType().startsWith("text/plain")) {
  44.                 processMail(from, date, (String) content);
  45.                 return true;
  46.             } else if (content instanceof Multipart) {
  47.                 Multipart mp = (Multipart) content;
  48.                 for (int i = 0; i < mp.getCount(); i++) {
  49.                     if (handlePart(from, date, mp.getBodyPart(i))) {
  50.                         return true;
  51.                     }
  52.                 }
  53.                 return false;
  54.             } else {
  55.                 Debug.log("Unable to process message content - unknown content type");
  56.             }
  57.         } catch (IOException e) {
  58.             Debug.log("Exception handling incoming email " + e);
  59.         } catch (MessagingException e) {
  60.             Debug.log("Exception handling incoming email " + e);
  61.         } catch (Exception e) {
  62.             Debug.log("Exception handling incoming email " + e);
  63.         }
  64.  
  65.         return false;
  66.     }
  67.    
  68.     private boolean handlePart(String from, String date, BodyPart part)
  69.             throws MessagingException, IOException {
  70.         if (part.getContentType().startsWith("text/plain")
  71.                 || part.getContentType().startsWith("text/html")) {
  72.             processMail(from, date, (String) part.getContent());
  73.             return true;
  74.         } else {
  75.             if (part.getContent() instanceof Multipart) {
  76.                 Multipart mp = (Multipart) part.getContent();
  77.                 Debug.log("Handling a multipart sub-message with " + mp.getCount() + " sub-parts");
  78.                 for (int i = 0; i < mp.getCount(); i++) {
  79.                     if (handlePart(from, date, mp.getBodyPart(i))) {
  80.                         return true;
  81.                     }
  82.                 }
  83.                 Debug.log("No text or HTML part in the multipart mime sub-message");
  84.             }
  85.             return false;
  86.         }
  87.     }
  88.    
  89.     private String getMessageDate(Message message) {
  90.         Date when = null;
  91.         try {
  92.             when = message.getReceivedDate();
  93.             if (when == null) {
  94.                 when = message.getSentDate();
  95.             }
  96.             if (when == null) {
  97.                 return null;
  98.             }
  99.         } catch (MessagingException e) {
  100.             Debug.log("Cannot get message date: " + e);
  101.             e.printStackTrace();
  102.             return null;
  103.         }
  104.  
  105.         DateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
  106.         return format.format(when);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement