1

i need to implement the email signature with image.As of now we only support the text in email signature which is already working.i need to provide the functionality where i can insert the image inside mail signature. i can send the email to user within myapplication and also to user on external mail domain like gmail,yahoo etc. When mail is sent to some user with in my application system, system makes entryt o DB and when receiver receives in inbox (which internally read the mail from db). Now if user send the mail to external user on gmail it makes use of javax mail api . Similary i can receive the email from external mail domains(gmail,yahoo etc) Now i have few questions based on tis requirement:-

1)Is there any standard for how the external mail domains like gmail send the image inside signature to another domains like (my application mail domain)? Another point related to it gmail user can have two images ,one for signature and another image inside body. How will i determine which image belongs to signature? Is there any defined property for that?

2)Also not able to make out what is the best/consistent approach to send(whether to internal application user or external mail domain user ) the email signature containing image so that it renders correctly when user receives it?

what I had in my mind for point 2:- i earlier thought i can use solution suggested at How to display an image in jsp?. where with tag <.img src="/getImage.action?imageId=123">, i can fetch the image from db in action class or servlet and return. But keeping in mind once i send the mail to the user on gmail , he will not be able to access the servlet.So this approach does not seems to fit in requirement. Then i came across the another great stackoverflow link base64 encoded images in email signatures where solution by Tim Medora looked great but again the comment below the solution Gmail doesn't seem to support it again ended my Folks really i think i should be done if mail domain like gmail,yahoo support the solution suggested by because in that case i can send image as base64 string instead of image as attachment.

Folks would be really grateful if you can provide me some pointer/approach regarding both points 1 and 2

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • On a side first, although meant for users of Opera's built-in mail client, see and for what a signature actually is and how it affects replies in messages. – Shadow2531 Jun 21 '12 at 01:54

2 Answers2

6

To include images in the email message, first you have to include the images as MIME attachments in the email. Each of these attachments must have a "Content-ID" header.

--f46d0444ea0d6991ba04b91c92e6
Content-Type: image/gif; name="theImage.gif"
Content-Transfer-Encoding: base64
Content-ID: <theImage@abcd>

[base64 string]
--f46d0444ea0d6991ba04b91c92e6--

2) Then, in the email message, include the Content-ID in the src attribute of the <img> tag.

<img src="cid:theImage@abcd" />
Michael
  • 34,873
  • 17
  • 75
  • 109
  • Hi Michael Thanks for answering it. Are you saying i have to do something like this http://www.rgagnon.com/javadetails/java-0504.html in my java class when sending the message. If yes, i do not see in the example setting properties base64 string,content-id,content-type etc. Could you point me to some code snippet for doing the stuff you explained in point 1 in java . Sorry i am new to this mail stuff. Another point can't i sent send it simply as base64 string like this – M Sach Jun 20 '12 at 17:56
  • instead of sending attachment – M Sach Jun 20 '12 at 17:57
  • Also it would be helpful to provide some inputs on point 1 asked in my post. Thanks in advance – M Sach Jun 20 '12 at 17:58
  • 1
    Including base64-encoded image data inside of an image URL is called a **data URI**. If this doesn't work, then you'll have to add the images as attachments instead. The `SimpleMail2` code sample in the website you referenced (third one down) shows how to do this. The Java Mail API automatically base64-encodes the images and adds the appropriate mail headers for you, which is why you don't see these mentioned in the code sample. – Michael Jun 20 '12 at 18:03
  • Thanks Michael.It brings bit clarity. Can you throw some light on point 1 asked in my post. I am just repeating it here again Is there any standard for how the external mail domains like gmail send the image inside signature to another domains like (my application mail domain)? Another point related to it gmail user can have two images ,one for signature and another image inside body. How will i determine which image belongs to signature? Is there any defined property for that? – M Sach Jun 20 '12 at 18:17
  • @MSach I don't understand what you mean by "signature". Do you mean the signature that goes at the end of every message, like "Regards, Michael"? If so, any images in the signature can just be treated as a normal image. – Michael Jun 20 '12 at 18:26
  • Thanks Michael i will. I will do some POC and let you know the findings.Thanks for reply – M Sach Jun 20 '12 at 18:28
  • @MSach In regards to the mention of data URIs, although that's a solution, not a lot of clients support data URIs in HTML messages. But, Michael's suggestion to use an embedded image with a content-id header and then reference that with a cid URI is the correct way to do it. If Gmail doesn't support that way, that's just Gmail's fault. Not sure what Gmail does specifically. You could just send an HTML message with a signature in Gmail and then view the source of the sent message to see that it does. Perhaps it uses a certain class attribute value for its signatures in the HTML. – Shadow2531 Jun 21 '12 at 02:00
0

For Gmail to see the embedded image from byte array, I posted an answer on another similar question which is to use ByteArrayDataSource and embed it to the HtmlEmail. Here's the code snippet:

import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.mail.ImageHtmlEmail;
...
ImageHtmlEmail email = new ImageHtmlEmail();
byte[] qrImageBytes = createQRCode(); // get your image byte array
ByteArrayDataSource qrImageDataSource = new ByteArrayDataSource(qrImageBytes, "image/png");
String contentId = email.embed(qrImageDataSource, "QR Image");
Community
  • 1
  • 1
JerryP
  • 517
  • 6
  • 10