Thanks Ikai.
It did NOT work before because I set the dataHandler before I set the
FileName.
Just have to set the FileName and then set the DataHandler.
attachment.setFileName("ticker.png");
attachment.setDataHandler(new DataHandler(mimePartDataSource));
Is this behaviour intentional?
Many thanks
Don
On Nov 24, 10:15 am, "Ikai L (Google)" <[email protected]> wrote:
> Don,
>
> First, a word of caution: you'll probably want to contact the creators of
> the site you are trying to fetch the image from if you haven't done so
> already. Their terms of service prohibit the use of automatic downloading of
> images:http://support.stockcharts.com/forums/31090/entries/20485
>
> To fetch an image from a URL and send it via the Mail Service, this what you
> need to do:
>
> 1. Fetch your URL
> 2. Find the content type
> 3. Read the stream into a byte[]
> 4. Create a message and a data handler
> 5. Pass the byte[] into the data source, then into the data handler
>
> Example code:http://pastie.org/712159
>
> 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.MimeMultipart;
> import javax.mail.internet.MimeBodyPart;
> import javax.mail.internet.MimeMessage;
> import javax.mail.internet.InternetAddress;
> import javax.activation.DataHandler;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.ByteArrayOutputStream;
> import java.net.URL;
> import java.util.Properties;
>
> public class GetStockServlet extends HttpServlet {
> private static final String SENDER = "[email protected]";
> private static final String RECIPIENT = "[email protected]";
>
> protected void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>
> URL url = new URL("http://yoururl.com/image.png");
> InputStream in = url.openStream();
>
> byte[] rawData;
> int len;
> byte[] buffer = new byte[8192];
> ByteArrayOutputStream output = new ByteArrayOutputStream();
>
> try {
> while ((len = in.read(buffer, 0, buffer.length)) != -1)
> output.write(buffer, 0, len);
> rawData = output.toByteArray();
> } finally {
> output.close();
> }
>
> response.setContentType("image/png");
> response.getOutputStream().write(rawData);
> response.getOutputStream().flush();
>
> String htmlBody = "Here is the quote you wanted";
>
> Properties props = new Properties();
> Session session = Session.getDefaultInstance(props, null);
>
> Message message = new MimeMessage(session);
>
> Multipart mp = new MimeMultipart();
>
> MimeBodyPart htmlPart = new MimeBodyPart();
>
> try {
> message.setFrom(new InternetAddress(SENDER, "Stock Service"));
> message.addRecipient(Message.RecipientType.TO, new
> InternetAddress(RECIPIENT));
> message.setSubject("Stock Quote: " + symbol);
>
> htmlPart.setContent(htmlBody, "text/html");
> mp.addBodyPart(htmlPart);
>
> ByteArrayDataSource dataSource = new
> ByteArrayDataSource(rawData, "image/png");
>
> MimeBodyPart attachment = new MimeBodyPart();
> attachment.setFileName(symbol + ".png");
> attachment.setDataHandler(new
> DataHandler(dataSource));
> mp.addBodyPart(attachment);
>
> message.setContent(mp);
> Transport.send(message);
> } catch (MessagingException e) {
> throw new IOException(e);
> }
>
> }
>
> }
> On Sun, Nov 22, 2009 at 9:04 AM, Don <[email protected]> wrote:
> > 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("[email protected]", "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)" <[email protected]> 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 = "[email protected]";
> > > private static final String SENDER = "[email protected]";
>
> > > 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"));
>
> ...
>
> read more »
--
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.