2014-12-06 34 views
4

我正在使用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); 
     } 
    } 

回答

7

我想出瞭如何得到這個工作。以下是使用的代碼:

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("[email protected]", "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; 
+0

我一直在努力讓'EmbedImage'調用工作正常,但是這讓我很滿意,謝謝! ;) – 2014-12-15 02:21:31

+0

你應該指定attachment.Name,因爲默認情況下它是NULL – SochiX 2015-10-22 05:56:36

+0

這些圖像是否實際顯示爲嵌入?當我使用HTML中的''發送並在API中添加了「cid:logo.png」附件時,它們在Outlook中正常工作,但在Gmail中無法正常工作。嘗試使用您的代碼,並且Outlook仍顯示嵌入式,但Gmail仍顯示空白佔位符。 – ajeh 2017-01-25 21:39:43

3

不需要創建ContentType和LinkedResource對象。添加嵌入式圖像作爲附件。另外EmbedImage方法需要圖像名稱而不是完整的路徑。

下面的代碼工作:

VAR myMessage =新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.AddAttachment(@"C:\logo.png"); 
     myMessage.EmbedImage("logo.png", "Logo");** 

     SendMessage(myMessage);