I'm starting with SendGrid to send my e-mails, but I coudn't find how to embed images. Without using SendGrid I was using the following code to send e-mails with embedded images:
var mail = new System.Net.Mail.MailMessage();
mail.Subject = "Warning";
mail.From = "[email protected]";
mail.To.Add("[email protected]");
mail.IsBodyHtml = true;
mail.Body = "<html><body><a href='http://www.mywebsite.com' title='My Website'><img src='cid:my_image' alt='My Image' border='0' /></a><br /><h1>My E-mail Title</h1>E-mail content.</body></html>";
var av = AlternateView.CreateAlternateViewFromString(mail.Body, null, "text/html");
av.LinkedResources.Add(new LinkedResource(@"C:\my_image.png", "image/png"){ContentId="my_image"});
mail.AlternateViews.Add(av);
var smtp = new SmtpClient("smtp.test.com");
smtp.Credentials = new NetworkCredential("user", "pass");
smtp.Send(mail);
--- EDITED ---
Now I'm using the following code to send my e-mails (using SendGrid classes).
var message = SendGrid.GetInstance();
message.Subject = "Warning";
message.From = new MailAddress("[email protected]");
message.To = new MailAddress[] { new MailAddress("[email protected]") };
message.Html = "<html><body><a href='http://www.mywebsite.com' title='My Website'><img src='cid:my_image' alt='My Image' border='0' /></a><br /><h1>My E-mail Title</h1>E-mail content.</body></html>";
var transportSMTP = SMTP.GetInstance(new NetworkCredential("user", "pass"));
transportSMTP.Deliver(message);
What I need to know is how to embed and link my images inside the e-mail using its content id (CID).
I juggled around this for a little bit too. The solution was to Attach the image first and then to Embed afterwards with the direct name of the file in the email.
var myMessage = new SendGridMessage();
myMessage.AddTo("[email protected]");
myMessage.From = "[email protected]";
myMessage.Subject ="Forgotten Password";
string body = @"<div><img src='cid:email_reset' alt='Email Reset Header' border='0' /></div>" +
@"<p>Content...</p>";
string att = Request.PhysicalApplicationPath + @"Content\files\images\email_reset.jpg";
var attachment = new Attachment(att, new ContentType("image/jpeg"));
var linkedResource = new LinkedResource(att, new ContentType("image/jpeg"));
myMessage.AddAttachment(attachment.ContentStream, attachment.Name);
myMessage.EmbedImage(attachment.Name, "email_reset");
myMessage.Text = WebUtility.HtmlDecode(Regex.Replace(body, "<[^>]*(>|$)", string.Empty));
myMessage.Html = body;
// Authenticate and Send the email
For anyone who is struggling with sending embedded images in the emails via SendGrid and using the stored templates, here's how it works:
/* In my case the images reside in the SQL DB as BLOBs but for this example it does not matter.
All you need is an array of bytes with your image.
*/
var imageContent = File.ReadAllBytes("Images\\logo_eml.jpg");
using (MemoryStream s = new MemoryStream(imageContent))
{
ContentType ctype = new ContentType("image/jpg");
var attachment = new System.Net.Mail.Attachment(s, ctype);
var linkedResource = new LinkedResource(s, ctype);
attachment.Name = "cibc_ins_logo_eml.jpg";
emailMsg.AddAttachment(attachment.ContentStream, attachment.Name);
emailMsg.EmbedImage(attachment.Name, linkedResource.ContentId + ".jpg");
values.Add("%embedLogo%", linkedResource.ContentId + ".jpg");
}
string templateJSON = "{\"to\": [\"%to%\"],\r \"sub\": {\":name\": [\"%name%\"],\":embedLogo\": [\"%embedLogo%\"]},\r \"category\": [\"Quotes\"],\r \"filters\": {\"templates\": {\"settings\": {\"enable\": 1,\"template_id\": \"%templateid%\"}}}}"
emailMsg.Headers.Add("x-smtpapi", SubstituteTemplate(templateJSON, values));
The last line will replace the values. In my case it simply does a regex replace. This code will have to take care of substituting %embedLogo%
in your particular JSON with the value of linkedResource.ContentId+".jpg"
stored previously in the values
which is a Dictionary<string,string>
.
Here's what you need in the stored HTML template:
<img width="227" height="86" src="cid::embedLogo">
CID and two colons are critical.
Then Gmail will show the images embedded and not just attached!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With