Embed image in mail body using C# and Asp.Net

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:

  1. 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.
  2. 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();

    }

Upvotes: 1

Views: 4218

Answers (2)

Andr&#233; Boonzaaijer
Andr&#233; Boonzaaijer

Reputation: 846

For reference, a full working example:

    public void SendMail()
    {
        LinkedResource logo = new LinkedResource(
            "images\\image005.png",                 //Path of file
            "image/png");                           //Mime type: Important!
        logo.ContentId = "logo";                    //ID for reference

        //Actual HTML content of the body in an 'AlternateView' object.
        AlternateView vw = AlternateView.CreateAlternateViewFromString(
            "Hello, this is <b>HTML</b> mail with embedded image: <img src=\"cid:logo\" />", 
            null, 
            MediaTypeNames.Text.Html);              //Mime type: again important!
        vw.LinkedResources.Add(logo);

        var msg = new MailMessage() { IsBodyHtml = true };
        msg.AlternateViews.Add(vw);
        msg.From = new MailAddress("[email protected]");
        msg.To.Add(new MailAddress("[email protected]"));
        msg.Subject = "HTML Mail!";
        using (var client = new SmtpClient("localhost", 25)) 
            client.Send(msg);
    }

Upvotes: 0

Simon
Simon

Reputation: 151

  1. Convert your image to base64. Either with a website/tool like https://www.base64-image.de/ or programmatically http://www.c-sharpcorner.com/blogs/convert-an-image-to-base64-string-and-base64-string-to-image

  2. Embed it: <img src="data:image/jpeg;base64,IMAGEDATA" />

    Edit the data type to match the type of your image and replace "IMAGEDATA" with the image bytes converted to a base 64 string.

Upvotes: 2

Related Questions