I was trying to embed an image into an Outlook email. Basically, I want to create a windows service everyday for Birthday wishes. Service will send birthday wishes to all respective employees. For each day, the image template will be different, and there will be a background color each day.
I was trying to use the code snippet in this link, but facing with two issues:
- When I add image to the HTML Body, mail comes to outlook with red cross ('X') mark. I have verified the path, there was no issue on that.
- When I add anything additional to HTML Body, the image will be replaced with that one. Not sure where am I going wrong.
I am attaching the C# code which I tried till now:
private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
{
string path = Server.MapPath("~/Images/TestLogo.png");
Configuration config = System.Web.Configuration.WebConfigurationManager
.OpenWebConfiguration("~/");
var settings = (System.Net.Configuration.MailSettingsSectionGroup)
config.GetSectionGroup("system.net/mailSettings");
var smtp = settings.Smtp;
System.Net.Configuration.SmtpNetworkElement network = smtp.Network;
var outlookApp = new Microsoft.Office.Interop.Outlook.Application();
var mailitem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
mailitem.To = network.TargetName;
mailitem.Subject = subject;
Microsoft.Office.Interop.Outlook.Attachment attachment = mailitem.Attachments.Add(path, OlAttachmentType.olEmbeddeditem, null, "Test Image");
string imageCid = "TestLogo.png@123";
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
mailitem.BodyFormat = OlBodyFormat.olFormatRichText;
mailitem.HTMLBody = String.Format("<body bgcolor='#E6E6FA'>Dear TestMan,<img src=@cid:{0}\"></body>", imageCid);
//mailitem.Body = body;
mailitem.Display(false);
mailitem.Send();
}