Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, February 9, 2014 4:25 AM
Hi, this is a web api project. to send email.
I am able to send html by making use of the information I found here , to read the razor view, and asign it to the body of
the message, however, in my razor view, I have an image tag, the image referenced by this tag does not show when the email is received on gmail. (the text content show properly, only the image is not shown), any ideas? thanks
my code is here:
public HttpResponseMessage Post()
{
string path =HttpContext.Current.Server.MapPath(@"~/Views/Email/e.cshtml");
var template = File.ReadAllText(path);
dynamic s=new {Name="hello"};
var temp_ = Razor.Parse(template,s);
MailMessage emailMessage = new MailMessage();
string host = "smtp.gmail.com";
int port = 587;
string sender = "[email protected]";
emailMessage.From = new MailAddress("[email protected]");
emailMessage.To.Add(new MailAddress("[email protected]"));
emailMessage.IsBodyHtml = true;
// emailMessage.Subject = "
emailMessage.Body = temp_;
SmtpClient smtp = new SmtpClient(host, port);
smtp.UseDefaultCredentials = false;
NetworkCredential nc = new NetworkCredential(sender, "pwd");
smtp.Credentials = nc;
smtp.EnableSsl = true;
emailMessage.IsBodyHtml = true;
smtp.Send(emailMessage);
}
and the view is here:
@model dynamic
<h4>Hello @Model.Name</h4>
<img src="/Content/email/images/header.jpg" />
All replies (4)
Sunday, February 9, 2014 2:55 PM âś…Answered
Thanks everyone for your contribution, I got it working by passing the image path to the view this way:
string logoImage = HttpContext.Current.Server.MapPath(@"~/Content/email/images/logo.jpg");
Attachment logoImageAtt = new Attachment(logoImage);
emailMessage.Attachments.Add(logoImageAtt);
logoImageAtt.ContentDisposition.Inline = true;
logoImageAtt.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
var template = File.ReadAllText(path);
emailMessage.From = new MailAddress("..");
emailMessage.To.Add(new MailAddress("..."));
emailMessage.IsBodyHtml = true;
string logoImgId = "headerimg1";
logoImageAtt.ContentId = logoImgId;
//this model is passed to the view (email)
dynamic model = new {
logoSrc = logoImgId
};
var temp_ = Razor.Parse(template, model);
emailMessage.Body += temp_;
AND THIS IS THE VIEW
@model dynamic
<img src="cid:@Model.logoSrc" />
<b>Dear @Model.fname @Model.lname,</b>
Sunday, February 9, 2014 9:55 AM
Have you considered using an absolute URL for your image?
Sunday, February 9, 2014 1:33 PM
<img src="~/Content/email/images/header.jpg" />
Try the code above for your url
greetings Damien
Sunday, February 9, 2014 2:29 PM
you can not use relative urls in html mail messages. you must include the protocol and domain (generally these will be blocked and require show images click). Your other option is to imbed the image as an attachment and reference it via cid: protocol. also many email clients (but not outlook) support dataurls for images.