Search code examples
c#asp.netsmtp

C# embedding image in the body of the outlook email


I am trying to embed my company logo in the body of the email, I do receive the email, but I don't see the logo in the email. Instead of the logo, I see an image box that says "The linked image cannot be displayed". Below is my code:

     string strMailContent = "You can include your text here";
            string path = Server.MapPath(@"~/Images/logo.png");
            LinkedResource logo = new LinkedResource(path);
            logo.ContentId = "companylogo";
            AlternateView av1 = AlternateView.CreateAlternateViewFromString(
                      "<html><body><img src=cid:companylogo/>" +
                      "<br></body></html>" + strMailContent,
                 null, MediaTypeNames.Text.Html);

 try
            {
                SmtpClient client = new SmtpClient();

                client.Credentials = new NetworkCredential("test1", "pass");
                client.Port = 100;
                client.EnableSsl = true;
                client.Host = "smtp.office365.com";
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                av1.LinkedResources.Add(logo);
                MailMessage mail = new MailMessage();
                mail.AlternateViews.Add(av1);
                mail.IsBodyHtml = true;
                mail.From = new MailAddress("test", " Confirmation " );

                mail.To.Add(new MailAddress("n@n.com"));
                mail.Subject = "testSubject];

                mail.Body = sb.ToString();

                client.Send(mail);

I checked the path and path seems to be correct. I am not sure what am I doing wrong. This is what I see in Outlook email:

enter image description here

there is also an attachment in the email that has an extension "bin". I am not sure why that attachment is coming.

I already looked at several resources like this:

Send inline image in email

Image in body of the Mail C#

Any help will be highly appreciated.


Solution

  • Outlook doesn’t actually allow embedding like that. You can add your company logo as a signature though. Just add the image normally. You don’t need any code.