2015-09-29 45 views
2

是否可以在C#中使用控制檯應用程序中的發送網格?我的代碼不起作用,我真的不知道爲什麼。你可以幫幫我嗎?在控制檯應用程序中使用SendGrid

using System; 
using System.Net; 
using System.Net.Mail; 
using SendGrid; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create the email object first, then add the properties. 
      var myMessage = new SendGridMessage(); 
      myMessage.From = new MailAddress("[email protected]", "It's me"); 
      myMessage.AddTo("[email protected]"); 
      myMessage.Subject = "Testing the SendGrid Library"; 
      myMessage.Text = "Content of the e-mail"; 

      var username = "my_sendgrid_username"; 
      var password = "my_sendgrid_password"; 

      var credentials = new NetworkCredential(username, password); 

      // Create a Web transport for sending email. 
      var transportWeb = new Web(credentials); 
      transportWeb.DeliverAsync(myMessage); 
      Console.ReadLine(); 
     } 
    } 
} 
+0

您是否收到任何錯誤?什麼沒有發生?你有沒有用調試器完成它? – Ben

+0

當沒有等待的時候,我也有類似的行爲。它只是不發送沒有任何錯誤的電子郵件。當我等待DeliveryAsync方法時,它開始發送電子郵件。 – kat1330

+0

這不是你的問題的答案(它應該是可能的,其他人已經解決它),但你也可以使用常規.NET SMTP與Sendgrid的東西。 – Casey

回答

0

嘗試使用SendGrid API,並連接到它通過的HttpClient:

using (var client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri("https://api.sendgrid.com"); 
      var content = new FormUrlEncodedContent(new[] 
     { 
      new KeyValuePair<string, string>("api_key", "YOUR PASSWORD"), 
      new KeyValuePair<string, string>("api_user", "USER NAME"), 
      new KeyValuePair<string, string>("from", "[email protected]"), 
      new KeyValuePair<string, string>("to", "[email protected]"), 
      new KeyValuePair<string, string>("subject", "Whatever"), 
      new KeyValuePair<string,string>("text", "You can use 'html' :)") 

     }); 
      var result = client.PostAsync("/api/mail.send.json", content).Result; 
      string resultContent = result.Content.ReadAsStringAsync().Result; 
      Debug.WriteLine(resultContent); 
     } 

將值更改爲你的,它的工作對我來說..

+0

任何想法的附件? – Steve

3

夫婦對你的代碼點:

  • 您不是在等待DeliverAsync()調用 - 這可能會導致某些問題。這樣稱呼它:

await transportWeb.DeliverAsync(mail).ConfigureAwait(true);

  • 糾正我,如果我錯了,但是在Azure上,你不能因爲他們沒有被正確地發送到SendGrid API使用NetworkCredentials。我們正在使用API​​密鑰來配置transportWeb對象。

那麼試試這個固定碼,讓我們知道如何去:

var transportWeb = new Web(<your-api-key-here>); 
await transportWeb.DeliverAsync(myMessage).ConfigureAwait(true); 
1

其實試試這個:

static void Main() 
    { 
     SendEmail().Wait(); 
    } 

    static async Task SendEmail() 
    { 
     var myMessage = new SendGridMessage(); 
     myMessage.From = new MailAddress("from_emal"); 
     myMessage.AddTo("to_email"); 
     myMessage.Subject = "Testing the SendGrid Library"; 
     myMessage.Html = "<p>Hello World!</p>"; 
     myMessage.Text = "Hello World plain text!"; 

     var credentials = new NetworkCredential("sendgrid_user", "sendgrid_pass"); 
     var transportWeb = new Web(credentials); 
     await transportWeb.DeliverAsync(myMessage);    
    } 

看起來你應該等待DeliveryAsync方法。在Main方法中,當您移除Wait()時,它不會發送電子郵件。如果你使用Wait()它會發送。

+0

這是如何在VisualBasic中完成的? –