Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.yourappname;
- import java.io.IOException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.mail.BodyPart;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.internet.MimeMessage;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- @SuppressWarnings("serial")
- public class MailHandlerServlet extends HttpServlet {
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws IOException {
- try {
- MimeMessage message = MimeUtils.createMimeMessage(req);
- if (processMessage(message)) {
- Debug.log("Incoming email handled");
- } else {
- Debug.log("Failed to handle incoming email");
- }
- } catch (MessagingException e) {
- Debug.log("MessagingException: " + e);
- e.printStackTrace();
- }
- }
- private boolean processMessage(MimeMessage message) {
- String date = getMessageDate(message);
- String from = "unknown";
- try {
- from = message.getFrom()[0].toString();
- Object content = MimeUtils.getContent(message);
- if (message.getContentType().startsWith("text/plain")) {
- processMail(from, date, (String) content);
- return true;
- } else if (content instanceof Multipart) {
- Multipart mp = (Multipart) content;
- for (int i = 0; i < mp.getCount(); i++) {
- if (handlePart(from, date, mp.getBodyPart(i))) {
- return true;
- }
- }
- return false;
- } else {
- Debug.log("Unable to process message content - unknown content type");
- }
- } catch (IOException e) {
- Debug.log("Exception handling incoming email " + e);
- } catch (MessagingException e) {
- Debug.log("Exception handling incoming email " + e);
- } catch (Exception e) {
- Debug.log("Exception handling incoming email " + e);
- }
- return false;
- }
- private boolean handlePart(String from, String date, BodyPart part)
- throws MessagingException, IOException {
- if (part.getContentType().startsWith("text/plain")
- || part.getContentType().startsWith("text/html")) {
- processMail(from, date, (String) part.getContent());
- return true;
- } else {
- if (part.getContent() instanceof Multipart) {
- Multipart mp = (Multipart) part.getContent();
- Debug.log("Handling a multipart sub-message with " + mp.getCount() + " sub-parts");
- for (int i = 0; i < mp.getCount(); i++) {
- if (handlePart(from, date, mp.getBodyPart(i))) {
- return true;
- }
- }
- Debug.log("No text or HTML part in the multipart mime sub-message");
- }
- return false;
- }
- }
- private String getMessageDate(Message message) {
- Date when = null;
- try {
- when = message.getReceivedDate();
- if (when == null) {
- when = message.getSentDate();
- }
- if (when == null) {
- return null;
- }
- } catch (MessagingException e) {
- Debug.log("Cannot get message date: " + e);
- e.printStackTrace();
- return null;
- }
- DateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
- return format.format(when);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement