SendGrid SMTP API : Embed Image : Bad Request

北战南征 提交于 2019-12-03 16:32:26

I figured out how to get this working. Here is the code used:

var myMessage = new SendGridMessage();

contact_list = new List<MailAddress>();
contact_list.Add(new MailAddress("my email address"));

myMessage.To = contact_list.ToArray();
myMessage.From = new MailAddress("clientservice@stpis.com", "STP Client Service");
myMessage.Subject = "STP Report Package: " + package_report_name;
string img = @"C:\\logo.png";
ContentType ctype = new ContentType("image/png");
var attachment = new Attachment(img, ctype);
var linkedResource = new LinkedResource(img, ctype);
myMessage.AddAttachment(attachment.ContentStream, attachment.Name);
myMessage.EmbedImage(attachment.Name, linkedResource.ContentId);

string html = "<img src=cid:"+linkedResource.ContentId+" />";
myMessage.Html = html;

there is no need to create ContentType and LinkedResource objects. Add embedded image as an attachment. Also EmbedImage method expects image name and not the complete path.

Following code works:

var myMessage = new SendGridMessage();

        contact_list = new List<MailAddress>();
        contact_list.Add(new MailAddress("email@gmail.com"));
        myMessage.To = contact_list.ToArray();
        myMessage.From = new MailAddress("clientservice@stpis.com");
        myMessage.Subject = "Subject";

        string html = "<div><img src=cid:Logo /></div>";

        myMessage.Html = html;
        **myMessage.AddAttachment(@"C:\logo.png");
        myMessage.EmbedImage("logo.png", "Logo");**

        SendMessage(myMessage);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!