Ikai, my code looks same as yours to me:
package eu.kaibo.server;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions;
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 {
Session session = Session.getDefaultInstance(new Properties(),
null);
try {
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");
line 55: InputStream is = (InputStream)
message.getContent();
ByteArrayDataSource byteArrayDataSource = new
ByteArrayDataSource
(is, message.getContentType());
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));
}
}
TaskOptions taskOptions =
TaskOptions.Builder.url("/worker/mail");
taskOptions.param("sender", "x");
taskOptions.param("receiver", "x");
taskOptions.param("subject", "FW: " +
message.getSubject());
taskOptions.param("body", sb.toString());
QueueFactory.getDefaultQueue().add(taskOptions);
} catch (MessagingException e) {
logger.log(Level.SEVERE, null, e);
}
}
}
I am still getting exception:
Uncaught exception from servlet
java.io.IOException: Truncated quoted printable data
at
org.apache.geronimo.mail.util.QuotedPrintableEncoder.decodeNonspaceChar
(QuotedPrintableEncoder.java:597)
at org.apache.geronimo.mail.util.QuotedPrintableEncoder.decode
(QuotedPrintableEncoder.java:584)
at org.apache.geronimo.mail.util.QuotedPrintableDecoderStream.read
(QuotedPrintableDecoderStream.java:80)
at org.apache.geronimo.mail.handlers.TextHandler.getContent
(TextHandler.java:107)
at javax.activation.DataSourceDataContentHandler.getContent(Unknown
Source)
at javax.activation.DataHandler.getContent(Unknown Source)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:927)
at eu.kaibo.server.MailHandler.doPost(MailHandler.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
On Dec 2, 7:08 pm, "Ikai L (Google)" <[email protected]> wrote:
> 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
> <[email protected]>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 <[email protected]> wrote:
> > > Hi,
>
> > > Follow this thread:
>
> >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 <[email protected]> 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
> > [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.