我正在使用sendgrid SMTP API https://github.com/sendgrid/sendgrid-csharp發送電子郵件,但我無法弄清楚如何嵌入圖像。我可以使用.Net本機郵件API來做到這一點。我只是收到一個錯誤的請求。這裏是我的代碼是投擲SendGrid SMTP API:嵌入圖像:錯誤請求
private static void Main(string[] args)
{
try {
//// Create the email object first, then add the properties.
var myMessage = new SendGridMessage();
contact_list = new List<MailAddress>();
contact_list.Add(new MailAddress("[email protected]"));
myMessage.To = contact_list.ToArray();
myMessage.From = new MailAddress("[email protected]");
myMessage.Subject = "Subject";
string html = "<div><img src=cid:Logo /></div>";
myMessage.Html = html;
myMessage.EmbedImage(@"C:\logo.png", "Logo");
SendMessage(myMessage);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
private static void SendMessage(SendGridMessage message)
{
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("username", "pwdpwdpwd");
// Create a Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
try
{
transportWeb.Deliver(message);
Console.WriteLine("Sent!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
我一直在努力讓'EmbedImage'調用工作正常,但是這讓我很滿意,謝謝! ;) – 2014-12-15 02:21:31
你應該指定attachment.Name,因爲默認情況下它是NULL – SochiX 2015-10-22 05:56:36
這些圖像是否實際顯示爲嵌入?當我使用HTML中的''發送並在API中添加了「cid:logo.png」附件時,它們在Outlook中正常工作,但在Gmail中無法正常工作。嘗試使用您的代碼,並且Outlook仍顯示嵌入式,但Gmail仍顯示空白佔位符。 – ajeh 2017-01-25 21:39:43