Reputation: 1823
I am having issues getting a PNG to render as an embedded image in an email. All I am trying to do is embed a company logo in the bottom of an email.
The image is a resource in my project.
Here is code I am using to generate the email body text and alternate views:
string body =
@"<html><body><b>Please do not respond to this message</b><br/><br/>" +
"<img src='cid:companyLogo' width='100px'/></body></html>";
Bitmap logo = new Bitmap(Properties.Resources.logo_dark_background);
MemoryStream logoStream = new MemoryStream();
logo.Save(logoStream, ImageFormat.Jpeg);
LinkedResource companyLogo = new LinkedResource(logoStream);
companyLogo.ContentId = "companyLogo";
companyLogo.ContentType = new ContentType("image/jpg");
AlternateView av = AlternateView.CreateAlternateViewFromString(
body, null, MediaTypeNames.Text.Html);
av.LinkedResources.Add(companyLogo);
message.AlternateViews.Add(av);
Please note that 'message' is a MailMessage
object created earlier on.
Upvotes: 0
Views: 1061
Reputation: 148
The problem is that your stream's position is at the end of the stream after saving the logo.
Immediately after the logo.Save()
call, you can set logoStream.Position = 0
to reset it to the beginning.
Also don't forget to dispose of your stream, most likely in a using
statement.
Upvotes: 2