macieg_b
macieg_b

Reputation: 175

Using SendGrid in a console application

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("[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();
        }
    }
}

Upvotes: 4

Views: 3005

Answers (4)

user1843640
user1843640

Reputation: 3939

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

Upvotes: 1

kat1330
kat1330

Reputation: 5332

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.

Upvotes: 1

Alexey Shcherbak
Alexey Shcherbak

Reputation: 3454

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);

Upvotes: 4

Nasser AlNasser
Nasser AlNasser

Reputation: 1775

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", "[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);
        }

Change the values as yours, It's working for me ..

Upvotes: 0

Related Questions