Sorry, been sick for a while. Here is the complete source working on
1.3 release of GAE/J:

public class MailHandler extends HttpServlet {

        private static final long serialVersionUID = 895612806305910032L;

        private final Logger logger = Logger.getLogger(getClass().getName());

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

                final Session session = Session.getDefaultInstance(new 
Properties(),
null);

                try {

                        final MimeMessage message = new MimeMessage(session,
request.getInputStream());

                        final StringBuffer sb = new StringBuffer();

                        sb.append("From: ");
                        final Address[] senders = message.getFrom();
                        for (int i = 0; i < senders.length; i++)
                                sb.append(senders[i].toString()).append("; ");
                        sb.append("\n");

                        sb.append("To: ");
                        final Address[] receivers = message.getAllRecipients();
                        for (int i = 0; i < receivers.length; i++)
                                sb.append(receivers[i].toString()).append("; ");
                        sb.append("\n");

                        final Object content = message.getContent();
                        if (content instanceof String)
                                sb.append((String) content);
                        else if (content instanceof MimeMultipart) {
                                final MimeMultipart mmp = (MimeMultipart) 
content;
                                for (int i = 0; i < mmp.getCount(); i++) {
                                        final Part p = mmp.getBodyPart(i);
                                        final Object bp = p.getContent();
                                        if (bp instanceof String) 
sb.append((String) bp);
                                }
                        }

                        final TaskOptions taskOptions = 
TaskOptions.Builder.url("***");
                        taskOptions.param("sender", "***");
                        taskOptions.param("receiver", "***");
                        taskOptions.param("subject", "FW: " + 
message.getSubject());
                        taskOptions.param("body", sb.toString());
                        QueueFactory.getDefaultQueue().add(taskOptions);

                } catch (MessagingException e) {
                        logger.log(Level.SEVERE, null, e);
                }

        }
}


On Dec 9, 11:29 pm, "Ikai L (Google)" <[email protected]> wrote:
> Peter, I'm hoping this solved your issue with CIDs and inline image
> attachments. If you can, it'd be awesome if you could post some sample code
> for the group.
>
> On Wed, Dec 9, 2009 at 2:02 PM, Peter Ondruska 
> <[email protected]>wrote:
>
>
>
>
>
> > Thank you Jeremy, Ikai, and tetest. I was able to finish incoming mail
> > handler in Java and it works fine now. I has been confusing due to
> > changes in 1.2.7 and 1.2.8 releases. Peter
>
> > On Dec 9, 10:52 pm, "Ikai L (Google)" <[email protected]> wrote:
> > > I've just tested this code out with Yahoo, Gmail and Hotmail for
> > processing
> > > an attachment. It's working well. Thanks to Jeremy Blythe for looking
> > into
> > > this and posting about it! I'll add this to the cookbook when I get a
> > > chance.
>
> > > // IncomingMailHandlerServlet.java
>
> > > import com.google.appengine.api.datastore.Blob;
>
> > > import javax.servlet.http.HttpServlet;
> > > import javax.servlet.http.HttpServletRequest;
> > > import javax.servlet.http.HttpServletResponse;
> > > import javax.servlet.ServletException;
> > > import javax.mail.internet.MimeMessage;
> > > import javax.mail.internet.MimeBodyPart;
> > > import javax.mail.internet.MimeMultipart;
> > > import javax.mail.Session;
> > > import javax.mail.MessagingException;
> > > import javax.mail.Multipart;
> > > import javax.mail.BodyPart;
> > > import javax.jdo.PersistenceManager;
> > > import java.io.IOException;
> > > import java.io.ByteArrayOutputStream;
> > > import java.io.InputStream;
> > > import java.util.Properties;
> > > import java.util.logging.Logger;
>
> > > import javatest.PMF;
> > > import javatest.models.Image;
>
> > > public class IncomingMailHandlerServlet extends HttpServlet {
>
> > >     private static final Logger log =
> > > Logger.getLogger(IncomingMailHandlerServlet.class.getName());
>
> > >     protected void doPost(HttpServletRequest request, HttpServletResponse
> > > response) throws ServletException, IOException {
> > >         Properties props = new Properties();
> > >         Session session = Session.getDefaultInstance(props, null);
> > >         try {
> > >             MimeMessage message = new MimeMessage(session,
> > > request.getInputStream());
> > >             Object content = message.getContent();
>
> > >             if (content instanceof String) {
> > >                 // do something
> > >             } else if (content instanceof Multipart) {
> > >                 MimeMultipart inboundMultipart = (MimeMultipart) content;
> > >                 for (int i = 0; i < inboundMultipart.getCount(); i++) {
> > >                     BodyPart part = inboundMultipart.getBodyPart(i);
>
> > >                     if (part.getDisposition() == null) {
> > >                         // This is just a plain text part
> > >                     } else if
> > (part.getDisposition().equals("attachment")) {
> > >                         // Create a new ByteArrayDataSource with this
> > part
> > >                         MimeBodyPart inboundMimeBodyPart = (MimeBodyPart)
> > > part;
>
> > >                         // The call to getContentType here may return a
> > > filename along with content type. Ex:
> > >                         //     image/jpeg; name="filename.jpg"
> > >                         //     Gmail and Yahoo add the filename, Hotmail
> > > does not
> > >                         // It doesn't seem to affect display in a browser
> > > but you may wish to sanitize it
> > >                         String contentType =
> > > inboundMimeBodyPart.getContentType();
>
> > >                         InputStream is = part.getInputStream();
>
> > >                         byte[] rawData, buffer = new byte[8192];
> > >                         int len;
> > >                         ByteArrayOutputStream output = new
> > > ByteArrayOutputStream();
>
> > >                         try {
> > >                             while ((len = is.read(buffer, 0,
> > buffer.length))
> > > != -1) output.write(buffer, 0, len);
> > >                             rawData = output.toByteArray();
> > >                         } finally {
> > >                             output.close();
> > >                         }
>
> > >                         PersistenceManager pm =
> > > PMF.get().getPersistenceManager();
>
> > >                         Image image = new Image(new Blob(rawData),
> > > contentType);
> > >                         try {
> > >                             pm.makePersistent(image);
> > >                         } finally {
> > >                             pm.close();
> > >                         }
>
> > >                     }
>
> > >                 }
>
> > >             } else {
> > >                 // We got something weird, shouldn't ever get here. Let's
> > > add some logging
> > >             }
>
> > >         } catch (MessagingException e) {
> > >             // do something
> > >         }
> > >     }
>
> > > }
>
> > > // Image.java
> > > import com.google.appengine.api.datastore.Blob;
> > > import com.google.appengine.api.datastore.Key;
>
> > > import javax.jdo.annotations.*;
>
> > > @PersistenceCapable(identityType = IdentityType.APPLICATION)
> > > public class Image {
>
> > >     @PrimaryKey
> > >     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > >     private Key key;
>
> > >     @Persistent
> > >     private Blob data;
>
> > >     @Persistent
> > >     private String contentType;
>
> > >     public Image(Blob data, String contentType) {
> > >         this.data = data;
> > >         this.contentType = contentType;
> > >     }
>
> > >     public Key getKey() {
> > >         return key;
> > >     }
>
> > >     public Blob getData() {
> > >         return data;
> > >     }
>
> > >     public void setData(Blob data) {
> > >         this.data = data;
> > >     }
>
> > >     public String getContentType() {
> > >         return contentType;
> > >     }
>
> > >     public void setContentType(String contentType) {
> > >         this.contentType = contentType;
> > >     }
>
> > > }
>
> > > On Mon, Dec 7, 2009 at 5:47 PM, Ikai L (Google) <[email protected]>
> > wrote:
>
> > > > Sam,
>
> > > > I understand your confusion. I'm looking into it. The code I posted was
> > to
> > > > convert an InputStream to the proper format. My understanding is that
> > 1.2.8
> > > > actually fixed the issue, so our code should actually end up being
> > simpler
> > > > and closer adhere to standard Java incoming mail processing.
>
> > > > Any help the community provides would be much appreciated! The faster
> > we
> > > > can all get to the bottom of this, the faster I can get the docs
> > updated.
>
> > > > On Sun, Dec 6, 2009 at 2:40 AM, Sam <[email protected]> wrote:
>
> > > >> Appengine team, can someone please post (on the incoming mail docs
> > > >> page) a full example of processing an incoming email in appengine
> > > >> 1.2.8 including getting the message body and attachment?  Please test
> > > >> from different mail clients.  Everyone is totally confused here,
> > > >> especially with the mysterious API "fix" in 1.2.8 that broke all
> > > >> incoming mail code.  Thanks.
>
> > > >> --
>
> > > >> You received this message because you are subscribed to the Google
> > Groups
> > > >> "Google App Engine for Java" group.
> > > >> To post to this group, send email to
> > > >> [email protected].
> > > >> To unsubscribe from this group, send email to
> > > >> [email protected]<google-appengine-java%2B
> > > >>  [email protected]><google-appengine-java%2B
> > [email protected]>
> > > >> .
> > > >> For more options, visit this group at
> > > >>http://groups.google.com/group/google-appengine-java?hl=en.
>
> > > > --
> > > > Ikai Lan
> > > > Developer Programs Engineer, Google App Engine
>
> > > --
> > > Ikai Lan
> > > Developer Programs Engineer, Google App Engine
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Google App Engine for Java" group.
> > To post to this group, send email to
> > [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<google-appengine-java%2B 
> > [email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-appengine-java?hl=en.
>
> --
> Ikai Lan
> Developer Programs Engineer, Google App Engine

--

You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.


Reply via email to