Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can send the image by email saved in my web application Images folder , but my question is i have image in database as string format how can i send it by email?


For Image in folder sending i am using below code:

LinkedResource logo = new LinkedResource(System.AppDomain.CurrentDomain.BaseDirectory + @"Images/logo.jpg");
logo.ContentId = "logo";
AlternateView av1 = AlternateView.CreateAlternateViewFromString("" + htmlTemplate, null, MediaTypeNames.Text.Html);

av1.LinkedResources.Add(logo);
message.AlternateViews.Add(av1);


How to Send if i have image in string format?

What I have tried:

i have tried to replace the <img src=[abc]> like,
htmlTemplate=htmlTemplate.Replace("[abc]", @"data:image/gif;base64," + software.SoftwareImage);  // does not worked

also tried
LinkedResource(@"data:image/gif;base64," + software.SoftwareImag);

//does not worked
Posted
Updated 10-Oct-17 4:33am
v2
Comments
Jochen Arndt 10-Oct-17 6:17am    
It looks like your image is already base64 encoded. Then just get it as string from the database into a local variable.

Then you have to create a multipart mail where you can add the image as one of the parts specifying base64 encoding and a content ID. This content ID must be used as <img src=cid:[the_id]>.

1 solution

Assuming your image is a Gif stored as a Base64 string:
C#
byte[] imageBytes = Convert.FromBase64String(software.SoftwareImag);
Stream contentStream = new MemoryStream(imageBytes);
LinkedResource image = new LinkedResource(contentStream, MediaTypeNames.Image.Gif);
image.ContentId = "software";

AlternateView av1 = AlternateView.CreateAlternateViewFromString(htmlTemplate, null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(image);
message.AlternateViews.Add(av1);

NB: Storing the image in the database as a Base64-encoded string wastes a lot of space. You should either be storing it using a binary type, or storing the image in the file system, and just storing the path in the database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900