Peter,

I'm working on a cookbook entry for processing incoming mail in Java. Here's
some code I had working. Can you take a look at this and see if there's
anything you can adapt?

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;
    }
}



import Image;
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.*;
import javax.mail.util.ByteArrayDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.jdo.PersistenceManager;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.util.Properties;

public class IncomingMailHandlerServlet extends HttpServlet {

    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());

            InputStream inputStream = message.getInputStream();

            ByteArrayDataSource inboundDataSource = new
ByteArrayDataSource(inputStream, message.getContentType());
            Multipart inboundMultipart = new
MimeMultipart(inboundDataSource);

            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"
                    // 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();
                    }

                }

            }

        } catch (MessagingException e) {
            throw new ServletException(e);
        }
    }

}


On Wed, Dec 2, 2009 at 6:00 AM, Peter Ondruska <peter.ondru...@gmail.com>wrote:

> tetest, thank you, the complete working example would be then:
>
>                        MimeMessage message = new MimeMessage(session,
> request.getInputStream());
>
>                        StringBuffer sb = new StringBuffer();
>
>                        sb.append("From: ");
>                        Address[] senders = message.getFrom();
>                        for (int i = 0; i < senders.length; i++)
>                                sb.append(senders[i].toString()).append(";
> ");
>                        sb.append("\n");
>
>                        sb.append("To: ");
>                        Address[] receivers = message.getAllRecipients();
>                        for (int i = 0; i < receivers.length; i++)
>                                sb.append(receivers[i].toString()).append(";
> ");
>                        sb.append("\n");
>
>                        InputStream is = (InputStream) message.getContent();
>                        String contentType = message.getContentType();
>                        ByteArrayDataSource byteArrayDataSource = new
> ByteArrayDataSource
> (is, contentType);
>                        Multipart mmp = new
> MimeMultipart(byteArrayDataSource);
>
>                        for (int i = 0; i < mmp.getCount(); i++) {
>                                Part p = mmp.getBodyPart(i);
>                                if ("text/plain".equals(p.getContentType()))
> {
>                                        if (i > 0) sb.append("*******");
>                                        ByteArrayInputStream bais =
> (ByteArrayInputStream) p.getContent
> ();
>                                        byte[] buffer = new byte[1024];
>                                        int length = 0;
>                                        while ((length = bais.read(buffer))
> != -1)
>                                                sb.append(new String(buffer,
> 0, length));
>                                }
>                        }
>
> Does not work reliably (java.io.IOException: Truncated quoted
> printable data and java.lang.OutOfMemoryError: Java heap space) but
> that is either other bug in GAE or my code.
>
> On Dec 1, 4:38 am, tetest <tet...@gmail.com> wrote:
> > Hi,
> >
> > Follow this thread:
> >
> >
> http://groups.google.com/group/google-appengine-java/browse_thread/th...http://groups.google.com/group/google-appengine-java/browse_thread/th.
> ..
> >
> > Though something only has to be able to be useful.
> >
> > thanks.
> >
> > On 11月30ζ—₯, 午後11:34, Peter Ondruska <peter.ondru...@gmail.com> wrote:
> >
> >
> >
> > > I am following instructions onhttp://
> code.google.com/appengine/docs/java/mail/receiving.html
> > > to process incoming mail but failing on retrieving content (headers
> > > are fine).
> >
> > > "The getContent() method returns an object that implements the
> > > Multipart interface. You can then call getCount() to determine the
> > > number of parts and getBodyPart(int index) to return a particular body
> > > part."
> >
> > > MimeMultipart mmp = (MimeMultipart) message.getContent();
> >
> > > Error:
> > > java.lang.ClassCastException: java.io.ByteArrayInputStream cannot be
> > > cast to javax.mail.internet.MimeMultipart
> >
> > > And when I check at runtime what class is returned by getContent() it
> > > really is java.io.ByteArrayInputStream.
> >
> > > Am I doing something wrong?
>
> --
>
> 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
> google-appengine-j...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengine-java+unsubscr...@googlegroups.com<google-appengine-java%2bunsubscr...@googlegroups.com>
> .
> 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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.


Reply via email to