Monday 15 April 2013

servlets - JavaMail: inline images generated incorrectly -



servlets - JavaMail: inline images generated incorrectly -

i coded servlet below generates response email in .eml format (using javamail). browser detects mime type , opens email app (microsoft live in case) when response received. "x-unsent" set "1" when email opened can edited , sent.

the email content html image inline. when open generated email can see content no issues. when come in address , seek send email message "one or more images not found, want continue?". send anyways , when check business relationship of email recipient email received instead of having image inline, attached. seems wrong way inline images generated. ideas?

protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("message/rfc822"); response.setheader("content-disposition", "attachment; filename=\"email.eml\""); printwriter out = response.getwriter(); string eml = null; seek { message message = new mimemessage(session.getinstance(system.getproperties())); message.addheader("x-unsent", "1"); message.setsubject("email inline image"); // mail service has 2 parts, body , embedded image mimemultipart multipart = new mimemultipart("related"); // first part (the html) bodypart messagebodypart = new mimebodypart(); string htmltext = "<h1>this image: </h1><img src=\"cid:image\">"; messagebodypart.setcontent(htmltext, "text/html"); multipart.addbodypart(messagebodypart); // sec part (the image) messagebodypart = new mimebodypart(); string filepath = "c:/image.png"; datasource fds = new filedatasource(filepath); messagebodypart.setdatahandler(new datahandler(fds)); messagebodypart.setheader("content-type", "image/png"); messagebodypart.setheader("content-id", "image"); messagebodypart.setdisposition( mimebodypart.inline ); // add together image multipart multipart.addbodypart(messagebodypart); // set message.setcontent(multipart); bytearrayoutputstream os = new bytearrayoutputstream(); message.writeto(os); eml = new string(os.tobytearray(),"utf-8"); } grab (messagingexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } out.print(eml); out.flush(); out.close(); }

try simpler version might prepare problem:

// first part (the html) mimebodypart messagebodypart = new mimebodypart(); string htmltext = "<h1>this image: </h1><img src=\"cid:image\">"; messagebodypart.settext(htmltext, null, "html"); multipart.addbodypart(messagebodypart); // sec part (the image) messagebodypart = new mimebodypart(); string filepath = "c:/image.png"; messagebodypart.attachfile(filepath, "image/png", "base64"); messagebodypart.setcontentid("<image>"); // add together image multipart multipart.addbodypart(messagebodypart);

servlets javamail

No comments:

Post a Comment