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;
}
public MimeBodyPart createMessageBody() {
MimeBodyPart mbp = null;
try {
DbgLog.info("JavaMail Resource :" +
javax.activation.DataHandler.class.getResource("DataHandler.class"));
mbp = new MimeBodyPart();
String strType = "image/gif";
ByteArrayDataSource dataSource = new ByteArrayDataSource
(getImageByteArray(), strType);
String strFilename =
this.replacedImgTag.strImgSrcFilename.toLowerCase();
mbp.setFileName(strFilename);
String strContentType = strType + "; name=\"" +
strFilename +
"\"";
mbp.setHeader("Content-Type", strContentType);
String strContentDisposition = "inline; filename=\"" +
strFilename + "\"";
mbp.setHeader("Content-Disposition",
strContentDisposition);
String strContentID = this.replacedImgTag.strImgSrcCID;
mbp.setContentID("<" + strContentID + ">");
mbp.setHeader("Content-Transfer-Encoding",
"base64");
mbp.setDataHandler(new DataHandler(dataSource));
String str1 = mbp.getContentType();
String str2 = mbp.getEncoding();
String str3 = mbp.getContentID();
String strtmp = str3;
DbgLog.info("ImageMessageBodyFactory#createMessageBody;\n
mbp#ContentType="
+ mbp.getContentType());
} catch (MessagingException e) {
e.printStackTrace();
DbgLog.warning("HtmlMimeMessage#createImageBodyPart;
MessagingException; " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
DbgLog.warning("HtmlMimeMessage#createImageBodyPart;
Exception; " + e.getMessage());
}
return mbp;
}
}
//////////////////////////////////////////////////////////////////
class HtmlMessageBodyFactory {
private String strHtmlSrc = "";
private String strHtmlForMail = "";
private ArrayList<ReplacedImgTag> replacedImgTagList = null;
// constructor
public HtmlMessageBodyFactory (String strHtmlSrc,
ArrayList<ReplacedImgTag> imgTagList) {
this.strHtmlSrc = strHtmlSrc;
this.replacedImgTagList = imgTagList;
}
private boolean createHtmlForMail(String strHtmlSrc) {
boolean bRes = false;
StringBuffer strbuffHtmlForMail = new
StringBuffer(strHtmlSrc);
try {
Pattern pattern = Pattern.compile(REGEX_IMGTAG);
Matcher matcher = pattern.matcher(strHtmlSrc);
while (matcher.find()) {
String strImgTagOrg = matcher.group();
String strSrcPath = "";
Pattern reSrcAttr =
Pattern.compile("(src=)\"(.*?)\"");
Matcher matchSrcAttr =
reSrcAttr.matcher(strImgTagOrg);
if (matchSrcAttr.find()) {
strSrcPath =
matchSrcAttr.group(2);
}
this.replacedImgTagList.add(new
ReplacedImgTag(strImgTagOrg,
strSrcPath));
}
int iImgSrcCount =
this.replacedImgTagList.size();
for (int i = 0; i < iImgSrcCount; i++) {
Pattern p =
Pattern.compile(replacedImgTagList.get
(i).strImgTagOrg, Pattern.LITERAL);
Matcher m =
p.matcher(strbuffHtmlForMail.toString());
if (m.find()) {
strbuffHtmlForMail.replace(m.start(), m.end(),
this.replacedImgTagList.get(i).strImgTagCID);
}
m.reset();
}
bRes = true;
}
catch (PatternSyntaxException e) {
e.printStackTrace();
DbgLog.warning("HtmlMessageBodyFactory#createHtmlForMail;
MessagingException; " + e.getMessage());
}
this.strHtmlForMail = strbuffHtmlForMail.toString();
return bRes;
}
private MimeBodyPart createHtmlMessageBody(String strHtml) {
MimeBodyPart mbp = new MimeBodyPart();
try {
mbp.setContent(strHtml, "text/html; charset=" +
CommonData.CHARSET);
} catch (MessagingException e) {
e.printStackTrace();
DbgLog.warning("HtmlMessageBodyFactory#createHtmlMessageBody;
MessagingException; " + e.getMessage());
}
return mbp;
}
public MimeBodyPart createMessageBody() {
MimeBodyPart mbp = null;
if (createHtmlForMail(this.strHtmlSrc)) {
mbp =
createHtmlMessageBody(this.strHtmlForMail);
try {
DbgLog.info("HtmlMessageBodyFactory#createMessageBody;\n
mbp#ContentType="
+ mbp.getContentType());
} catch (MessagingException e) {
e.printStackTrace();
}
}
return mbp;
}
}
}
/
*************************************************************************************/
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import com.mnrngsk.test20091217.mail.CommonData;
import com.mnrngsk.test20091217.mail.DbgLog;
public class TextMessageBodyFactory {
private static final String ENCODING =
"7bit";
private static final String CONTENT_TYPE = "text/plain";
private static final String CHARSET =
CommonData.CHARSET;
private String strTextSrc = "";
// constructor
public TextMessageBodyFactory () {
}
public void setText(String strSrc) {
this.strTextSrc = strSrc;
}
public MimeBodyPart createMessageBody() {
MimeBodyPart mbp = new MimeBodyPart();
try {
mbp.setHeader("Content-Transfer-Encoding", ENCODING);
// mbp.addHeaderLine("Content-Type: " + CONTENT_TYPE +
//
"; charset=" + CHARSET);
mbp.setHeader("Content-Type", CONTENT_TYPE + ";
charset=" +
CHARSET);
mbp.setText(this.strTextSrc, CHARSET);
} catch (MessagingException e) {
e.printStackTrace();
DbgLog.warning("TextMessageBodyFactory#createMessageBody;
MessagingException; " + e.getMessage());
}
return mbp;
}
}
/
*************************************************************************************/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import com.mnrngsk.test20091217.mail.DbgLog;
public class AttachedMessageBodyFactory {
private MimeMultipart mmpMsg = null;
private String strAttachment = "";
public AttachedMessageBodyFactory() {
this.mmpMsg = new MimeMultipart("mixed");
}
public MimeMultipart getMultipart() {
return this.mmpMsg;
}
private byte[] getByteArray(String strSrc) {
byte[] rawData = null;
URL url = null;
ByteArrayOutputStream output = null;
try {
url = new URL(strSrc);
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);
}
output.flush();
rawData = output.toByteArray();
} catch (MalformedURLException e) {
e.printStackTrace();
DbgLog.warning("AttachedMessageBodyFactory#getByteArray;
MalformedURLException; " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
DbgLog.warning("AttachedMessageBodyFactory#getByteArray;
IOException; " + e.getMessage());
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
DbgLog.warning("AttachedMessageBodyFactory#getByteArray;
IOException #2; " + e.getMessage());
}
}
return rawData;
}
public void setAttachment(String strSrc) {
this.strAttachment = strSrc;
}
public MimeBodyPart createMessageBody() {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(this.strAttachment);
try {
mbp = new MimeBodyPart();
String strType = fds.getContentType();
byte[] rawData = getByteArray(this.strAttachment);
ByteArrayDataSource dataSource = new
ByteArrayDataSource(rawData,
strType);
mbp.setFileName(fds.getName());
mbp.setDataHandler(new DataHandler(dataSource));
String str1 = mbp.getContentType();
String str2 = mbp.getEncoding();
String str3 = mbp.getContentID();
String strtmp = str3;
} catch (MessagingException e) {
e.printStackTrace();
DbgLog.warning("AttachedMessageBodyFactory#createMessageBody;
MessagingException; " + e.getMessage());
}
return mbp;
}
}
/
*************************************************************************************/
On Dec 18, 7:12 am, "Ikai L (Google)" <[email protected]> wrote:
> 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 <[email protected]> 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)" <[email protected]> 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 <[email protected]> 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)" <[email protected]> wrote:
> > > > > Don,
>
> > > > > First, a word of caution: you'll probably want to contact the
> > creators of
> > > > > the site you are trying to fetch the image from if you haven't done
> > so
> > > > > already. Their terms of service prohibit the use of automatic
> > downloading
> > > > of
> > > > > images:http://support.stockcharts.com/forums/31090/entries/20485
>
> > > > > To fetch an image from a URL and send it via the Mail Service, this
> > what
> > > > you
> > > > > need to do:
>
> > > > > 1. Fetch your URL
> > > > > 2. Find the content type
> > > > > 3. Read the stream into a byte[]
> > > > > 4. Create a message and a data handler
> > > > > 5. Pass the byte[] into the data source, then into the data handler
>
> > > > > Example code:http://pastie.org/712159
>
> > > > > import javax.servlet.http.HttpServlet;
> > > > > import javax.servlet.http.HttpServletRequest;
> > > > > import javax.servlet.http.HttpServletResponse;
> > > > > import javax.servlet.ServletException;
> > > > > import javax.mail.*;
> > > > > import javax.mail.util.ByteArrayDataSource;
> > > > > import javax.mail.internet.MimeMultipart;
> > > > > import javax.mail.internet.MimeBodyPart;
> > > > > import javax.mail.internet.MimeMessage;
> > > > > import javax.mail.internet.InternetAddress;
> > > > > import javax.activation.DataHandler;
> > > > > import java.io.IOException;
> > > > > import java.io.InputStream;
> > > > > import java.io.ByteArrayOutputStream;
> > > > > import java.net.URL;
> > > > > import java.util.Properties;
>
> > > > > public class GetStockServlet extends HttpServlet {
> > > > > private static final String SENDER = "[email protected]";
> > > > > private static final String RECIPIENT = "
> > [email protected]";
>
> > > > > protected void doGet(HttpServletRequest request,
> > HttpServletResponse
> > > > > response) throws ServletException, IOException {
>
> > > > > URL url = new URL("http://yoururl.com/image.png");
> > > > > InputStream in = url.openStream();
>
> > > > > byte[] rawData;
> > > > > int len;
> > > > > byte[] buffer = new byte[8192];
> > > > > ByteArrayOutputStream output = new ByteArrayOutputStream();
>
> > > > > try {
> > > > > while ((len = in.read(buffer, 0, buffer.length)) != -1)
> > > > > output.write(buffer, 0, len);
> > > > > rawData = output.toByteArray();
> > > > > } finally {
> > > > > output.close();
> > > > > }
>
> > > > > response.setContentType("image/png");
> > > > > response.getOutputStream().write(rawData);
> > > > > response.getOutputStream().flush();
>
> > > > > String htmlBody = "Here is the quote you wanted";
>
> > > > > Properties props = new Properties();
> > > > > Session session = Session.getDefaultInstance(props, null);
>
> > > > > Message message = new MimeMessage(session);
>
> > > > > Multipart mp = new MimeMultipart();
>
> > > > > MimeBodyPart htmlPart = new MimeBodyPart();
>
> > > > > try {
> > > > > message.setFrom(new InternetAddress(SENDER, "Stock
> > > > Service"));
> > > > > message.addRecipient(Message.RecipientType.TO, new
> > > > > InternetAddress(RECIPIENT));
> > > > > message.setSubject("Stock Quote: " + symbol);
>
> > > > > htmlPart.setContent(htmlBody, "text/html");
> > > > > mp.addBodyPart(htmlPart);
>
> > > > > ByteArrayDataSource dataSource = new
> > > > > ByteArrayDataSource(rawData, "image/png");
>
> > > > > MimeBodyPart attachment = new MimeBodyPart();
> > > > > attachment.setFileName(symbol + ".png");
> > > > > attachment.setDataHandler(new
> > > > > DataHandler(dataSource));
> > > > > mp.addBodyPart(attachment);
>
> > > > > message.setContent(mp);
> > > > > Transport.send(message);
> > > > > } catch (MessagingException e) {
> > > > > throw new IOException(e);
> > > > > }
>
> > > > > }
>
> > > > > }
> > > > > On Sun, Nov 22, 2009 at 9:04 AM, Don <[email protected]> wrote:
> > > > > > Hi Ikai,
>
> > > > > > I tried your example code, but I cannot attach an image on the
> > email
> > > > > > that I send.
> > > > > > There is no conversion error either.
>
> > > > > > Here is snippets of the code, please help..
>
> > > > > > //Retrieving image:
> > > > > > URL url = new URL("http://stockcharts.com/c-sc/sc?s=" + ticker
> > +
> > > > > > "&p=DAILY&b=5&g=0&i=0&r=3528");
> > > > > > //Sending mail
> > > > > > SendMail sendmail = new SendMail(customerEmail, url.openStream());
>
> > > > > > // send mail function
> > > > > > public SendMail(String recipient, InputStream imagestream) {
> > > > > > msg.setFrom( new InternetAddress("[email protected]", "dons
> > > > > > service"));
> > > > > > msg.addRecipient(Message.RecipientType.TO,
> > new
> > > > > > InternetAddress
> > > > > > (recipient));
> > > > > > msg.setSubject("dons service daily charts");
>
> > > > > > Multipart multipart = new MimeMultipart();
>
> > > > > > MimeBodyPart htmlPart = new MimeBodyPart();
> > > > > > htmlPart.setContent(" url2: " ,
> > "text/html");
> > > > > > multipart.addBodyPart(htmlPart);
>
> > > > > > // append png image?
> > > > > > ByteArrayDataSource mimePartDataSource = new
> > > > > > ByteArrayDataSource
> > > > > > (imagestream, "image/png");
>
> > > > > > MimeBodyPart attachment = new
> > MimeBodyPart();
> > > > > > attachment.setDataHandler(new
> > > > > > DataHandler(mimePartDataSource));
> > > > > > attachment.setFileName("ticker.png");
> > > > > > multipart.addBodyPart(attachment);
> > > > > > msg.setContent(multipart);
>
> > > > > > Transport.send(msg);
> > > > > > }
>
> > > > > > On Nov 17, 5:35 am, "Ikai L (Google)" <[email protected]> wrote:
> > > > > > > I couldn't reproduce your exact error, but I was able to put
> > together
> > > > a
> > > > > > > working example of an inbound email handler to relay messages.
> > I'm
> > > > going
> > > > > > to
> > > > > > > expand the documentation about processing inbound emails. Here's
> > some
> > > > > > > working code:http://pastie.org/701517
>
> > > > > > > Does this example help any? Code is also pasted below, but it'll
> > be
> > > > > > easier
> > > > > > > for you to look at the Pastie.
>
> > > > > > > import javax.servlet.http.HttpServlet;
> > > > > > > import javax.servlet.http.HttpServletRequest;
> > > > > > > import javax.servlet.http.HttpServletResponse;
> > > > > > > import javax.servlet.ServletException;
> > > > > > > import javax.mail.*;
> > > > > > > import javax.mail.util.ByteArrayDataSource;
> > > > > > > import javax.mail.internet.MimeMessage;
> > > > > > > import javax.mail.internet.MimeMultipart;
> > > > > > > import javax.mail.internet.MimeBodyPart;
> > > > > > > import javax.mail.internet.InternetAddress;
> > > > > > > import javax.activation.DataHandler;
> > > > > > > import java.io.IOException;
> > > > > > > import java.io.InputStream;
> > > > > > > import java.util.logging.Logger;
> > > > > > > import java.util.Properties;
>
> > > > > > > public class MailHandlerServlet extends HttpServlet {
> > > > > > > private static final Logger log =
> > > > > > > Logger.getLogger(MailHandlerServlet.class.getName());
> > > > > > > private static final String RECIPIENT = "[email protected]
> > ";
> > > > > > > private static final String SENDER = "[email protected]";
>
> > > > > > > protected void doPost(HttpServletRequest request,
> > > > HttpServletResponse
> > > > > > > response) throws ServletException, IOException {
> > > > > > > Properties props = new Properties();
> > > > > > > Session session = Session.getDefaultInstance(props,
> > null);
> > > > > > > try {
> > > > > > > MimeMessage message = new MimeMessage(session,
> > > > > > > request.getInputStream());
>
> > > > > > > Object content = message.getContent(); // You could
> > also
> > > > > > > probably just use message.getInputStream() here
> > > > > > > // and avoid
> > the
> > > > > > > conditional type check
>
> > > > > > > if (content instanceof String) {
> > > > > > > log.info("Received a string");
> > > > > > > } else if (content instanceof InputStream) {
> > > > > > > // My somewhat limited testing indicates that
> > this is
> > > > > > always
> > > > > > > getting returned as an
> > > > > > > // InputStream
>
> > > > > > > InputStream inputStream = (InputStream) content;
> > > > > > > ByteArrayDataSource inboundDataSource = new
> > > > > > > ByteArrayDataSource(inputStream,
>
> ...
>
> read more »
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.