Smtp null reference error RRS feed

  • Question

  • While trying to send an email using I am getting an error in release mode smtp.Send fires a null reference exception on a test machine and works perfectly on Development machine.

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(FromEmail);


    mail.Subject = "Test";

    SmtpClient smtp = new SmtpClient();

    smtp.Host = HostName;
    smtp.Port = PortName;

    smtp.UseDefaultCredentials = true;



    Monday, September 16, 2019 6:44 AM

All replies

  • Are you quite sure that HostName and PortName have valid values?  You should print them out to a debug location somewhere, just to be sure.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, September 16, 2019 4:46 PM
  • Hello,

    When composing an SMTP Email try this pattern (not complete).

    var yourHost = "Your host";
    var yourPort = 0;
    var mail = new MailMessage();
    using (var smtp = new SmtpClient(yourHost,yourPort))
        smtp.Credentials = new NetworkCredential("TODO","TODO");
        smtp.EnableSsl = true;

    The only time not to create the client in a using statement is when you subscribe to SendComplete delegate.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Monday, September 16, 2019 5:11 PM
  • Look at SmtpClient.Send Method. I see multiple things that can be null. Also look at the sample there. It shows use of try/catch. When you get errors such as this it is best to check for errors. Actually, it is best to check for errors to prevent getting errors like this.

    Sam Hobbs

    Monday, September 16, 2019 8:05 PM

    Thank you for posting here.

    According to your description, I make a test on my side and send an email to my Hotmail account successfully.

    It works well after I publish the project.

    Here’s the code, and you can refer it.

                Console.WriteLine("Please enter your hotmail account");
                string account = Console.ReadLine();
                MailAddress from = new MailAddress(account);
                Console.WriteLine("Please enter the password of your account");
                string password = Console.ReadLine();
                Console.WriteLine("Please enter the hotmail account you want to send");
                MailAddress to = new MailAddress(Console.ReadLine());
                MailMessage message = new MailMessage(from, to);
                message.Subject = "Test";
                Console.WriteLine("Please enter the content you want to send");
                string content = Console.ReadLine();
                message.Body = content;
                SmtpClient smtp = new SmtpClient("", 587);
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(account, password);
                smtp.Credentials = NetworkCred;

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, September 18, 2019 10:08 AM