Hi.Peter,
As one example.
When you use
----------------------------------------------------------------------------
InputStream inputStream = message.getInputStream(); //
ByteArrayDataSource inboundDataSource = new
ByteArrayDataSource(inputStream, message.getContentType());
Multipart inboundMultipart = new MimeMultipart(inboundDataSource);
----------------------------------------------------------------------------
Please switch the processing of each type by using
MimeMessage#isMimeType.
(In the offered source it is variable [message]. )
MimeMessage#isMimeType("multipart/*") == true
Convert into MimeMultipart.
MimeMessage#isMimeType("text/plain") == true
MimeMessage#isMimeType("text/html") == true
Need not convert into MimeMultipart.
Of course,
Jeremy Blythe have provided us information that [contentType.indexOf
( "multipart")] also Behave the same.
Thanks Jeremy Blythe.
Ikai.
Thank you variously for useful information as for processing
concerning the attached file to datastore ,etc.
Though it noticed now.
MimeMessage#isMimeType("multipart/*") == true
The return value of MimeMessage#getContent() seems to have changed
into Multipart. (SDK-1.2.8)
(The real thing environment has been updated recently. )
http://code.google.com/p/googleappengine/wiki/SdkForJavaReleaseNotes
When you use [message.getContent() ]
Such a method might be able to be used.
Object content = message.getContent();
if ( content instanceof Multipart){
MimeMultipart mmp = (MimeMultipart)content;
}
else{
//plain text/html part .etc
}
Though there might be an insufficient condition.
Please try.
thanks.
On 12月5日, 午前3:24, Peter Ondruska <[email protected]> wrote:
> OK. now I am really puzzled. message.getContent() is very much
> dependent on how message is sent, e.g. email from Gmail:
> message.getContent().getClass().getName() is
> javax.mail.internet.MimeMultipart
>
> email from Hotmail and Yahoo mail:
> message.getContent().getClass().getName() throws:
> 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)
>
> On Dec 4, 6:38 pm, Peter Ondruska <[email protected]> wrote:
>
> > 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
>
> ...
>
> もっと読む ≫
--
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.