I am working with a file for Unit testing and a Resourcefile ("WorkflowNotifications.resx") which contains strings with HTML code and one png image ("mail_header.png"). In the HTML code is an image which should be the one from the resourcefile. My goal is to refer this image to the sourcelink of the img attribute. And in doing so I should use the Attachmentclass. I do not know what to do with my initialized Attachmentobject and how to add it to the sourcelink. I know there is a similar post on this topic, but mine is a bit more specific because I should not refer to image directly in the HTML code. Thank you for your time and help!
html img code:
<img src="cid:"{0}" >
I use string.Format to replace the placeholder in the unit testing method so far, but the image will not display.
Help method for getting a stream:
public static Stream GetMailHeader()
{
return ToStream((Image)MailTemplates.WorkflowNotifications.ResourceManager.GetObject("mail_header"), ImageFormat.Png);
}
private static Stream ToStream(Image image, ImageFormat format)
{
var stream = new System.IO.MemoryStream();
image.Save(stream, format);
stream.Position = 0;
return stream;
}
Attachment object in the unittesting method:
var att = new Attachment(Resources.ResourceGovernor.GetMailHeader(), "mail_header");
ResourceGoverner is just a class that is handling most of the resources and WorkflowNotifications is the resourcefile.
I found a solution by myself. For everyone who has similar issues: Just add the attachment to your MailMessage by writing:
mailMessage.Attachments.Add(att);
and change the html code to this:
<img src="mail_header.dat">