Don't forget to star issue : 
http://code.google.com/p/googleappengine/issues/detail?id=965

On Dec 31, 3:23 pm, minor-undroid <mnrn...@gmail.com> 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              = 
> "@xxxxxxx";
>         private static final String             REGEX_IMGTAG    = 
> "<img(\"[^\"]*\"|'[^']*'|
> [^'\">])*src=(\"[^\"]*\"|'[^']*'|[^'\">])*/?>";
>
>         private MimeMessage                     mmHtmlMsg = null;
>         private MimeMultipart                   mmpHtmlMail = null;
>         private String                  strHtmlSrc = "";
>         private boolean                         bAttach = false;
>         private String                  strAttach = "";
>
>         private ArrayList<ReplacedImgTag> replacedImgTagList
>                                                         = new 
> ArrayList<ReplacedImgTag>();
>
>         // 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()
>                                                 );
>                         }
>
>                         int iImgSrcCount = this.replacedImgTagList.size();
>                         if (iImgSrcCount > 0) {
>                                 MimeMultipart mmpHtmlAndImg = new 
> MimeMultipart("related");
>
>                                 if (mbpHtml != null) {
>                                         mmpHtmlAndImg.addBodyPart(mbpHtml);
>                                         
> DbgLog.info("HtmlMimeMessage#createMimeMessage;"
>                                                         + "\n  
> mbpHtml#ContentType=" + mbpHtml.getContentType()
>                                                         + "\n  
> mmpHtmlAndImg#ContentType=" +
> mmpHtmlAndImg.getContentType()
>                                                         );
>                                 }
>
>                                 addInlineImageTo(mmpHtmlAndImg);
>
>                                 MimeBodyPart mbpHtmlAndImg = new 
> MimeBodyPart();
>                                 mbpHtmlAndImg.setContent(mmpHtmlAndImg);
>                                 mmpAlter.addBodyPart(mbpHtmlAndImg);
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage;"
>                                                 + "\n  
> mbpHtmlAndImg#ContentType=" + mbpHtmlAndImg.getContentType
> ()
>                                                 + "\n  mmpAlter#ContentType=" 
> + mmpAlter.getContentType()
>                                                 );
>                         }
>                         else {
>                                 if (mbpHtml != null) {
>                                         mmpAlter.addBodyPart(mbpHtml);
>                                         
> DbgLog.info("HtmlMimeMessage#createMimeMessage;"
>                                                         + "\n  
> mbpHtml#ContentType=" + mbpHtml.getContentType()
>                                                         + "\n  
> mmpAlter#ContentType=" + mmpAlter.getContentType()
>                                                         );
>                                 }
>                         }
>
>                         if (this.bAttach) {
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage; w Attachment
> (mixed);");
>
>                                 MimeBodyPart mbpAlter = new MimeBodyPart();
>                                 mbpAlter.setContent(mmpAlter);
>
>                                 mmpMixed.addBodyPart(mbpAlter);
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage;"
>                                                 + "\n  mbpAlter#ContentType=" 
> + mbpAlter.getContentType()
>                                                 + "\n  mmpMixed#ContentType=" 
> + mmpMixed.getContentType()
>                                                 );
>
>                                 mmpMixed.addBodyPart(mbpAttach);
>                                 this.mmHtmlMsg.setContent(mmpMixed);
>                                 this.mmHtmlMsg.saveChanges();
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage;"
>                                                 + "\n 
> this.mmHtmlMsg#ContentType=" +
> this.mmHtmlMsg.getContentType()
>                                                 + "\n===Preamble==>\n" + 
> mmpMixed.getPreamble() +
> "\n<==Preamble===\n"
>                                                 );
>                                 this.mmpHtmlMail = mmpMixed;
>                         }
>                         else {
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage; w/o Attachment;");
>
>                                 this.mmHtmlMsg.setContent(mmpAlter);
>                                 this.mmHtmlMsg.saveChanges();
>                                 
> DbgLog.info("HtmlMimeMessage#createMimeMessage;"
>                                                 + "\n 
> this.mmHtmlMsg#ContentType=" +
> this.mmHtmlMsg.getContentType()
>                                                 + "\n===Preamble==>\n" + 
> mmpAlter.getPreamble() +
> "\n<==Preamble===\n"
>                                                 );
>                                 this.mmpHtmlMail = mmpAlter;
>                         }
>
>                 } catch (MessagingException e) {
>                         e.printStackTrace();
>                 DbgLog.warning("HtmlMimeMessage#createHtmlMessage;
> MessagingException; " + e.getMessage());
>                 }
>
>         return bRes;
>         }
>
>         //////////////////////////////////////////////////////////////////
>
>         class ReplacedImgTag {
>                 public String           strImgTagOrg = "";
>                 public String           strImgTagCID = "";
>                 public String           strImgSrcOrg = "";
>                 public String           strImgSrcCID = "";
>                 public String           strImgSrcFilename = "";
>
>                 // constructor
>                 public ReplacedImgTag(String tagOrg, String srcOrg) {
>                         this.strImgTagOrg = tagOrg;
>                         this.strImgSrcCID = getImageContentId(srcOrg);
>                         this.strImgTagCID = "<img src=\"cid:" + 
> this.strImgSrcCID + "\" />";
>
>                         this.strImgSrcOrg = srcOrg;
>                         this.strImgSrcFilename = 
> getFilename(this.strImgSrcOrg);
>                 }
>
>                 private String getImageContentId(String strFilename) {
>                         int hashFilename = strFilename.hashCode();
>                         String strRes = Integer.toHexString(hashFilename) + 
> CID_DOMAIN;
>                         return strRes;
>                 }
>
>                 private String getFilename(String strImgSrcOrg) {
>                         String strTmp = new String(strImgSrcOrg);
>                         String strFile[] = strTmp.split("[/|\\\\]");
>                         String strFilename = strTmp;
>                         if (strFile.length > 0) {
>                                 strFilename = strFile[strFile.length - 1];
>                         }
>                         return strFilename;
>                 }
>         }
>
>         //////////////////////////////////////////////////////////////////
>
>         class TextByHtmlMessageBodyFactory {
>
>                 private String          strHtmlSrc = "";
>
>                 // constructor
>                 public TextByHtmlMessageBodyFactory (String strHtmlSrc) {
>                         this.strHtmlSrc = strHtmlSrc;
>                 }
>
>                 private String convertHtmlTag(String strMsgbody, String regex,
> String str) {
>                         String strRes = strMsgbody;
>                         Pattern p = Pattern.compile(regex);
>
>                         Matcher m = p.matcher(strMsgbody);
>                         while (m.find()) {
>                         }
>                         strRes = m.replaceAll(str);
>
>                         return strRes;
>                 }
>
>                 private String createPlainText(String strHtmlSrc) {
>                         String strRes = convertHtmlTag(strHtmlSrc, 
> REGEX_IMGTAG, "[img]");
>                         strRes = convertHtmlTag(strRes, "<br\\s?/?>", "\n");
>                         strRes = convertHtmlTag(strRes, 
> "<(\"[^\"]*\"|'[^']*'|[^'\">])*>",
> "");
>
>                         return strRes;
>                 }
>
>                 public MimeBodyPart createMessageBody() {
>
>                         MimeBodyPart mbp = null;
>                         TextMessageBodyFactory txtMbpFactory = new 
> TextMessageBodyFactory
> ();
>                         
> txtMbpFactory.setText(createPlainText(this.strHtmlSrc));
>                         mbp = txtMbpFactory.createMessageBody();
>
>                         try {
>                                 
> DbgLog.info("TextByHtmlMessageBodyFactory#createMessageBody;\n
> mbp#ContentType="
>                                                 + mbp.getContentType());
>                         } catch (MessagingException e) {
>                                 e.printStackTrace();
>                         }
>
>                         return mbp;
>                 }
>         }
>
>         //////////////////////////////////////////////////////////////////
>
>         class ImageMessageBodyFactory {
>
>                 private static final String             REGEX_HTTP      = 
> "^https?://.*";
>
>                 private ReplacedImgTag replacedImgTag = null;
>
>                 // constructor
>                 public ImageMessageBodyFactory (ReplacedImgTag 
> replacedImgTag) {
>                         this.replacedImgTag = replacedImgTag;
>                 }
>
>                 private byte[] getImageByteArray() {
>                         String strImgSrc = this.replacedImgTag.strImgSrcOrg;
>                         byte[] rawData = null;
>
>                         if (strImgSrc.matches(REGEX_HTTP)) {
>                                 URL url = null;
>                                 ByteArrayOutputStream output = null;
>                                 try {
>                                         url = new 
> URL("https://www.google.com/accounts/ah/
> appengine.jpg");
>                                         InputStream in;
>                                         in = url.openStream();
>
>                                         int len;
>                                         byte[] buffer = new byte[8192];
>                                         output = new ByteArrayOutputStream();
>
>                                         while ((len = in.read(buffer, 0, 
> buffer.length)) != -1) {
>                                         output.write(buffer, 0, len);
>                                         }
>                                         rawData = output.toByteArray();
>
>                                 } catch (MalformedURLException e) {
>                                         e.printStackTrace();
>                                         
> DbgLog.warning("ImageMessageBodyFactory#getImageDataHandler;
> MalformedURLException; " + e.getMessage());
>                                 } catch (IOException e) {
>                                         e.printStackTrace();
>                                         
> DbgLog.warning("ImageMessageBodyFactory#getImageDataHandler;
> IOException; " + e.getMessage());
>                                 }
>                         }
>                         else {
>                         }
>
>                         return rawData;
>                 }
> ...
>
> read more »

--

You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.


Reply via email to