09 May, 2014

How to embed an image inline in an email?

Quite a new thing (to me at least), but how do you embed an image in an email programmatically? Not just attach the picture but make it appear inline as an embedded part of the html email?

Turns out that’s fairly easy (when you know how?):

MailMessage msg = new MailMessage();
foreach (var recpient in lst)
msg.To.Add(recpient);

msg.Subject = subject;
msg.IsBodyHtml =
true;
msg.Priority =
MailPriority.Normal;
msg.Body = htmlBody;

Attachment inlineLogo = new Attachment("logo.png");
msg.Attachments.Add(inlineLogo);

inlineLogo.ContentId =
"logo";
inlineLogo.ContentDisposition.Inline =
true;
inlineLogo.ContentDisposition.DispositionType =
DispositionTypeNames.Inline;

Logger.Write("Sending email to: {0}", rcpts);
new SmtpClient().Send(msg);
The accompanying html that works with this is (the important part is src=”cid:logo”)
    <p class="italic">
<
b>/Reporting System</b><br />
This email is sent out automated. You can not respond to this email.<br /><br />
<
img width="60" height="60" src="cid:logo" />
</
p>
</
body>

Technorati Tags:

No comments:

InRiver: Not loading your extensions?

(You really need to in the loop to appreciate the issue this post addresses). Man, I've been fighting this problem for hours before I ...