How to use razor engine for email templating with image src

I’ve found this link on how to using Razor Engine for email templates in asp.net and it worked great. But I’ve tried to have a logo in the email template with an image.

Something like this:

EmailTemplate.cshtml (this by the way is a strongly-type view)

<html>
<body>
  <img src="logo.jpg" />
</body>
</html>

and when I try to submit it on email, it seems that the image path was not read, it only rendered an X in the content.

I’m thinking to pass the image path as part of the Model but it seems odd that way. Is there any way to achieve this?

Any help would be much appreciated. Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To see image everywhere you can use these options:

Absolute Url

You can simply use full absolute path of image for example "http://example.com/images/logo.png"

IMO It is the most simple option and recommended for your problem.

Attachment

As mentioned by Mason in comments You can attach image to mail and then put image tag and useContentId of attachment:

//(Thanks to Mason for comment and Thanks to  Bartosz Kosarzyck for sample code)
string subject = "Subject";
string body = @"<img src=""$CONTENTID$""/> <br/> Some Content";

MailMessage mail = new MailMessage();
mail.From = new MailAddress("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b0d1904062b0e130a061b070e45080406">[email protected]</a>");
mail.To.Add(new MailAddress("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e7fcd3f6ebf2fee3fff6bdf0fcfe">[email protected]</a>"));
mail.Subject = subject;
mail.Body = body;
mail.Priority = MailPriority.Normal;

string contentID = Guid.NewGuid().ToString().Replace("-", "");
body = body.Replace("$CONTENTID$", "cid:" + contentID);

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//path of image or stream
LinkedResource imagelink = new LinkedResource(@"C:UsersR.AghaeiDesktopoutlook.png", "image/png");
imagelink.ContentId = contentID;
imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink);
mail.AlternateViews.Add(htmlView);

SmtpClient client = new SmtpClient();
client.Host = "mail.example.com";
client.Credentials = new NetworkCredential("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e283c21230e2b362f233e222b602d2123">[email protected]</a>", "password");
client.Send(mail);

Data Uri

you can use data uri (data:image/png;base64,….).

Not Recommended because of weak support in most of mail clients, I tested it with Outlook.com(web) and OutlookWebAccess(web) and Office Outlook(Windows) and Outlook(windows 8.1) and unfortunately it worked only on OutlookWebAccess(web).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x