Click here to Skip to main content
15,888,401 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



CSS
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

C#
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 26-Apr-22 17:56pm
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

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