Click here to Skip to main content
14,696,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

I am using sendgrid to send an emails, but now whenever i send the email i am getting



System.NullReferenceException was unhandled
Message: An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.Threading.Tasks.dll
Additional information: Object reference not set to an instance of an object.




error.

Its a simple function i am calling to send emails , i tried with try catch block but its not throwing an exception

Below is my method which i am calling from button click

public void SendEmail()
        {

            string emailTo = string.Empty; string emailFrom = string.Empty;
            string subject = string.Empty; string text = string.Empty;
            string html = string.Empty; string userName = string.Empty;
            string password = string.Empty;

            // Create the email object first, then add the properties.
            SendGridMessage myMessage = new SendGridMessage();
            myMessage.AddTo("test@gmail.com ");
            myMessage.From = new MailAddress("test@gmail.com", "User I am");
            myMessage.Subject = "Testing the SendGrid Library";
            myMessage.Text = "Hello World!";
            myMessage.Html = "";

            // Create credentials, specifying your user name and password.
            var credentials = new NetworkCredential("myUserName", "myPassword");

            // Create an Web transport for sending email, using credentials...
            var transportWeb = new Web(credentials);

            // Send the email.
            transportWeb.DeliverAsync(myMessage);
        }




Please suggest
Posted
Updated 13-Jun-15 20:12pm
v4
Comments
Torakami 14-Jun-15 0:38am
   
I checkd the emails and they are coming properly , but due to this error my visual studio is breaking in between
Michael_Davies 14-Jun-15 0:46am
   
We need to see the code.
Torakami 14-Jun-15 0:50am
   
I have updated my question and added method which i am calling , messages are going properly but not sure why the error is coming
Michael_Davies 14-Jun-15 1:02am
   
Missing closing quote: myMessage.AddTo("test@gmail.com);
Torakami 14-Jun-15 1:11am
   
no thats fine , during editing other email id , i missed that , that is not the problem i will correct that
Michael_Davies 14-Jun-15 1:03am
   
Just a comment, all those empty strings you create and never use?

string emailTo = string.Empty; string emailFrom = string.Empty;
string subject = string.Empty; string text = string.Empty;
string html = string.Empty; string userName = string.Empty;
string password = string.Empty;

Just saying...
Torakami 14-Jun-15 1:13am
   
Yes i understand , i will use those variables
   
In what line of what code?
—SA
Torakami 14-Jun-15 1:14am
   
There is no error in any of the line , once the execution gets finished suddenly visual studio prompted me above error which i have mentioned in my question description
Kornfeld Eliyahu Peter 14-Jun-15 2:42am
   
If you use Deliver instead of DeliverAsync, does it work?
Torakami 14-Jun-15 3:52am
   
need to check .. i am first time trying send grid
Torakami 14-Jun-15 5:30am
   
hey man , it worked . How do u know that deleiver will work and whats the difference between deliverasync and deliver ??
Kornfeld Eliyahu Peter 14-Jun-15 5:33am
   
DeliverAsync will run asynchronously (no wait for server)...
It was a know problem between version 3.0 and 5.1 so I thought you should check for...
Torakami 14-Jun-15 6:19am
   
cool , thanks brother .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900