Send Automated Email through Windows Service that has an embedded image using C#
- by Refracted Paladin
I already have a C# windows service that we use internally to monitor a directory on our network and when it detects a change sends off an email using our internal SMTP server to the specified groups of people.
Now I need to embedd an image in that automated email. I understand that I need to create an AlternateView and a Linked Resource and use the Linked Resource's cID in the AlternateView, correct.
What I do not understand is where do I put the image? Should I add it to my service project and set Copy to Output Directory = Copy Always? If so, how would I then access when creating my LinkedResource? Also, where do I put the image on the Server hosting the Service?
Here is what I have so far but it doesn't seem to work. I don't get any errors, that I am aware of, but I do not get an email either. I am guessing it is looking for the image but that I do not have it in the correct location.
// This event is called when an object(file,folder) is created in the srcPath
void WatcherCreated(object source , FileSystemEventArgs e)
{
var folderName = e.Name;
var folderPath = e.FullPath;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "New Enrollment for " + folderName;
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is the plain text view", null,
"text/html");
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString("Here is an embedded image. <img src=cid:enrollProcessID>",
null, "text/html");
LinkedResource imageResourceLink = new LinkedResource("EnrollmentProcess.jpg")
{ContentId = "enrollProcessID"};
htmlView.LinkedResources.Add(imageResourceLink);
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);
var smtp = new SmtpClient("internalSMTP");
smtp.Send(mail);
}