Hi Ikai,

I tried your example code, but I cannot attach an image on the email
that I send.
There is no conversion error either.

Here is snippets of the code, please help..

//Retrieving image:
URL     url = new URL("http://stockcharts.com/c-sc/sc?s="; + ticker +
"&p=DAILY&b=5&g=0&i=0&r=3528");
//Sending mail
SendMail sendmail = new SendMail(customerEmail, url.openStream());

// send mail function
public SendMail(String recipient, InputStream imagestream) {
msg.setFrom( new InternetAddress("lydonchan...@gmail.com", "dons
service"));
                        msg.addRecipient(Message.RecipientType.TO, new 
InternetAddress
(recipient));
                        msg.setSubject("dons service daily charts");

                        Multipart multipart = new MimeMultipart();

                        MimeBodyPart htmlPart = new MimeBodyPart();
                        htmlPart.setContent(" url2: " , "text/html");
                        multipart.addBodyPart(htmlPart);

                        // append png image?
                        ByteArrayDataSource mimePartDataSource = new 
ByteArrayDataSource
(imagestream, "image/png");

                        MimeBodyPart attachment = new MimeBodyPart();
                        attachment.setDataHandler(new 
DataHandler(mimePartDataSource));
                        attachment.setFileName("ticker.png");
                        multipart.addBodyPart(attachment);
                        msg.setContent(multipart);

                        Transport.send(msg);
}


On Nov 17, 5:35 am, "Ikai L (Google)" <ika...@google.com> wrote:
> I couldn't reproduce your exact error, but I was able to put together a
> working example of an inbound email handler to relay messages. I'm going to
> expand the documentation about processing inbound emails. Here's some
> working code:http://pastie.org/701517
>
> Does this example help any? Code is also pasted below, but it'll be easier
> for you to look at the Pastie.
>
> 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.MimeMessage;
> import javax.mail.internet.MimeMultipart;
> import javax.mail.internet.MimeBodyPart;
> import javax.mail.internet.InternetAddress;
> import javax.activation.DataHandler;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.logging.Logger;
> import java.util.Properties;
>
> public class MailHandlerServlet extends HttpServlet {
>     private static final Logger log =
> Logger.getLogger(MailHandlerServlet.class.getName());
>     private static final String RECIPIENT = "recipi...@gmail.com";
>     private static final String SENDER = "sen...@google.com";
>
>     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(); // You could also
> probably just use message.getInputStream() here
>                                                    // and avoid the
> conditional type check
>
>             if (content instanceof String) {
>                 log.info("Received a string");
>             } else if (content instanceof InputStream) {
>                 // My somewhat limited testing indicates that this is always
> getting returned as an
>                 // InputStream
>
>                 InputStream inputStream = (InputStream) content;
>                 ByteArrayDataSource inboundDataSource = new
> ByteArrayDataSource(inputStream, message.getContentType());
>                 Multipart inboundMultipart = new
> MimeMultipart(inboundDataSource);
>
>                 // Set the body with whatever text you want
>                 Multipart outboundMultipart = new MimeMultipart();
>                 MimeBodyPart messageBodyPart = new MimeBodyPart();
>                 messageBodyPart.setText("Set your body here");
>                 outboundMultipart.addBodyPart(messageBodyPart);
>
>                 // Loop over the multipart message coming in and
>                 // append them to the outbound Multipart object
>                 for (int i = 0; i < inboundMultipart.getCount(); i++) {
>                     BodyPart part = inboundMultipart.getBodyPart(i);
>                     /*
>                         The content-disposition header is optional:
>                        http://www.ietf.org/rfc/rfc1806.txt
>
>                         This header specifies the filename and type of
>                         a MIME part.
>                     */
>                     if(part.getDisposition() == null) {
>                         // This is just a plain text email
>                     } else {
>                         // We have something interesting. Let's parse it.
>
>                         // Create a new ByteArrayDataSource with this part
>                         MimeBodyPart inboundMimeBodyPart = (MimeBodyPart)
> part;
>                         InputStream is = part.getInputStream();
>                         ByteArrayDataSource mimePartDataSource = new
> ByteArrayDataSource(is, inboundMimeBodyPart.getContentType());
>
>                         // Create a new outbound MimeBodyPart and set this
> as the handler
>                         MimeBodyPart outboundMimeBodyPart = new
> MimeBodyPart();
>                         outboundMimeBodyPart.setDataHandler(new
> DataHandler(mimePartDataSource));
>
> outboundMimeBodyPart.setFileName(inboundMimeBodyPart.getFileName());
>                         outboundMultipart.addBodyPart(outboundMimeBodyPart);
>
>                     }
>
>                 }
>                 message.setContent(outboundMultipart);
>
>             }
>             message.setFrom(new InternetAddress(SENDER, "Relay account"));
>             message.setRecipient(Message.RecipientType.TO, new
> InternetAddress(RECIPIENT, "Recipient"));
>
>             Transport.send(message);
>
>         } catch (MessagingException e) {
>             throw new ServletException(e);
>         }
>     }
>
> }
> On Sat, Nov 14, 2009 at 1:11 AM, mably <fm2...@mably.com> wrote:
> > Hi Ikai, have you been able to reproduce my "Converting attachment
> > data failed" exception ?
>
> > I'm still stuck on this strange bug.
>
> > Thanx for your help.
>
> > On 11 nov, 00:00, mably <fm2...@mably.com> wrote:
> > > Of course, here is below my email relay servlet class.  What I'm
> > > willing to do is to hide my customers email addresses by relaying
> > > email to them via my google app email adress.
>
> > > I would like to be able to relay html emails with images which is
> > > quite common nowadays.  I am sending my test emails from a gmail
> > > account.
>
> > > Thanx for your help.
>
> > > /*
> > >  * Copyright (C) 2009 Francois Masurel
> > >  *
> > >  * This program is free software: you can redistribute it and/or
> > > modify
> > >  * it under the terms of the GNU General Public License as published
> > > by
> > >  * the Free Software Foundation, either version 3 of the License, or
> > >  * any later version.
> > >  *
> > >  * This program is distributed in the hope that it will be useful,
> > >  * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > >  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > >  * GNU General Public License for more details.
> > >  *
> > >  * You should have received a copy of the GNU General Public License
> > >  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
>
> > >  */
>
> > > package com.mably.cms.web;
>
> > > import java.io.IOException;
> > > import java.io.InputStream;
> > > import java.util.Arrays;
> > > import java.util.Properties;
>
> > > import javax.activation.DataHandler;
> > > import javax.activation.DataSource;
> > > import javax.mail.BodyPart;
> > > import javax.mail.Message;
> > > import javax.mail.MessagingException;
> > > import javax.mail.Multipart;
> > > import javax.mail.Session;
> > > import javax.mail.Transport;
> > > import javax.mail.internet.InternetAddress;
> > > import javax.mail.internet.MimeBodyPart;
> > > import javax.mail.internet.MimeMessage;
> > > import javax.mail.internet.MimeMultipart;
> > > import javax.mail.internet.MimeMessage.RecipientType;
> > > 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 org.slf4j.Logger;
> > > import org.slf4j.LoggerFactory;
>
> > > import com.google.inject.Inject;
> > > import com.google.inject.Singleton;
> > > import com.mably.cms.model.ContentManager;
> > > import com.mably.cms.utils.TraitementStream;
>
> > > /**
> > >  * @author f.masurel
> > >  *
> > >  */
> > > @Singleton
> > > public class ContactMailServlet extends HttpServlet {
>
> > >     /** serialVersionUID. */
> > >     private static final long serialVersionUID = 7131942698661805870L;
>
> > >     /** Logger. */
> > >     private static final Logger LOG =
> > >         LoggerFactory.getLogger(ContactMailServlet.class);
>
> > >     @Inject private ContentManager contentManager;
>
> > >     /**
> > >      * {...@inheritdoc}
> > >      */
> > >     public void doPost(HttpServletRequest req, HttpServletResponse
> > > res)
> > >             throws ServletException, IOException {
>
> > >         Properties props = new Properties();
> > >         Session session = Session.getDefaultInstance(props, null);
>
> > >         try {
>
> > >                 MimeMessage msg = new MimeMessage(session,
> > req.getInputStream
> > > ());
>
> > >                         String contentType = msg.getContentType();
>
> > >                         LOG.info("Mail content type : " + contentType);
> > >                         LOG.info("Mail sent to " + Arrays.toString(
>
> > msg.getRecipients(RecipientType.TO)));
> > >                         LOG.info("Mail received from " +
> > msg.getSender());
>
> > >                         MimeMessage resmsg = new MimeMessage(session);
>
> > >                         resmsg.setSubject(msg.getSubject());
> > >                         resmsg.setRecipient(Message.RecipientType.TO,
> > msg.getSender());
>
> > >                         Multipart rescontent = new MimeMultipart();
>
> > >                         Object content = msg.getContent();
> > >                         if (content instanceof String) {
> > >                                 LOG.info("Content : string");
> > >                         } else if (content instanceof Multipart) {
> > >                                 LOG.info("Content : multipart");
> > >                                 Multipart mpcontent = (Multipart)
> > msg.getContent();
> > >                                 for (int i = 0; i < mpcontent.getCount();
> > i++) {
> > >                                         BodyPart part =
> > mpcontent.getBodyPart(i);
> > >                                         MimeBodyPart respart = new
> > MimeBodyPart();
> > >                                         respart.setContent(
>
> > part.getContent(), part.getContentType());
> > >                                         rescontent.addBodyPart(respart);
> > >                                 }
> > >                         } else if (content instanceof InputStream) {
> > >                                 LOG.info("Content : inputstream");
> > >                                 InputStream inputStream = (InputStream)
> > content;
> > >                                 ByteArrayDataSource ds =
> > >                                         new
> > ByteArrayDataSource(inputStream, contentType);
> > >                                 Multipart mpcontent = new
> > MimeMultipart(ds);
> > >                                 for (int i = 0; i < mpcontent.getCount();
> > i++) {
> > >                                         BodyPart part =
> > mpcontent.getBodyPart(i);
> > >                                         MimeBodyPart respart = new
> > MimeBodyPart();
> > >                                         String level = String.valueOf(i);
> > >                                         this.processInputStreamPart(part,
> > respart, level);
> > >                                         rescontent.addBodyPart(respart);
> > >                                 }
> > >                         }
>
> > >                         resmsg.setContent(rescontent);
>
> > >                         resmsg.setSender(new InternetAddress("
> > f.masu...@gmail.com"));
> > >                         resmsg.setFrom(msg.getSender());
> > >                         //resmsg.setReplyTo(new Address[] {
> > msg.getSender() });
> > >                         resmsg.setSubject(msg.getSubject());
> > >                         resmsg.setRecipient(Message.RecipientType.TO,
> > msg.getSender());
>
> > >                         resmsg.saveChanges();
>
> > >             Transport.send(resmsg);
>
> > >                 } catch (MessagingException e) {
>
> > >                         LOG.error(e.toString(), e);
>
> > >                 }
>
> > >     }
>
> > >     private void processInputStreamPart(
> > >                 BodyPart srcPart, MimeBodyPart tgtPart, String level)
> > >     throws MessagingException, IOException {
> > >                 String partType = srcPart.getContentType();
> > >                 LOG.info("Part " + level + " content type : " +
> > partType);
> > >                 Object partContent = srcPart.getContent();
> > >                 LOG.info("Part " + level + " content : " + partContent);
> > >                 if (partType.startsWith("multipart/")) {
> > >                         String subType =
> > partType.substring(partType.indexOf('/') + 1);
> > >                         Multipart tgtPartMP = new MimeMultipart(subType);
> > >                         ByteArrayDataSource srcPartDS = new
> > ByteArrayDataSource(
> > >                                         (InputStream) partContent,
> > partType);
> > >                         Multipart srcPartMP = new
> > MimeMultipart(srcPartDS);
> > >                         for (int j = 0; j < srcPartMP.getCount(); j++) {
> > >                                 BodyPart srcChildPart =
> > srcPartMP.getBodyPart(j);
> > >                                 MimeBodyPart tgtChildPart = new
> > MimeBodyPart();
> > >                                 String childLevel = level + "." + j;
> > >                                 this.processInputStreamPart(
> > >                                                 srcChildPart,
> > tgtChildPart, childLevel);
> > >                                 tgtPartMP.addBodyPart(tgtChildPart);
> > >                         }
> > >                         tgtPart.setContent(tgtPartMP);
> > >                 } else if (partType.startsWith("text/")) {
> > >                         String data = TraitementStream.toString(
> > >                                         (InputStream) partContent);
> > >                         tgtPart.setContent(data, partType);
> > >                 } else {
> > >                         String dataType = partType.substring(0,
> > partType.indexOf(';'));
> > >                         byte[] srcData  =
> > TraitementStream.getBytesFromInputStream(
> > >                                         srcPart.getInputStream());
> > >                         LOG.info("Data type = " + dataType);
> > >                         LOG.info("Data size = " + srcData.length);
> > >                         LOG.info("File name = " + srcPart.getFileName());
> > >                         tgtPart.setFileName(srcPart.getFileName());
> > >                         tgtPart.setDisposition(srcPart.getDisposition());
> > >                         DataSource tgtPartDS = new
> > ByteArrayDataSource(srcData, dataType);
> > >                         tgtPart.setDataHandler(new
> > DataHandler(tgtPartDS));
> > >                         //tgtPart.setContent(srcData, dataType);
> > >                 }
> > >     }
>
> > >     /**
> > >      * {...@inheritdoc}
> > >      */
> > >     public void doHead(HttpServletRequest req, HttpServletResponse
> > > res)
> > >             throws ServletException, IOException {
> > >     }
>
> > >     /**
> > >      * {...@inheritdoc}
> > >      */
> > >     public void doGet(HttpServletRequest req, HttpServletResponse res)
> > >             throws ServletException, IOException {
> > >     }
>
> > > }
>
> > > On 10 nov, 23:46, "Ikai L (Google)" <ika...@google.com> wrote:
>
> > > > François,
>
> > > > Do you have a code snippet, log entries or a stack trace?
>
> > > > --
> > > > 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<google-appengine-java%2bunsubscr...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-appengine-java?hl=.
>
> --
> 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=.


Reply via email to