Place local image on body when sending email

Asked

Viewed 3,627 times

2

To send an email from the application I am using the MailMessage available. I am now trying to place an image in the email body via html:

MailMessage mail = new MailMessage();
SmtpClient SmtpCliente = new SmtpClient(server);
...
mail.IsBodyHtml = true;
mail.Body += "<br />Cumprimentos,<br />";
mail.Body += "<img src=\"D://MediaOleotorres/logoOleotorresAssinatura.png\" height=\"42\" width=\"42\">";

However when sending the email, the image does not appear. I have to use an image that is available online?

EDIT: I have also tried to put the image as attached and then add it to the body, it is possible?

string attachmentPath = @"D:/MediaOleotorres/imagens/logoOleotorresAssinatura.png";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);

mail.Attachments.Add(inline);
  • You can accept your own answer in this case.

1 answer

1


I have already found a solution, which involves creating a new local image shortcut that you want to add to the body of the message and add to the body:

            var contentID = "Image";
            var inlineLogo = new Attachment(@"D://MediaOleotorres/logoOleotorresAssinatura.png/logoOleotorresAssinatura.png");
            inlineLogo.ContentId = contentID;
            inlineLogo.ContentDisposition.Inline = true;
            inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
            mail.Attachments.Add(inlineLogo);
            mail.Body += "<br /><br /><img src=\"cid:" + contentID + "\" height=\"42\" width=\"42\"><br />";
  • I still think the right thing to do would be for that image to be hosted online

  • Yes, I think so too. But it’s one of those cases where the client "knows"

  • I sure understand you

  • I know, I just didn’t come to the OR this weekend to do it :P

Browser other questions tagged

You are not signed in. Login or sign up in order to post.