我从SendGrid开始发送我的电子邮件,但我找不到如何嵌入图片。在不使用SendGrid的情况下,我使用以下代码发送带有嵌入式图像的电子邮件:
var mail = new System.Net.Mail.MailMessage();
mail.Subject = "Warning";
mail.From = "from_user@test.com";
mail.To.Add("to_user@test.com");
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 ---
现在我使用以下代码发送我的电子邮件(使用SendGrid类)。
var message = SendGrid.GetInstance();
message.Subject = "Warning";
message.From = new MailAddress("from_user@test.com");
message.To = new MailAddress[] { new MailAddress("to_user@test.com") };
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);
我需要知道的是如何使用其内容ID(CID)在电子邮件中嵌入和链接我的图像。
答案 0 :(得分:3)
我也在这附近玩弄了一下。解决方案是首先附加图像,然后使用电子邮件中文件的直接名称嵌入到Embed中。
var myMessage = new SendGridMessage();
myMessage.AddTo("to@email.com");
myMessage.From = "from@email.com";
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
答案 1 :(得分:2)
对于任何想要简单一些的人,您可以在Base64中对图像文件进行编码,然后将其作为字符串直接嵌入到电子邮件的html主体中,它对我有用,因为我只想在电子邮件中包含一个小徽标签名:
<img alt="My Image" src="data:image/jpeg;base64,/9j/4S/+RXhpZgAATU0AKgAAAAgACAESAAMAENkDZ5u8/61a+X...more encoding" />
答案 2 :(得分:0)
我和一位SendGrid客户支持工程师谈过,他指示我来这里..
所以你的代码就是......
var message = SendGrid.GetInstance();
message.Subject = "Warning";
message.From = new MailAddress("from_user@test.com");
message.To = new MailAddress[] { new MailAddress("to_user@test.com") };
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>";
// added
message.EmbedImage(@"C:\my_image.png","my_image");
// added
var transportSMTP = SMTP.GetInstance(new NetworkCredential("user", "pass"));
transportSMTP.Deliver(message);
我正在寻找一种方法来添加一个MemoryStream对象而不是一个文件名,但它看起来还没有。
答案 3 :(得分:0)
对于那些正在努力通过SendGrid在电子邮件中发送嵌入图像并使用存储模板的人来说,以下是它的工作原理:
/* 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));
最后一行将替换值。在我的情况下,它只是一个正则表达式替换。此代码必须注意使用%embedLogo%
中linkedResource.ContentId+".jpg"
之前存储的values
值替换您的特定JSON中的Dictionary<string,string>
。
以下是您在存储的HTML模板中所需的内容:
<img width="227" height="86" src="cid::embedLogo">
CID和两个冒号是至关重要的。
然后Gmail会显示嵌入的图片而不仅仅是附加图片!