Problem embedding base64 encoded Image (png) into Html body

0 votes
asked Dec 16, 2013 by dirk (530 points)
edited Dec 17, 2013

Hello,

I tried to implement the example you described in your tutorials (but with png instead of jpeg):

MailMessage message = new MailMessage();

LinkedResource picture = new LinkedResource(@"c:\temp\image.png", "image/png");
picture.ContentId = "0123456789@rebex.net"; // a unique ID

AlternateView htmlView = new AlternateView();
htmlView.SetContent(
"<html><body><img src='cid:0123456789@rebex.net'></body></html>",
MediaTypeNames.Text.Html);

message.AlternateViews.Add(htmlView);
message.Resources.Add(picture);

I don't get an exception, but I only see a cross sign in Outlook 2010 or in a browser(firefox) when I open the mail (and the image cannot be saved) . Im my visual studio the image is displayed as Resource00000.png or similar and I only see an Array A with bytes. What could be the issue? It works neither with jpeg nor with png.

Any help would be kindly appreciated.

dirk

Applies to: Rebex Secure Mail

3 Answers

+1 vote
answered Dec 17, 2013 by dirk (530 points)
edited Dec 17, 2013
 
Best answer

Good morning,

I have good news: it finally works :) After trying as you mentioned to save the message I found out, that I saw the image. After analyzing and comparing the saved mail's content with the sent one, the problem was obvious: The base64 encoded image is not included in the sent message. So, looking into the code before the message gets sent out the problem/mistake could be found: Before the message was actually sent out its values were transformed partially into another message object. That way the Resources with the image's content were forgot to be saved into the new object. I changed the code to just work with the first message object and pipe it into the next functions.

p.s.: My build version is #4981.

Thanks for the help, dirk

commented Dec 17, 2013 by Tomas Knopp (58,890 points)
edited Dec 17, 2013

Good morning Dirk, I am glad it works for you now! Thank you for your cooperation as well.

+1 vote
answered Dec 16, 2013 by Tomas Knopp (58,890 points)
edited Dec 16, 2013

Hello Dirk,

the above code works for me both with jpg and png images. But from the title of your question I guess that you are not loading an ordinary png or jpg image, but instead a base64 encoded one.

Why are you loading a base64 encoded image? I do not think you will be able to view such an encoded image for instance with MS Paint either.

If you will just load a normal jpg or png with the code above and then do

message.Save(@"C:\temp\mail.eml");

You will see that the linked resource will be base64 encoded automatically, so you do not have to do it on your own beforehand.

If this is not the case and you really have the images in this format, let me know.

0 votes
answered Dec 16, 2013 by dirk (530 points)
edited Dec 17, 2013

Hello Tomáš,

thanks for your reply. Oh, you do? Hm, I just took a normal image file, that I could open in Paint. My goal was to encode the mail in base64 with the image included in the html body and not as a normal attachment. Do you maybe have an example project so I could compare with mine? Or any other ideas?

Thanks, dirk

commented Dec 16, 2013 by Tomas Knopp (58,890 points)
edited Dec 16, 2013

Could you please let me know which version of Rebex components you are using? Please try the latest trial verion and let me know if the code above still does not work for you.

commented Dec 16, 2013 by dirk (530 points)
edited Dec 16, 2013

Thanks for the link. How can I find out which version I am using? I have to check that out then. Unfortunately I will only be able to try tomorrow morning. I will let you know asap then.

commented Dec 16, 2013 by Tomas Knopp (58,890 points)
edited Dec 16, 2013

To check the build number:

in your VS project, right click on one of the referenced Rebex dll (e.g. Rebex.Mail but any will work) and click on Properties. Under 'Version' you will see something like 2.0.5085.0 (the build number is the 3rd number. In this case 5085, i.e. version 2013R3).

The support for images being automagically linked from HTML was added in version 2012R2 (i.e. build number 4546). If you get a smaller build number, please try the latest trial version and let me know if it works for you or not. Thank you!

...