[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-10 Thread Ikai L (Google)
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-java@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=en
-~--~~~~--~~--~--~---



[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-10 Thread mably

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 .
 */

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);
  

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-14 Thread mably
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  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 .
>
>  */
>
> 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 = (InputS

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-16 Thread mably
Hi Ikai, thanx for you help.

I've copy pasted your code on my webapp (webwinewatch), I've no error
but the mail isn't correctly relayed, all attachements are removed.

I've just modified the sender, from and recipient lines :

message.setSender(new InternetAddress("@gmail.com", "Relay
account"));
message.setFrom(message.getSender());
message.setRecipient(Message.RecipientType.TO, message.getSender());

Do you have any clue ?

You can test it, sending an email to : contact-
t...@webwinewatch.appspotmail.com

Thanx again for your help.

On 16 nov, 21:35, "Ikai L (Google)"  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

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-16 Thread mably
In fact standards attachments works.

But it's not what I'm trying do do.

I'm need to relay HTML messages with inline images and this is still
not working.

Is it a kind of limitation of GAE or is it supposed to work ?

Thanx for your help.

François

On 16 nov, 23:09, mably  wrote:
> Hi Ikai, thanx for you help.
>
> I've copy pasted your code on my webapp (webwinewatch), I've no error
> but the mail isn't correctly relayed, all attachements are removed.
>
> I've just modified the sender, from and recipient lines :
>
> message.setSender(new InternetAddress("@gmail.com", "Relay
> account"));
> message.setFrom(message.getSender());
> message.setRecipient(Message.RecipientType.TO, message.getSender());
>
> Do you have any clue ?
>
> You can test it, sending an email to : contact-
> t...@webwinewatch.appspotmail.com
>
> Thanx again for your help.
>
> On 16 nov, 21:35, "Ikai L (Google)"  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 ou

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-18 Thread mably
I'm just sending a simple mail from my Gmail account with an embedded
gif image.

I can perfectly read it from my mail servlet handler but when I try to
relay it (cf. code from first message) I get this annoying "Converting
attachment data failed" error which seems to be specific to GAE.

Here is the mail sent data :


MIME-Version: 1.0
Sender: x...@gmail.com
Received: by 10.142.204.15 with HTTP; Wed, 18 Nov 2009 13:57:20 -0800
(PST)
Date: Wed, 18 Nov 2009 22:57:20 +0100
Delivered-To: x...@gmail.com
X-Google-Sender-Auth: c9f34852bdb4f249
Message-ID:
<75c1488c0911181357w4e8efdc2r7e82d8e1c3e03...@mail.gmail.com>
Subject: Test
From: Francois MASUREL 
To: contact-test 
Content-Type: multipart/related; boundary=001636e90e61aa516e0478ac5398

--001636e90e61aa516e0478ac5398
Content-Type: multipart/alternative;
boundary=001636e90e61aa516b0478ac5397

--001636e90e61aa516b0478ac5397
Content-Type: text/plain; charset=UTF-8

[image:
?
ui=2&view=att&th=125094c9bff3199b&attid=0.1&disp=attd&realattid=ii_125094c9bff3199b&zw]

--001636e90e61aa516b0478ac5397
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable




--001636e90e61aa516b0478ac5397--
--001636e90e61aa516e0478ac5398
Content-Type: image/gif; name="enveloppe.gif"
Content-Transfer-Encoding: base64
Content-ID: 
X-Attachment-Id: ii_125094c9bff3199b

R0lGODlhEAAQAPeYAP39/tHR5MrK4fLy99PT6NfX6Nvb6/
f3+ff3+uDg7sPD3fHx9ra206qqyrKy
0eLi8Onp8bCwz
+jo8cvL4WVlm7y81cLC2fPz96ysyrCwzcXF3c7O487O4tDQ5Le30+fn8cvL4ufn
8PDw9/Ly+fDw9PLy+N7e66Skx8/
P46mpymBglNLS5KKixoWFsYaGs56ewNXV5pqawKyszuTk7pyc
wdra6Pr6/
Ht7qnt7qXl5qry82LGx0MbG3pCQsrOz0qyszZubucjI3eLi7eDg6eDg7X5+rJSUu9zc
54uLtvz8/b+/2KenyPX1+K6uzpmZwOvr9a6uz8XF3Ovr9tLS5uPj8Hx8qvv7/
ZiYwLm50NPT5tjY
6K+v0M7O5IGBqtzc68/
P3mpqnoeHsLCwynJypNjY60ZGgJ2dw1hYjc7O4bi40nh4qWlpnNjY5fn5
+5SUvcTE2oCAqbOz0Le31tzc7L292vj4+sPD2by81unp8s/P5s3N3fPz
+VxckqOjyHZ2psLC23p6
pt3d7d7e7PDw+L292JOTu/z8/Kury7Ozyra20ubm8MTE1/b2/Pn5/
Hd3qJCQvOnp876+2tHR5tHR
4oaGrbW10pmZv/7+/v///
wAA




ACH5BAEAAJgALAAQABAA
AAjlADEJHCiwzSQjamYQXIjAAo0fAq5gWChwgB03PiBdkhDkxgKCITywiLLn0qUkU2A4ESPQRKIm
K5iYNIlHCREefg5gMjNBBICZAKx4SUGlQA4smOJw
+BAAjaJLABh1aBCITJ8uNmrIuDRAS4Uldwyk
OZQFhQIKXxCcEGLSkCBLOKowoKNDARJKmN5AMQmghIBIEfKAICTnURlMFwotuNQogYYHcwJsYDDm
DCKBGXZcGiQpgIECf1zwAUJiIIQWI6RwSUAghooeQyi
+cPCEwBYwcI5QFMimSCVHgPTsHlgnzJpF
OncHBAA7
--001636e90e61aa516e0478ac5398--

On Nov 18, 10:34 pm, "Ikai L (Google)"  wrote:
> François,
>
> I'm not familiar with any standards with regard to inline images. Do you
> have this working outside of App Engine? Do you have working code from that?
> If there are discrepancies in our implementation of javax.mail.* and
> standard implementations we should be aware.
>
> On Mon, Nov 16, 2009 at 10:30 PM, mably  wrote:
> > In fact standards attachments works.
>
> > But it's not what I'm trying do do.
>
> > I'm need to relay HTML messages with inline images and this is still
> > not working.
>
> > Is it a kind of limitation of GAE or is it supposed to work ?
>
> > Thanx for your help.
>
> > François
>
> > On 16 nov, 23:09, mably  wrote:
> > > Hi Ikai, thanx for you help.
>
> > > I've copy pasted your code on my webapp (webwinewatch), I've no error
> > > but the mail isn't correctly relayed, all attachements are removed.
>
> > > I've just modified the sender, from and recipient lines :
>
> > > message.setSender(new InternetAddress("@gmail.com", "Relay
> > > account"));
> > > message.setFrom(message.getSender());
> > > message.setRecipient(Message.RecipientType.TO, message.getSender());
>
> > > Do you have any clue ?
>
> > > You can test it, sending an email to : contact-
> > > t...@webwinewatch.appspotmail.com
>
> > > Thanx again for your help.
>
> > > On 16 nov, 21:35, "Ikai L (Google)"  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.Mi

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-18 Thread mably
I've written a simple class sending an HTML email with an embedded
image (cf. below) via GMail SMTP.  It works perfectly fine when run
locally from Eclipse.

The same code in a GAE mail servlet handler produce the infamous
"Converting attachment data failed" error.

So it really seems to be a problem in GAE javamail implementation.

/*
 * 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 .
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.Message;
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;

/**
 * @author Francois
 *
 */
public class GMailSMTP {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final int SMTP_HOST_PORT = 465;
private static final String SMTP_AUTH_USER = "x...@gmail.com";
private static final String SMTP_AUTH_PWD  = "yyy";

public static void main(String[] args) throws Exception{
   new GMailSMTP().test();
}

public void test() throws Exception{
Properties props = new Properties();

props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
// props.put("mail.smtps.quitwait", "false");

String from = SMTP_AUTH_USER;
String to = SMTP_AUTH_USER;
String subject = "Testing SMTP-SSL";
String htmlText = "Hello";
byte[] imgData = this.obtainByteData("enveloppe.gif");

Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress
(to));
msg.setSubject(subject);

Multipart mp = new MimeMultipart("related");

MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlText, "text/html");
mp.addBodyPart(htmlPart);

MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("test.gif");
attachment.setContent(imgData, "image/gif");
attachment.setHeader("Content-ID","");
mp.addBodyPart(attachment);

msg.setContent(mp);

Transport transport = mailSession.getTransport();

transport.connect
  (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER,
SMTP_AUTH_PWD);

transport.sendMessage(msg,
msg.getRecipients(Message.RecipientType.TO));

transport.close();
}

/**
 * Constructs a byte array and fills it with data that is read
from the
 * specified resource.
 * @param filename the path to the resource
 * @return the specified resource as a byte array
 * @throws java.io.IOException if the resource cannot be read, or
the
 *   bytes cannot be written, or the streams cannot be closed
 */
private byte[] obtainByteData(String filename) throws IOException
{
InputStream inputStream = getClass().getResourceAsStream
(filename);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream
(1024);
byte[] bytes = new byte[512];

// Read bytes from the input stream in bytes.length-sized
chunks and write
// them into the output stream
int readBytes;
while ((readBytes = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, readBytes);
}

// Convert the contents of the output stream into a byte array
byte[] byteData = outputStream.toByteArray();

// Close the streams
inputStream.close();
outputStream.close();

return byteData;
}

}


On Nov 18, 11:11 pm, "Ikai L (Google)"  wrote:
> What I mean is, is this code working on a different mail server?
>
> On Wed, Nov 18, 2009 at 2:00 PM, mably  wrote:
> > I'm just sending a simple mail from my Gmail account with an embedded
> > gif image.
>
> > I can perfectly read it from my mail servlet handler but when I try to
> > relay it (cf. code from first message) I get this annoying "Converting
> > attachment data failed" error which seems to be s

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-18 Thread mably
Of course, the code above doesn't need to be run in mail handler
servlet :-)

On Nov 19, 12:01 am, mably  wrote:
> I've written a simple class sending an HTML email with an embedded
> image (cf. below) via GMail SMTP.  It works perfectly fine when run
> locally from Eclipse.
>
> The same code in a GAE mail servlet handler produce the infamous
> "Converting attachment data failed" error.
>
> So it really seems to be a problem in GAE javamail implementation.
>
> /*
>  * 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 .
>
>  */
>
> import java.io.ByteArrayOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.Properties;
>
> import javax.mail.Message;
> 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;
>
> /**
>  * @author Francois
>  *
>  */
> public class GMailSMTP {
>
>     private static final String SMTP_HOST_NAME = "smtp.gmail.com";
>     private static final int SMTP_HOST_PORT = 465;
>     private static final String SMTP_AUTH_USER = "x...@gmail.com";
>     private static final String SMTP_AUTH_PWD  = "yyy";
>
>     public static void main(String[] args) throws Exception{
>        new GMailSMTP().test();
>     }
>
>     public void test() throws Exception{
>         Properties props = new Properties();
>
>         props.put("mail.transport.protocol", "smtps");
>         props.put("mail.smtps.host", SMTP_HOST_NAME);
>         props.put("mail.smtps.auth", "true");
>         // props.put("mail.smtps.quitwait", "false");
>
>         String from = SMTP_AUTH_USER;
>         String to = SMTP_AUTH_USER;
>         String subject = "Testing SMTP-SSL";
>         String htmlText = "Hello";
>         byte[] imgData = this.obtainByteData("enveloppe.gif");
>
>         Session mailSession = Session.getDefaultInstance(props);
>         mailSession.setDebug(true);
>
>         Message msg = new MimeMessage(mailSession);
>         msg.setFrom(new InternetAddress(from));
>         msg.addRecipient(Message.RecipientType.TO, new InternetAddress
> (to));
>         msg.setSubject(subject);
>
>         Multipart mp = new MimeMultipart("related");
>
>         MimeBodyPart htmlPart = new MimeBodyPart();
>         htmlPart.setContent(htmlText, "text/html");
>         mp.addBodyPart(htmlPart);
>
>         MimeBodyPart attachment = new MimeBodyPart();
>         attachment.setFileName("test.gif");
>         attachment.setContent(imgData, "image/gif");
>         attachment.setHeader("Content-ID","");
>         mp.addBodyPart(attachment);
>
>         msg.setContent(mp);
>
>         Transport transport = mailSession.getTransport();
>
>         transport.connect
>           (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER,
> SMTP_AUTH_PWD);
>
>         transport.sendMessage(msg,
>             msg.getRecipients(Message.RecipientType.TO));
>
>         transport.close();
>     }
>
>     /**
>      * Constructs a byte array and fills it with data that is read
> from the
>      * specified resource.
>      * @param filename the path to the resource
>      * @return the specified resource as a byte array
>      * @throws java.io.IOException if the resource cannot be read, or
> the
>      *   bytes cannot be written, or the streams cannot be closed
>      */
>     private byte[] obtainByteData(String filename) throws IOException
> {
>         InputStream inputStream = getClass().getResourceAsStream
> (filename);
>         ByteArrayOutputStream outputStream = new ByteArrayOutputStream
> (1024);
>         byte[] bytes = new byte[512];
>
>         // Read bytes from the input stream in bytes.length-sized
> chunks and write
>         // them into the output stream
>         int readBytes;
>         while ((readBytes = inputStream.read(bytes)) > 0) {
>             outputStream.write(bytes, 0, readBytes);
>         }
>
>         // Convert the contents of the output stream into a byte array
>         byte[] byteData = outputStream.toByteArray();
>
>         // Close the streams
>         inputStream.close();
>         outputStream.close();
>
>         return byteData;
>     }
>
> }
>
> On Nov 18, 11:11 pm, "Ikai L (Google)"  wrote:
>
> > What I mean is, is this code wo

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-18 Thread mably
I haven't found a way to set a Content-ID header on a mimebodypart/
attachment using the low-level API.  The MailService.Attachment class
is quite rudimentary (only filename and data).

May be some part of the low level API are still undocumented ?

Thanx for all.

On Nov 19, 12:25 am, "Ikai L (Google)"  wrote:
> That's good information to know. We are not using Sun's JavaMail
> implementation, so it's possible there are points of incompatibility.
> There's one last thing you may want to try:
>
> http://code.google.com/appengine/docs/java/javadoc/com/google/appengi...
>
> The above page documents the Low-Level API. Try creating an email object via
> that API, setting the Content-ID of the attachment and the HTML tag to point
> to the cid. You're already doing that in the example you sent using
> javax.mail, but I'm wondering if the low-level API is capable of doing this.
> If not, I'll open an issue.
>
> On Wed, Nov 18, 2009 at 3:06 PM, mably  wrote:
> > Of course, the code above doesn't need to be run in mail handler
> > servlet :-)
>
> > On Nov 19, 12:01 am, mably  wrote:
> > > I've written a simple class sending an HTML email with an embedded
> > > image (cf. below) via GMail SMTP.  It works perfectly fine when run
> > > locally from Eclipse.
>
> > > The same code in a GAE mail servlet handler produce the infamous
> > > "Converting attachment data failed" error.
>
> > > So it really seems to be a problem in GAE javamail implementation.
>
> > > /*
> > >  * 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 .
>
> > >  */
>
> > > import java.io.ByteArrayOutputStream;
> > > import java.io.IOException;
> > > import java.io.InputStream;
> > > import java.util.Properties;
>
> > > import javax.mail.Message;
> > > 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;
>
> > > /**
> > >  * @author Francois
> > >  *
> > >  */
> > > public class GMailSMTP {
>
> > >     private static final String SMTP_HOST_NAME = "smtp.gmail.com";
> > >     private static final int SMTP_HOST_PORT = 465;
> > >     private static final String SMTP_AUTH_USER = "x...@gmail.com";
> > >     private static final String SMTP_AUTH_PWD  = "yyy";
>
> > >     public static void main(String[] args) throws Exception{
> > >        new GMailSMTP().test();
> > >     }
>
> > >     public void test() throws Exception{
> > >         Properties props = new Properties();
>
> > >         props.put("mail.transport.protocol", "smtps");
> > >         props.put("mail.smtps.host", SMTP_HOST_NAME);
> > >         props.put("mail.smtps.auth", "true");
> > >         // props.put("mail.smtps.quitwait", "false");
>
> > >         String from = SMTP_AUTH_USER;
> > >         String to = SMTP_AUTH_USER;
> > >         String subject = "Testing SMTP-SSL";
> > >         String htmlText = "Hello";
> > >         byte[] imgData = this.obtainByteData("enveloppe.gif");
>
> > >         Session mailSession = Session.getDefaultInstance(props);
> > >         mailSession.setDebug(true);
>
> > >         Message msg = new MimeMessage(mailSession);
> > >         msg.setFrom(new InternetAddress(from));
> > >         msg.addRecipient(Message.RecipientType.TO, new InternetAddress
> > > (to));
> > >         msg.setSubject(subject);
>
> > >         Multipart mp = new MimeMultipart("related");
>
> > >         MimeBodyPart htmlPart = new MimeBodyPart();
> > >         htmlPart.setContent(htmlText, "text/html");
> > >         mp.addBodyPart(htmlPart);
>
> > >         MimeBodyPart attachment = new MimeBodyPart();
> > >         attachment.setFileName("test.gif");
> > >         attachment.setContent(imgData, "image/gif");
> > >         attachment.setHeader("Content-ID","");
> > >         mp.addBodyPart(attachment);
>
> > >         msg.setContent(mp);
>
> > >         Transport transport = mailSession.getTransport();
>
> > >         transport.connect
> > >           (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER,
> > > SMTP_AUTH_PWD);
>
> > >         transport.sendMessage(msg,
> > >             msg.getRecipients(Message.RecipientType.TO));
>
> > >         transport.close();
> >

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-19 Thread mably
Any chance to see this bug fixed in the future ?

Is there any documentation on the meaning of "Converting
attachment data failed" error message ?

On 19 nov, 20:55, "Ikai L (Google)"  wrote:
> Looks like you're right. As far as I know there are no hidden methods.
>
> Another workaround for this limitation could be to store incoming images in
> the data store on an incoming email, then reference them in the emails that
> get sent out. It's not ideal, but it may be the solution to what you are
> looking for.
>
>
>
> On Wed, Nov 18, 2009 at 3:36 PM, mably  wrote:
> > I haven't found a way to set a Content-ID header on a mimebodypart/
> > attachment using the low-level API.  The MailService.Attachment class
> > is quite rudimentary (only filename and data).
>
> > May be some part of the low level API are still undocumented ?
>
> > Thanx for all.
>
> > On Nov 19, 12:25 am, "Ikai L (Google)"  wrote:
> > > That's good information to know. We are not using Sun's JavaMail
> > > implementation, so it's possible there are points of incompatibility.
> > > There's one last thing you may want to try:
>
> > >http://code.google.com/appengine/docs/java/javadoc/com/google/appengi...
>
> > > The above page documents the Low-Level API. Try creating an email object
> > via
> > > that API, setting the Content-ID of the attachment and the HTML tag to
> > point
> > > to the cid. You're already doing that in the example you sent using
> > > javax.mail, but I'm wondering if the low-level API is capable of doing
> > this.
> > > If not, I'll open an issue.
>
> > > On Wed, Nov 18, 2009 at 3:06 PM, mably  wrote:
> > > > Of course, the code above doesn't need to be run in mail handler
> > > > servlet :-)
>
> > > > On Nov 19, 12:01 am, mably  wrote:
> > > > > I've written a simple class sending an HTML email with an embedded
> > > > > image (cf. below) via GMail SMTP.  It works perfectly fine when run
> > > > > locally from Eclipse.
>
> > > > > The same code in a GAE mail servlet handler produce the infamous
> > > > > "Converting attachment data failed" error.
>
> > > > > So it really seems to be a problem in GAE javamail implementation.
>
> > > > > /*
> > > > >  * 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/>.
>
> > > > >  */
>
> > > > > import java.io.ByteArrayOutputStream;
> > > > > import java.io.IOException;
> > > > > import java.io.InputStream;
> > > > > import java.util.Properties;
>
> > > > > import javax.mail.Message;
> > > > > 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;
>
> > > > > /**
> > > > >  * @author Francois
> > > > >  *
> > > > >  */
> > > > > public class GMailSMTP {
>
> > > > >     private static final String SMTP_HOST_NAME = "smtp.gmail.com";
> > > > >     private static final int SMTP_HOST_PORT = 465;
> > > > >     private static final String SMTP_AUTH_USER = "x...@gmail.com";
> > > > >     private static final String SMTP_AUTH_PWD  = "yyy";
>
> > > > >     public static void main(String[] args) throws Exception{
> > > > >        new GMailSMTP().test();
> > > > >     }
>
> > > > >     public void test() throws Exception{
> > > > >         Properties props = new Properties();
>
> > > > >         props.put("mail.transport.protocol", "smtps");
> > > > >         props.put("mail.smtps.host", SMTP_HOST_NAME);
> > > > >         props.put("mail.smtps.auth", "true");
> > > > >         // props.put("mail.smtps.quitwait", "false");
>
> > > > >         String from = SMTP_AUTH_USER;
> > > > >         String to = SMTP_AUTH_USER;
> > > > >         String subject = "Testing SMTP-SSL";
> > > > >         String htmlText = "Hello";
> > > > >         byte[] imgData = this.obtainByteData("enveloppe.gif");
>
> > > > >         Session mailSession = Session.getDefaultInstance(props);
> > > > >         mailSession.setDebug(true);
>
> > > > >         Message msg = new MimeMessage(mailSession);
> > > > >         msg.setFrom(new InternetAddress(from));
> > > > >         msg.addRecipient(Message.RecipientType.TO, new
> >

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-19 Thread mably
By the way, do you know if Google plans to open source some parts of
GAE librairies so we could investigate deeper when we encounter such a
problem ?

On 19 nov, 21:01, mably  wrote:
> Any chance to see this bug fixed in the future ?
>
> Is there any documentation on the meaning of "Converting
> attachment data failed" error message ?
>
> On 19 nov, 20:55, "Ikai L (Google)"  wrote:
>
>
>
> > Looks like you're right. As far as I know there are no hidden methods.
>
> > Another workaround for this limitation could be to store incoming images in
> > the data store on an incoming email, then reference them in the emails that
> > get sent out. It's not ideal, but it may be the solution to what you are
> > looking for.
>
> > On Wed, Nov 18, 2009 at 3:36 PM, mably  wrote:
> > > I haven't found a way to set a Content-ID header on a mimebodypart/
> > > attachment using the low-level API.  The MailService.Attachment class
> > > is quite rudimentary (only filename and data).
>
> > > May be some part of the low level API are still undocumented ?
>
> > > Thanx for all.
>
> > > On Nov 19, 12:25 am, "Ikai L (Google)"  wrote:
> > > > That's good information to know. We are not using Sun's JavaMail
> > > > implementation, so it's possible there are points of incompatibility.
> > > > There's one last thing you may want to try:
>
> > > >http://code.google.com/appengine/docs/java/javadoc/com/google/appengi...
>
> > > > The above page documents the Low-Level API. Try creating an email object
> > > via
> > > > that API, setting the Content-ID of the attachment and the HTML tag to
> > > point
> > > > to the cid. You're already doing that in the example you sent using
> > > > javax.mail, but I'm wondering if the low-level API is capable of doing
> > > this.
> > > > If not, I'll open an issue.
>
> > > > On Wed, Nov 18, 2009 at 3:06 PM, mably  wrote:
> > > > > Of course, the code above doesn't need to be run in mail handler
> > > > > servlet :-)
>
> > > > > On Nov 19, 12:01 am, mably  wrote:
> > > > > > I've written a simple class sending an HTML email with an embedded
> > > > > > image (cf. below) via GMail SMTP.  It works perfectly fine when run
> > > > > > locally from Eclipse.
>
> > > > > > The same code in a GAE mail servlet handler produce the infamous
> > > > > > "Converting attachment data failed" error.
>
> > > > > > So it really seems to be a problem in GAE javamail implementation.
>
> > > > > > /*
> > > > > >  * 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/>.
>
> > > > > >  */
>
> > > > > > import java.io.ByteArrayOutputStream;
> > > > > > import java.io.IOException;
> > > > > > import java.io.InputStream;
> > > > > > import java.util.Properties;
>
> > > > > > import javax.mail.Message;
> > > > > > 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;
>
> > > > > > /**
> > > > > >  * @author Francois
> > > > > >  *
> > > > > >  */
> > > > > > public class GMailSMTP {
>
> > > > > >     private static final String SMTP_HOST_NAME = "smtp.gmail.com";
> > > > > >     private static final int SMTP_HOST_PORT = 465;
> > > > > >     private static final String SMTP_AUTH_USER = "x...@gmail.com";
> > > > > >     private static final String SMTP_AUTH_PWD  = "yyy";
>
> > > > > >     public static void main(String[] args) throws Exception{
> > > > > >        new GMailSMTP().test();
> > > > > >     }
>
> > > > > >     public void test() throws Exception{
> > > > > >         Properties props = new Properties();
>
> > > > > >         props.put("mail.transport.protocol", "smtps");
> > > > > >         props.put("mail.smtps.host", SMTP_HOST_NAME);
> > > > > >         props.put("mail.smtps.auth", "true");
> > > > > >         // props.put("mail.smtps.quitwait", "false");
>
> > > > > >         String from = SMTP_AUTH_USER;
> > > > > >         String to = SMTP_AUTH_USER;
> > > > > >         String subject = "Testing SMTP-SSL";
> > > > > >         String htmlText = "Hello";
> > > > > >      

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-23 Thread Don
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)"  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
>        

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-26 Thread Don
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)"  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 = "your.sen...@domain.com";
>     private static final String RECIPIENT = "your.recipi...@domain.com";
>
>     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  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("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 

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-12-15 Thread Don
Hi Ikai,

So does the order of setDataHandler + setFileName cause problems as I
wrote above?

Regards


On Dec 1, 5:25 am, "Ikai L (Google)"  wrote:
> Interesting, so this did not work?
>
> attachment.setDataHandler(new DataHandler(mimePartDataSource));
> attachment.setFileName("ticker.png");
>
> I'll need to do some research into the Java mail spec to see if we are
> matching it. Could be a bug if we aren't.
>
> On Thu, Nov 26, 2009 at 12:48 PM, Don  wrote:
> > 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)"  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 = "your.sen...@domain.com";
> > >     private static final String RECIPIENT = "your.recipi...@domain.com";
>
> > >     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  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 +
> > 

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-12-31 Thread minor-undroid
Hi there,

I reproduced same problem.
I cannot send attachment file and inline image with HTML mail.
(Of course, I could send plain text mail without attachment and
 HTML mail without inline image)
I don't have any idea.  It would be very helpful to tell me anything.

I build the mime message in HtmlMimeMessage#createMimeMessage method.
My code is as follows,
/
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.activation.DataHandler;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import com.mnrngsk.test20091217.mail.CommonData;
import com.mnrngsk.test20091217.mail.DbgLog;


public class HtmlMimeMessage {

private static final String CID_DOMAIN  = 
"@xxx";
private static final String REGEX_IMGTAG= 
"])*src=(\"[^\"]*\"|'[^']*'|[^'\">])*/?>";

private MimeMessage mmHtmlMsg = null;
private MimeMultipart   mmpHtmlMail = null;
private String  strHtmlSrc = "";
private boolean bAttach = false;
private String  strAttach = "";

private ArrayList replacedImgTagList
= new 
ArrayList();


// constructor
public HtmlMimeMessage(MimeMessage msg, boolean bAttach) {
this.mmHtmlMsg = msg;
this.bAttach = bAttach;
}

public void setSource(String strSrc) {
this.strHtmlSrc = strSrc;
}

public void setAttachment(String strAttach) {
if (this.bAttach) {
this.strAttach = strAttach;
}
}


private void addInlineImageTo(MimeMultipart mmp) {
int iImgSrcCount = this.replacedImgTagList.size();
for (int i = 0; i < iImgSrcCount; i++) {
DbgLog.info("HtmlMimeMessage#addInlineImageTo; Inline 
Image #" +
Integer.toString(i));

ImageMessageBodyFactory imageMbpFactory = new
ImageMessageBodyFactory(this.replacedImgTagList.get(i));
MimeBodyPart mbpImg = 
imageMbpFactory.createMessageBody();
try {
mmp.addBodyPart(mbpImg);
DbgLog.info("HtmlMimeMessage#addInlineImageTo;\n
mbpImg#ContentType="
+ mbpImg.getContentType() + "\n 
 this.mmpHtmlMail#ContentType="
+ mmp.getContentType());
} catch (MessagingException e) {
e.printStackTrace();
DbgLog.warning("HtmlMimeMessage#addInlineImageTo;
MessagingException; " + e.getMessage());
}
}
}

public boolean createMimeMessage() {
boolean bRes = false;

try {
TextByHtmlMessageBodyFactory textMbpFactory = new
TextByHtmlMessageBodyFactory(this.strHtmlSrc);
MimeBodyPart mbpText = 
textMbpFactory.createMessageBody();

HtmlMessageBodyFactory htmlMbpFactory = new 
HtmlMessageBodyFactory
(this.strHtmlSrc, replacedImgTagList);
MimeBodyPart mbpHtml = 
htmlMbpFactory.createMessageBody();

MimeMultipart mmpMixed = null;
MimeBodyPart mbpAttach = null;

if (this.bAttach) {
DbgLog.info("HtmlMimeMessage#createMimeMessage; 
w Attachment
(mixed);");

AttachedMessageBodyFactory attachMbpFactory = 
new
AttachedMessageBodyFactory();
attachMbpFactory.setAttachment(this.strAttach);
mbpAttach = 
attachMbpFactory.createMessageBody();

mmpMixed = attachMbpFactory.getMultipart();
}

MimeMultipart mmpAlter = new 
MimeMultipart("alternative");

if (mbpText != null) {
mmpAlter.addBodyPart(mbpText);
DbgLog.info("HtmlMimeMessage#createMimeMessage;"
+ "\n  mbpText#ContentType=" + 
mbpText.getContentType()
+ "\n  mmpAlter#ContentType=" + 
mmpAlter.getContentType()
  

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-12-31 Thread mably
Don't forget to star issue : 
http://code.google.com/p/googleappengine/issues/detail?id=965

On Dec 31, 3:23 pm, minor-undroid  wrote:
> Hi there,
>
> I reproduced same problem.
> I cannot send attachment file and inline image with HTML mail.
> (Of course, I could send plain text mail without attachment and
>  HTML mail without inline image)
> I don't have any idea.  It would be very helpful to tell me anything.
>
> I build the mime message in HtmlMimeMessage#createMimeMessage method.
> My code is as follows,
> /
> *** 
> **/
> import java.io.ByteArrayOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.net.MalformedURLException;
> import java.net.URL;
> import java.util.ArrayList;
> import java.util.regex.Matcher;
> import java.util.regex.Pattern;
> import java.util.regex.PatternSyntaxException;
>
> import javax.activation.DataHandler;
> import javax.mail.MessagingException;
> import javax.mail.internet.MimeBodyPart;
> import javax.mail.internet.MimeMessage;
> import javax.mail.internet.MimeMultipart;
> import javax.mail.util.ByteArrayDataSource;
>
> import com.mnrngsk.test20091217.mail.CommonData;
> import com.mnrngsk.test20091217.mail.DbgLog;
>
> public class HtmlMimeMessage {
>
>         private static final String             CID_DOMAIN              = 
> "@xxx";
>         private static final String             REGEX_IMGTAG    = 
> " [^'\">])*src=(\"[^\"]*\"|'[^']*'|[^'\">])*/?>";
>
>         private MimeMessage                     mmHtmlMsg = null;
>         private MimeMultipart                   mmpHtmlMail = null;
>         private String                  strHtmlSrc = "";
>         private boolean                         bAttach = false;
>         private String                  strAttach = "";
>
>         private ArrayList replacedImgTagList
>                                                         = new 
> ArrayList();
>
>         // constructor
>         public HtmlMimeMessage(MimeMessage msg, boolean bAttach) {
>                 this.mmHtmlMsg = msg;
>                 this.bAttach = bAttach;
>         }
>
>         public void setSource(String strSrc) {
>                 this.strHtmlSrc = strSrc;
>         }
>
>         public void setAttachment(String strAttach) {
>                 if (this.bAttach) {
>                         this.strAttach = strAttach;
>                 }
>         }
>
>         private void addInlineImageTo(MimeMultipart mmp) {
>                 int iImgSrcCount = this.replacedImgTagList.size();
>                 for (int i = 0; i < iImgSrcCount; i++) {
>                         DbgLog.info("HtmlMimeMessage#addInlineImageTo; Inline 
> Image #" +
> Integer.toString(i));
>
>                         ImageMessageBodyFactory imageMbpFactory = new
> ImageMessageBodyFactory(this.replacedImgTagList.get(i));
>                         MimeBodyPart mbpImg = 
> imageMbpFactory.createMessageBody();
>                         try {
>                                 mmp.addBodyPart(mbpImg);
>                                 
> DbgLog.info("HtmlMimeMessage#addInlineImageTo;\n
> mbpImg#ContentType="
>                                                 + mbpImg.getContentType() + 
> "\n  this.mmpHtmlMail#ContentType="
>                                                 + mmp.getContentType());
>                         } catch (MessagingException e) {
>                                 e.printStackTrace();
>                         DbgLog.warning("HtmlMimeMessage#addInlineImageTo;
> MessagingException; " + e.getMessage());
>                         }
>                 }
>         }
>
>     public boolean createMimeMessage() {
>         boolean bRes = false;
>
>         try {
>                 TextByHtmlMessageBodyFactory textMbpFactory = new
> TextByHtmlMessageBodyFactory(this.strHtmlSrc);
>                         MimeBodyPart mbpText = 
> textMbpFactory.createMessageBody();
>
>                         HtmlMessageBodyFactory htmlMbpFactory = new 
> HtmlMessageBodyFactory
> (this.strHtmlSrc, replacedImgTagList);
>                         MimeBodyPart mbpHtml = 
> htmlMbpFactory.createMessageBody();
>
>                         MimeMultipart mmpMixed = null;
>                         MimeBodyPart mbpAttach = null;
>
>                         if (this.bAttach) {
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage; w Attachment
> (mixed);");
>
>                                 AttachedMessageBodyFactory attachMbpFactory = 
> new
> AttachedMessageBodyFactory();
>                                 
> attachMbpFactory.setAttachment(this.strAttach);
>                                 mbpAttach = 
> attachMbpFactory.createMessageBody();
>
>                                 mmpMixed = attachMbpFactory.getMultipart();
>                         }
>
>                         MimeMultipart mmpAlter = new 
> MimeMultipart("alternative");
>
>                         if

[appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-12-31 Thread minor-undroid
Sorry, I made a wrting mistake.

I cannot send the HTML mail with inline image,
and cannot send the HTML mail with attachement file, too.
(I could send the plain text mail with an attachment image
 and HTML mail without attachment file)

I think It's only GAE's problem.
Do you have any idea?


On Dec 31, 11:23 pm, minor-undroid  wrote:
> Hi there,
>
> I reproduced same problem.
> I cannotsendattachmentfile and inline image with HTMLmail.
> (Of course, I couldsendplain textmailwithoutattachmentand
>  HTMLmailwithout inline image)
> I don't have any idea.  It would be very helpful to tell me anything.
>
> I build the mime message in HtmlMimeMessage#createMimeMessage method.
> My code is as follows,
> /
> */
> import java.io.ByteArrayOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.net.MalformedURLException;
> import java.net.URL;
> import java.util.ArrayList;
> import java.util.regex.Matcher;
> import java.util.regex.Pattern;
> import java.util.regex.PatternSyntaxException;
>
> import javax.activation.DataHandler;
> import javax.mail.MessagingException;
> import javax.mail.internet.MimeBodyPart;
> import javax.mail.internet.MimeMessage;
> import javax.mail.internet.MimeMultipart;
> import javax.mail.util.ByteArrayDataSource;
>
> import com.mnrngsk.test20091217.mail.CommonData;
> import com.mnrngsk.test20091217.mail.DbgLog;
>
> public class HtmlMimeMessage {
>
>         private static final String             CID_DOMAIN              = 
> "@xxx";
>         private static final String             REGEX_IMGTAG    = 
> " [^'\">])*src=(\"[^\"]*\"|'[^']*'|[^'\">])*/?>";
>
>         private MimeMessage                     mmHtmlMsg = null;
>         private MimeMultipart                   mmpHtmlMail = null;
>         private String                  strHtmlSrc = "";
>         private boolean                         bAttach = false;
>         private String                  strAttach = "";
>
>         private ArrayList replacedImgTagList
>                                                         = new 
> ArrayList();
>
>         // constructor
>         public HtmlMimeMessage(MimeMessage msg, boolean bAttach) {
>                 this.mmHtmlMsg = msg;
>                 this.bAttach = bAttach;
>         }
>
>         public void setSource(String strSrc) {
>                 this.strHtmlSrc = strSrc;
>         }
>
>         public void setAttachment(String strAttach) {
>                 if (this.bAttach) {
>                         this.strAttach = strAttach;
>                 }
>         }
>
>         private void addInlineImageTo(MimeMultipart mmp) {
>                 int iImgSrcCount = this.replacedImgTagList.size();
>                 for (int i = 0; i < iImgSrcCount; i++) {
>                         DbgLog.info("HtmlMimeMessage#addInlineImageTo; Inline 
> Image #" +
> Integer.toString(i));
>
>                         ImageMessageBodyFactory imageMbpFactory = new
> ImageMessageBodyFactory(this.replacedImgTagList.get(i));
>                         MimeBodyPart mbpImg = 
> imageMbpFactory.createMessageBody();
>                         try {
>                                 mmp.addBodyPart(mbpImg);
>                                 
> DbgLog.info("HtmlMimeMessage#addInlineImageTo;\n
> mbpImg#ContentType="
>                                                 + mbpImg.getContentType() + 
> "\n  this.mmpHtmlMail#ContentType="
>                                                 + mmp.getContentType());
>                         } catch (MessagingException e) {
>                                 e.printStackTrace();
>                         DbgLog.warning("HtmlMimeMessage#addInlineImageTo;
> MessagingException; " + e.getMessage());
>                         }
>                 }
>         }
>
>     public boolean createMimeMessage() {
>         boolean bRes = false;
>
>         try {
>                 TextByHtmlMessageBodyFactory textMbpFactory = new
> TextByHtmlMessageBodyFactory(this.strHtmlSrc);
>                         MimeBodyPart mbpText = 
> textMbpFactory.createMessageBody();
>
>                         HtmlMessageBodyFactory htmlMbpFactory = new 
> HtmlMessageBodyFactory
> (this.strHtmlSrc, replacedImgTagList);
>                         MimeBodyPart mbpHtml = 
> htmlMbpFactory.createMessageBody();
>
>                         MimeMultipart mmpMixed = null;
>                         MimeBodyPart mbpAttach = null;
>
>                         if (this.bAttach) {
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage; wAttachment
> (mixed);");
>
>                                 AttachedMessageBodyFactory attachMbpFactory = 
> new
> AttachedMessageBodyFactory();
>                                 
> attachMbpFactory.setAttachment(this.strAttach);
>                                 mbpAttach = 
> attachMbpFactory.createMessageBody();
>
>                                 mmpMix

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-16 Thread Ikai L (Google)
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  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  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

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-18 Thread Ikai L (Google)
François,

I'm not familiar with any standards with regard to inline images. Do you
have this working outside of App Engine? Do you have working code from that?
If there are discrepancies in our implementation of javax.mail.* and
standard implementations we should be aware.

On Mon, Nov 16, 2009 at 10:30 PM, mably  wrote:

> In fact standards attachments works.
>
> But it's not what I'm trying do do.
>
> I'm need to relay HTML messages with inline images and this is still
> not working.
>
> Is it a kind of limitation of GAE or is it supposed to work ?
>
> Thanx for your help.
>
> François
>
> On 16 nov, 23:09, mably  wrote:
> > Hi Ikai, thanx for you help.
> >
> > I've copy pasted your code on my webapp (webwinewatch), I've no error
> > but the mail isn't correctly relayed, all attachements are removed.
> >
> > I've just modified the sender, from and recipient lines :
> >
> > message.setSender(new InternetAddress("@gmail.com", "Relay
> > account"));
> > message.setFrom(message.getSender());
> > message.setRecipient(Message.RecipientType.TO, message.getSender());
> >
> > Do you have any clue ?
> >
> > You can test it, sending an email to : contact-
> > t...@webwinewatch.appspotmail.com
> >
> > Thanx again for your help.
> >
> > On 16 nov, 21:35, "Ikai L (Google)"  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 pl

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-18 Thread Ikai L (Google)
What I mean is, is this code working on a different mail server?

On Wed, Nov 18, 2009 at 2:00 PM, mably  wrote:

> I'm just sending a simple mail from my Gmail account with an embedded
> gif image.
>
> I can perfectly read it from my mail servlet handler but when I try to
> relay it (cf. code from first message) I get this annoying "Converting
> attachment data failed" error which seems to be specific to GAE.
>
> Here is the mail sent data :
>
>
> MIME-Version: 1.0
> Sender: x...@gmail.com
> Received: by 10.142.204.15 with HTTP; Wed, 18 Nov 2009 13:57:20 -0800
> (PST)
> Date: Wed, 18 Nov 2009 22:57:20 +0100
> Delivered-To: x...@gmail.com
> X-Google-Sender-Auth: c9f34852bdb4f249
> Message-ID:
> <75c1488c0911181357w4e8efdc2r7e82d8e1c3e03...@mail.gmail.com>
> Subject: Test
> From: Francois MASUREL 
> To: contact-test 
> Content-Type: multipart/related; boundary=001636e90e61aa516e0478ac5398
>
> --001636e90e61aa516e0478ac5398
> Content-Type: multipart/alternative;
> boundary=001636e90e61aa516b0478ac5397
>
> --001636e90e61aa516b0478ac5397
> Content-Type: text/plain; charset=UTF-8
>
> [image:
> ?
>
> ui=2&view=att&th=125094c9bff3199b&attid=0.1&disp=attd&realattid=ii_125094c9bff3199b&zw]
>
> --001636e90e61aa516b0478ac5397
> Content-Type: text/html; charset=UTF-8
> Content-Transfer-Encoding: quoted-printable
>
>  ui=3D2&view=3Datt&th=3D125094c9bff3199b&attid=3D=
> 0.1&disp=3Dattd&realattid=3Dii_125094c9bff3199b&zw"
> alt=3D"?ui=
>
> =3D2&view=3Datt&th=3D125094c9bff3199b&attid=3D0.1&disp=3Dat=
> td&realattid=3Dii_125094c9bff3199b&zw"
> src=3D"cid:ii_125094c9bff319=
> 9b">
> 
>
> --001636e90e61aa516b0478ac5397--
> --001636e90e61aa516e0478ac5398
> Content-Type: image/gif; name="enveloppe.gif"
> Content-Transfer-Encoding: base64
> Content-ID: 
> X-Attachment-Id: ii_125094c9bff3199b
>
> R0lGODlhEAAQAPeYAP39/tHR5MrK4fLy99PT6NfX6Nvb6/
> f3+ff3+uDg7sPD3fHx9ra206qqyrKy
> 0eLi8Onp8bCwz
> +jo8cvL4WVlm7y81cLC2fPz96ysyrCwzcXF3c7O487O4tDQ5Le30+fn8cvL4ufn
> 8PDw9/Ly+fDw9PLy+N7e66Skx8/
> P46mpymBglNLS5KKixoWFsYaGs56ewNXV5pqawKyszuTk7pyc
> wdra6Pr6/
> Ht7qnt7qXl5qry82LGx0MbG3pCQsrOz0qyszZubucjI3eLi7eDg6eDg7X5+rJSUu9zc
> 54uLtvz8/b+/2KenyPX1+K6uzpmZwOvr9a6uz8XF3Ovr9tLS5uPj8Hx8qvv7/
> ZiYwLm50NPT5tjY
> 6K+v0M7O5IGBqtzc68/
> P3mpqnoeHsLCwynJypNjY60ZGgJ2dw1hYjc7O4bi40nh4qWlpnNjY5fn5
> +5SUvcTE2oCAqbOz0Le31tzc7L292vj4+sPD2by81unp8s/P5s3N3fPz
> +VxckqOjyHZ2psLC23p6
> pt3d7d7e7PDw+L292JOTu/z8/Kury7Ozyra20ubm8MTE1/b2/Pn5/
> Hd3qJCQvOnp876+2tHR5tHR
> 4oaGrbW10pmZv/7+/v///
> wAA
>
> 
>
> 
>
> 
>
> 
>
> ACH5BAEAAJgALAAQABAA
>
> AAjlADEJHCiwzSQjamYQXIjAAo0fAq5gWChwgB03PiBdkhDkxgKCITywiLLn0qUkU2A4ESPQRKIm
> K5iYNIlHCREefg5gMjNBBICZAKx4SUGlQA4smOJw
> +BAAjaJLABh1aBCITJ8uNmrIuDRAS4Uldwyk
>
> OZQFhQIKXxCcEGLSkCBLOKowoKNDARJKmN5AMQmghIBIEfKAICTnURlMFwotuNQogYYHcwJsYDDm
> DCKBGXZcGiQpgIECf1zwAUJiIIQWI6RwSUAghooeQyi
> +cPCEwBYwcI5QFMimSCVHgPTsHlgnzJpF
> OncHBAA7
> --001636e90e61aa516e0478ac5398--
>
> On Nov 18, 10:34 pm, "Ikai L (Google)"  wrote:
> > François,
> >
> > I'm not familiar with any standards with regard to inline images. Do you
> > have this working outside of App Engine? Do you have working code from
> that?
> > If there are discrepancies in our implementation of javax.mail.* and
> > standard implementations we should be aware.
> >
> > On Mon, Nov 16, 2009 at 10:30 PM, mably  wrote:
> > > In fact standards attachments works.
> >
> > > But it's not what I'm trying do do.
> >
> > > I'm need to relay HTML messages with inline images and this is still
> > > not working.
> >
> > > Is it a kind of limitation of GAE or is it supposed to work ?
> >
> > > Thanx for your help.
> >
> > > François
> >
> > > On 16 nov, 23:09, mably  wrote:
> > > > Hi Ikai, thanx for you help.
> >
> > > > I've copy pasted your code on my webapp (webwinewatch), I've no error
> > > > but the mail isn't correctly relayed, all attachements are removed.
> >
> > > > I've just modified the sender, from and recipient lines :
> >
> > > > message.setSender(new InternetAddress("@gmail.com",
> "Relay
> > > > account"));
> > > > message.setFrom(message.getSender());
> > > > message.setRecipient(Message.RecipientType.TO, message.getSender());
> >
> > > > Do you have any clue ?
> >
> > > > You can test it, sending an email to : contact-
> > > > t...@webwinewatch.appspotmail.com
> >
> > > > Thanx again for your help.
> >
> > > > On 16 nov, 21:35, "Ikai L (Google)"  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 documenta

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-18 Thread Ikai L (Google)
That's good information to know. We are not using Sun's JavaMail
implementation, so it's possible there are points of incompatibility.
There's one last thing you may want to try:

http://code.google.com/appengine/docs/java/javadoc/com/google/appengine/api/mail/package-summary.html

The above page documents the Low-Level API. Try creating an email object via
that API, setting the Content-ID of the attachment and the HTML tag to point
to the cid. You're already doing that in the example you sent using
javax.mail, but I'm wondering if the low-level API is capable of doing this.
If not, I'll open an issue.

On Wed, Nov 18, 2009 at 3:06 PM, mably  wrote:

> Of course, the code above doesn't need to be run in mail handler
> servlet :-)
>
> On Nov 19, 12:01 am, mably  wrote:
> > I've written a simple class sending an HTML email with an embedded
> > image (cf. below) via GMail SMTP.  It works perfectly fine when run
> > locally from Eclipse.
> >
> > The same code in a GAE mail servlet handler produce the infamous
> > "Converting attachment data failed" error.
> >
> > So it really seems to be a problem in GAE javamail implementation.
> >
> > /*
> >  * 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 .
> >
> >  */
> >
> > import java.io.ByteArrayOutputStream;
> > import java.io.IOException;
> > import java.io.InputStream;
> > import java.util.Properties;
> >
> > import javax.mail.Message;
> > 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;
> >
> > /**
> >  * @author Francois
> >  *
> >  */
> > public class GMailSMTP {
> >
> > private static final String SMTP_HOST_NAME = "smtp.gmail.com";
> > private static final int SMTP_HOST_PORT = 465;
> > private static final String SMTP_AUTH_USER = "x...@gmail.com";
> > private static final String SMTP_AUTH_PWD  = "yyy";
> >
> > public static void main(String[] args) throws Exception{
> >new GMailSMTP().test();
> > }
> >
> > public void test() throws Exception{
> > Properties props = new Properties();
> >
> > props.put("mail.transport.protocol", "smtps");
> > props.put("mail.smtps.host", SMTP_HOST_NAME);
> > props.put("mail.smtps.auth", "true");
> > // props.put("mail.smtps.quitwait", "false");
> >
> > String from = SMTP_AUTH_USER;
> > String to = SMTP_AUTH_USER;
> > String subject = "Testing SMTP-SSL";
> > String htmlText = "Hello";
> > byte[] imgData = this.obtainByteData("enveloppe.gif");
> >
> > Session mailSession = Session.getDefaultInstance(props);
> > mailSession.setDebug(true);
> >
> > Message msg = new MimeMessage(mailSession);
> > msg.setFrom(new InternetAddress(from));
> > msg.addRecipient(Message.RecipientType.TO, new InternetAddress
> > (to));
> > msg.setSubject(subject);
> >
> > Multipart mp = new MimeMultipart("related");
> >
> > MimeBodyPart htmlPart = new MimeBodyPart();
> > htmlPart.setContent(htmlText, "text/html");
> > mp.addBodyPart(htmlPart);
> >
> > MimeBodyPart attachment = new MimeBodyPart();
> > attachment.setFileName("test.gif");
> > attachment.setContent(imgData, "image/gif");
> > attachment.setHeader("Content-ID","");
> > mp.addBodyPart(attachment);
> >
> > msg.setContent(mp);
> >
> > Transport transport = mailSession.getTransport();
> >
> > transport.connect
> >   (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER,
> > SMTP_AUTH_PWD);
> >
> > transport.sendMessage(msg,
> > msg.getRecipients(Message.RecipientType.TO));
> >
> > transport.close();
> > }
> >
> > /**
> >  * Constructs a byte array and fills it with data that is read
> > from the
> >  * specified resource.
> >  * @param filename the path to the resource
> >  * @return the specified resource as a byte array
> >  * @throws java.io.IOException if the resource cannot be read, or
> > the
> >  *   bytes cannot be written, or the streams cannot be closed
> >  */
> > private byte[]

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-19 Thread Ikai L (Google)
Looks like you're right. As far as I know there are no hidden methods.

Another workaround for this limitation could be to store incoming images in
the data store on an incoming email, then reference them in the emails that
get sent out. It's not ideal, but it may be the solution to what you are
looking for.

On Wed, Nov 18, 2009 at 3:36 PM, mably  wrote:

> I haven't found a way to set a Content-ID header on a mimebodypart/
> attachment using the low-level API.  The MailService.Attachment class
> is quite rudimentary (only filename and data).
>
> May be some part of the low level API are still undocumented ?
>
> Thanx for all.
>
> On Nov 19, 12:25 am, "Ikai L (Google)"  wrote:
> > That's good information to know. We are not using Sun's JavaMail
> > implementation, so it's possible there are points of incompatibility.
> > There's one last thing you may want to try:
> >
> > http://code.google.com/appengine/docs/java/javadoc/com/google/appengi...
> >
> > The above page documents the Low-Level API. Try creating an email object
> via
> > that API, setting the Content-ID of the attachment and the HTML tag to
> point
> > to the cid. You're already doing that in the example you sent using
> > javax.mail, but I'm wondering if the low-level API is capable of doing
> this.
> > If not, I'll open an issue.
> >
> > On Wed, Nov 18, 2009 at 3:06 PM, mably  wrote:
> > > Of course, the code above doesn't need to be run in mail handler
> > > servlet :-)
> >
> > > On Nov 19, 12:01 am, mably  wrote:
> > > > I've written a simple class sending an HTML email with an embedded
> > > > image (cf. below) via GMail SMTP.  It works perfectly fine when run
> > > > locally from Eclipse.
> >
> > > > The same code in a GAE mail servlet handler produce the infamous
> > > > "Converting attachment data failed" error.
> >
> > > > So it really seems to be a problem in GAE javamail implementation.
> >
> > > > /*
> > > >  * 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/>.
> >
> > > >  */
> >
> > > > import java.io.ByteArrayOutputStream;
> > > > import java.io.IOException;
> > > > import java.io.InputStream;
> > > > import java.util.Properties;
> >
> > > > import javax.mail.Message;
> > > > 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;
> >
> > > > /**
> > > >  * @author Francois
> > > >  *
> > > >  */
> > > > public class GMailSMTP {
> >
> > > > private static final String SMTP_HOST_NAME = "smtp.gmail.com";
> > > > private static final int SMTP_HOST_PORT = 465;
> > > > private static final String SMTP_AUTH_USER = "x...@gmail.com";
> > > > private static final String SMTP_AUTH_PWD  = "yyy";
> >
> > > > public static void main(String[] args) throws Exception{
> > > >new GMailSMTP().test();
> > > > }
> >
> > > > public void test() throws Exception{
> > > > Properties props = new Properties();
> >
> > > > props.put("mail.transport.protocol", "smtps");
> > > > props.put("mail.smtps.host", SMTP_HOST_NAME);
> > > > props.put("mail.smtps.auth", "true");
> > > > // props.put("mail.smtps.quitwait", "false");
> >
> > > > String from = SMTP_AUTH_USER;
> > > > String to = SMTP_AUTH_USER;
> > > > String subject = "Testing SMTP-SSL";
> > > > String htmlText = "Hello";
> > > > byte[] imgData = this.obtainByteData("enveloppe.gif");
> >
> > > > Session mailSession = Session.getDefaultInstance(props);
> > > > mailSession.setDebug(true);
> >
> > > > Message msg = new MimeMessage(mailSession);
> > > > msg.setFrom(new InternetAddress(from));
> > > > msg.addRecipient(Message.RecipientType.TO, new
> InternetAddress
> > > > (to));
> > > > msg.setSubject(subject);
> >
> > > > Multipart mp = new MimeMultipart("related");
> >
> > > > MimeBodyPart htmlPart = new MimeBodyPart();
> > > > htmlPart.setContent(htmlText, "text/html");
> > > > mp.addBodyPart(htmlPart);
> >
> > > > MimeBodyPart attachment

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-19 Thread Ikai L (Google)
We'll always try to open source what makes sense. I'm not sure it'd make
sense to open source all GAE libraries since many of the calls talk to
services. The javax.mail.* could fall under the "makes sense" category.

On Thu, Nov 19, 2009 at 12:07 PM, mably  wrote:

> By the way, do you know if Google plans to open source some parts of
> GAE librairies so we could investigate deeper when we encounter such a
> problem ?
>
> On 19 nov, 21:01, mably  wrote:
> > Any chance to see this bug fixed in the future ?
> >
> > Is there any documentation on the meaning of "Converting
> > attachment data failed" error message ?
> >
> > On 19 nov, 20:55, "Ikai L (Google)"  wrote:
> >
> >
> >
> > > Looks like you're right. As far as I know there are no hidden methods.
> >
> > > Another workaround for this limitation could be to store incoming
> images in
> > > the data store on an incoming email, then reference them in the emails
> that
> > > get sent out. It's not ideal, but it may be the solution to what you
> are
> > > looking for.
> >
> > > On Wed, Nov 18, 2009 at 3:36 PM, mably  wrote:
> > > > I haven't found a way to set a Content-ID header on a mimebodypart/
> > > > attachment using the low-level API.  The MailService.Attachment class
> > > > is quite rudimentary (only filename and data).
> >
> > > > May be some part of the low level API are still undocumented ?
> >
> > > > Thanx for all.
> >
> > > > On Nov 19, 12:25 am, "Ikai L (Google)"  wrote:
> > > > > That's good information to know. We are not using Sun's JavaMail
> > > > > implementation, so it's possible there are points of
> incompatibility.
> > > > > There's one last thing you may want to try:
> >
> > > > >
> http://code.google.com/appengine/docs/java/javadoc/com/google/appengi...
> >
> > > > > The above page documents the Low-Level API. Try creating an email
> object
> > > > via
> > > > > that API, setting the Content-ID of the attachment and the HTML tag
> to
> > > > point
> > > > > to the cid. You're already doing that in the example you sent using
> > > > > javax.mail, but I'm wondering if the low-level API is capable of
> doing
> > > > this.
> > > > > If not, I'll open an issue.
> >
> > > > > On Wed, Nov 18, 2009 at 3:06 PM, mably  wrote:
> > > > > > Of course, the code above doesn't need to be run in mail handler
> > > > > > servlet :-)
> >
> > > > > > On Nov 19, 12:01 am, mably  wrote:
> > > > > > > I've written a simple class sending an HTML email with an
> embedded
> > > > > > > image (cf. below) via GMail SMTP.  It works perfectly fine when
> run
> > > > > > > locally from Eclipse.
> >
> > > > > > > The same code in a GAE mail servlet handler produce the
> infamous
> > > > > > > "Converting attachment data failed" error.
> >
> > > > > > > So it really seems to be a problem in GAE javamail
> implementation.
> >
> > > > > > > /*
> > > > > > >  * 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/>.
> >
> > > > > > >  */
> >
> > > > > > > import java.io.ByteArrayOutputStream;
> > > > > > > import java.io.IOException;
> > > > > > > import java.io.InputStream;
> > > > > > > import java.util.Properties;
> >
> > > > > > > import javax.mail.Message;
> > > > > > > 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;
> >
> > > > > > > /**
> > > > > > >  * @author Francois
> > > > > > >  *
> > > > > > >  */
> > > > > > > public class GMailSMTP {
> >
> > > > > > > private static final String SMTP_HOST_NAME = "
> smtp.gmail.com";
> > > > > > > private static final int SMTP_HOST_PORT = 465;
> > > > > > > private static final String SMTP_AUTH_USER = "
> x...@gmail.com";
> > > > > > > private static final String SMTP_AUTH_PWD  = "yyy";
> >
> > > > > > > public static void main(String[] args) throws Exception{
> > > > > > >new GMailSMTP().test();
> > > > > > > }
> >
> > > > > > > public void test() throws Exception{
> > >

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-11-30 Thread Ikai L (Google)
Interesting, so this did not work?

attachment.setDataHandler(new DataHandler(mimePartDataSource));
attachment.setFileName("ticker.png");

I'll need to do some research into the Java mail spec to see if we are
matching it. Could be a bug if we aren't.

On Thu, Nov 26, 2009 at 12:48 PM, Don  wrote:

> 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)"  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 = "your.sen...@domain.com";
> > private static final String RECIPIENT = "your.recipi...@domain.com";
> >
> > 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  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("lydonchan...@gmail.com", "dons
> > > s

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-12-17 Thread Ikai L (Google)
It doesn't seem like it should cause issues, but if you have a reproducible
test case please let us know.

On Tue, Dec 15, 2009 at 6:06 AM, Don  wrote:

> Hi Ikai,
>
> So does the order of setDataHandler + setFileName cause problems as I
> wrote above?
>
> Regards
>
>
> On Dec 1, 5:25 am, "Ikai L (Google)"  wrote:
> > Interesting, so this did not work?
> >
> > attachment.setDataHandler(new DataHandler(mimePartDataSource));
> > attachment.setFileName("ticker.png");
> >
> > I'll need to do some research into the Java mail spec to see if we are
> > matching it. Could be a bug if we aren't.
> >
> > On Thu, Nov 26, 2009 at 12:48 PM, Don  wrote:
> > > 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)"  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 = "your.sen...@domain.com";
> > > > private static final String RECIPIENT = "
> your.recipi...@domain.com";
> >
> > > > 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

Re: [appengine-java] Re: Can't send mail with attachment using JavaMail API

2009-12-31 Thread mnr ngsk
Thanks your quick response.
I didn't find that issue.


2009/12/31 minor-undroid 

> Sorry, I made a wrting mistake.
>
> I cannot send the HTML mail with inline image,
> and cannot send the HTML mail with attachement file, too.
> (I could send the plain text mail with an attachment image
>  and HTML mail without attachment file)
>
> I think It's only GAE's problem.
> Do you have any idea?
>
>
> On Dec 31, 11:23 pm, minor-undroid  wrote:
> > Hi there,
> >
> > I reproduced same problem.
> > I cannotsendattachmentfile and inline image with HTMLmail.
> > (Of course, I couldsendplain textmailwithoutattachmentand
> >  HTMLmailwithout inline image)
> > I don't have any idea.  It would be very helpful to tell me anything.
> >
> > I build the mime message in HtmlMimeMessage#createMimeMessage method.
> > My code is as follows,
> > /
> >
> */
> > import java.io.ByteArrayOutputStream;
> > import java.io.IOException;
> > import java.io.InputStream;
> > import java.net.MalformedURLException;
> > import java.net.URL;
> > import java.util.ArrayList;
> > import java.util.regex.Matcher;
> > import java.util.regex.Pattern;
> > import java.util.regex.PatternSyntaxException;
> >
> > import javax.activation.DataHandler;
> > import javax.mail.MessagingException;
> > import javax.mail.internet.MimeBodyPart;
> > import javax.mail.internet.MimeMessage;
> > import javax.mail.internet.MimeMultipart;
> > import javax.mail.util.ByteArrayDataSource;
> >
> > import com.mnrngsk.test20091217.mail.CommonData;
> > import com.mnrngsk.test20091217.mail.DbgLog;
> >
> > public class HtmlMimeMessage {
> >
> > private static final String CID_DOMAIN  =
> "@xxx";
> > private static final String REGEX_IMGTAG=
> " > [^'\">])*src=(\"[^\"]*\"|'[^']*'|[^'\">])*/?>";
> >
> > private MimeMessage mmHtmlMsg = null;
> > private MimeMultipart   mmpHtmlMail = null;
> > private String  strHtmlSrc = "";
> > private boolean bAttach = false;
> > private String  strAttach = "";
> >
> > private ArrayList replacedImgTagList
> > = new
> ArrayList();
> >
> > // constructor
> > public HtmlMimeMessage(MimeMessage msg, boolean bAttach) {
> > this.mmHtmlMsg = msg;
> > this.bAttach = bAttach;
> > }
> >
> > public void setSource(String strSrc) {
> > this.strHtmlSrc = strSrc;
> > }
> >
> > public void setAttachment(String strAttach) {
> > if (this.bAttach) {
> > this.strAttach = strAttach;
> > }
> > }
> >
> > private void addInlineImageTo(MimeMultipart mmp) {
> > int iImgSrcCount = this.replacedImgTagList.size();
> > for (int i = 0; i < iImgSrcCount; i++) {
> > DbgLog.info("HtmlMimeMessage#addInlineImageTo;
> Inline Image #" +
> > Integer.toString(i));
> >
> > ImageMessageBodyFactory imageMbpFactory = new
> > ImageMessageBodyFactory(this.replacedImgTagList.get(i));
> > MimeBodyPart mbpImg =
> imageMbpFactory.createMessageBody();
> > try {
> > mmp.addBodyPart(mbpImg);
> >
> DbgLog.info("HtmlMimeMessage#addInlineImageTo;\n
> > mbpImg#ContentType="
> > + mbpImg.getContentType()
> + "\n  this.mmpHtmlMail#ContentType="
> > + mmp.getContentType());
> > } catch (MessagingException e) {
> > e.printStackTrace();
> > DbgLog.warning("HtmlMimeMessage#addInlineImageTo;
> > MessagingException; " + e.getMessage());
> > }
> > }
> > }
> >
> > public boolean createMimeMessage() {
> > boolean bRes = false;
> >
> > try {
> > TextByHtmlMessageBodyFactory textMbpFactory = new
> > TextByHtmlMessageBodyFactory(this.strHtmlSrc);
> > MimeBodyPart mbpText =
> textMbpFactory.createMessageBody();
> >
> > HtmlMessageBodyFactory htmlMbpFactory = new
> HtmlMessageBodyFactory
> > (this.strHtmlSrc, replacedImgTagList);
> > MimeBodyPart mbpHtml =
> htmlMbpFactory.createMessageBody();
> >
> > MimeMultipart mmpMixed = null;
> > MimeBodyPart mbpAttach = null;
> >
> > if (this.bAttach) {
> >
> DbgLog.info("HtmlMimeMessage#createMimeMessage; wAttachment
> > (mixed);");
> >
> > AttachedMessageBodyFactory
> attachMbpFactory = new
> > A