Skip to main content

 none
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.To.Add(toEmailList);

    mail.Subject = "Test";

    SmtpClient smtp = new SmtpClient();

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

    smtp.UseDefaultCredentials = true;

    smtp.Send(mail);


    saikalyan

    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;
        smtp.Send(mail);
    }

    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.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, September 16, 2019 5:11 PM
    Moderator
  • 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
    SimpleSamples.Info

    Monday, September 16, 2019 8:05 PM
  • Hi SAI KALYAN, 

    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("Smtp.live.com", 587);
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(account, password);
                smtp.Credentials = NetworkCred;
    
                smtp.Send(message);
                Console.WriteLine("success");
                Console.ReadLine();

    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 MSDNFSF@microsoft.com.

    Wednesday, September 18, 2019 10:08 AM
    Moderator