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]> > >> . > >> 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]. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
