问题
Is it possible to use Send Grid in Console Application in C#? My code doesn't work and I really do not know why. Could you help me?
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("adresfrom@example.com", "It's me");
myMessage.AddTo("adressj@gmail.com");
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();
}
}
}
回答1:
Couple of points about your code:
- You're not awaiting on DeliverAsync() call - this may cause certain issues. Call it like this:
await transportWeb.DeliverAsync(mail).ConfigureAwait(true);
- Correct me If Im wrong, but on Azure you can't use NetworkCredentials as they aren't being sent properly to SendGrid API. We are using API key to configure transportWeb object.
So try this fixed code and let us know how it goes:
var transportWeb = new Web(<your-api-key-here>);
await transportWeb.DeliverAsync(myMessage).ConfigureAwait(true);
回答2:
Actually try this:
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);
}
It looks like you should await DeliveryAsync method. In Main method when you remove Wait() it will NOT send email. If you use Wait() it will send.
回答3:
It does work in a console application but you need to call it like this for some reason:
transportWeb.DeliverAsync(msg).Wait()
More here: https://github.com/sendgrid/sendgrid-csharp/issues/154
回答4:
Try to use SendGrid API and connect to it by 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", "some@body.com"),
new KeyValuePair<string, string>("to", "some@body.com"),
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);
}
Change the values as yours, It's working for me ..
来源:https://stackoverflow.com/questions/32833703/using-sendgrid-in-a-console-application