none
Getting (impossible!) ArgumentNullException from EnsureStrongCryptoSettingsInitialized() in System.Net.Http when sending email RRS feed

  • Question

  • I have a WPF app running on the 4.6.1 framework.

    I am trying to send an email. It works perfectly on other PCs, but not two that I am aware of. One is Windows 10 - the other 8.1. Both have 4.6.1 framework installed.

    Here is my email-sending code:

    var messageContents = "Some Body";
    var message = new MailMessage(
                        @"an.email.address@gmail.com",
                        "different.address@microsoft.com.au",
                        $"🐞 Fatal errors from AppName",
                        messageContents)
                    {
                        From = new MailAddress(@"an.email.address@gmail.com", "A Display Name"),
                        ReplyToList = {@"random@gmail.com"},
                        Sender = new MailAddress(@"an.email.address@gmail.com", "Sender Display"),
                    };
    
    var attach = new Attachment(file); // Create  the file attachment for this e-mail message.
    message.Attachments.Add(attach); // Add the file attachment to this e-mail message.
    var client = new SmtpClient
    {
        EnableSsl = true,
        Host = @"smtp.gmail.com",
        Port = 587,
        Credentials = new NetworkCredential(@"an.email.address@gmail.com", @"*AppropriatePassword*"),
    };
    
    client.Send(message);

    When this executes, in the client.Send method, I get a

    Exception thrown: 'System.ArgumentNullException' in mscorlib.dll

    Additional information: Value cannot be null.

    With the resulting callstack:

         mscorlib.dll!System.Enum.TryParseEnum(System.Type enumType, string value, bool ignoreCase, ref System.Enum.EnumResult parseResult)    Unknown

         mscorlib.dll!System.Enum.Parse(System.Type enumType, string value, bool ignoreCase)    Unknown
         System.dll!System.Net.ServicePointManager.EnsureStrongCryptoSettingsInitialized()    Unknown
         System.dll!System.Net.ServicePointManager.SecurityProtocol.get()    Unknown
         System.dll!System.Net.TlsStream.ProcessAuthentication(System.Net.LazyAsyncResult result)    Unknown
         System.dll!System.Net.TlsStream.Write(byte[] buffer, int offset, int size)    Unknown
         System.dll!System.Net.PooledStream.Write(byte[] buffer, int offset, int size)    Unknown
         System.dll!System.Net.Mail.SmtpConnection.Flush()    Unknown
         System.dll!System.Net.Mail.ReadLinesCommand.Send(System.Net.Mail.SmtpConnection conn)    Unknown
         System.dll!System.Net.Mail.EHelloCommand.Send(System.Net.Mail.SmtpConnection conn, string domain)    Unknown
         System.dll!System.Net.Mail.SmtpConnection.GetConnection(System.Net.ServicePoint servicePoint)    Unknown
         System.dll!System.Net.Mail.SmtpTransport.GetConnection(System.Net.ServicePoint servicePoint)    Unknown
         System.dll!System.Net.Mail.SmtpClient.GetConnection()    Unknown
         System.dll!System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage message)    Unknown
         name.dll!name.FatalExceptionHandler.EmailZipfile(string file) Line 196    C#

    According to the documentation on line 690 @ http://referencesource.microsoft.com/#System/net/System/Net/ServicePointManager.cs,3528c78e8b71ece2,references

    It should swallow any parse exceptions, including ArgumentNullException.

    So - What's going on? What am I looking at? How do I sort this out?


    • Edited by PimentDreams Thursday, December 10, 2015 9:27 PM improved title a little
    Thursday, December 10, 2015 9:19 PM

Answers

  • Hi PimentDreams,

    Based on your code, I also made a demo on my side.

    Here I create a console Application build with 4.6.1 framework, it works fine on my side.

    Only one difference, I used Hotmail since I don's have gmail account.

     static void Main(string[] args)
            {
                var messageContents = "Some Body";
                var message = new MailMessage(
                                    @"account@hotmail.com",
                                    "account@microsoft.com",
                                    $"🐞 Fatal errors from AppName",
                                    messageContents)
                {
                    From = new MailAddress(@"account@hotmail.com", "A Display Name"),
                    ReplyToList = { @"account@microsoft.com" },
                    Sender = new MailAddress(@"account@hotmail.com"),
                };
    
        
    
                var attach = new Attachment(@"c:\temp\text.txt"); // Create  the file attachment for this e-mail message.
                message.Attachments.Add(attach);
    
                var client = new SmtpClient
                {
                    EnableSsl = true,
                    Host = @"smtp.live.com",
                    Port = 587,
                    Credentials = new NetworkCredential(@"account@hotmail.com", "password"),
                };
    
                client.Send(message);
            }

    >>Exception thrown: 'System.ArgumentNullException' in mscorlib.dll

    Additional information: Value cannot be null.

    Based on this error, I am think about why throw this error. But seems no anywhere, I only think that your attched file issue. Please try to set a breakpoint to debug your code and check every parameter not null.

    In addition, since you use gmail account, please also note the following case that I repied before.

    send email error,

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, December 11, 2015 7:04 AM

All replies

  • Hi PimentDreams,

    Based on your code, I also made a demo on my side.

    Here I create a console Application build with 4.6.1 framework, it works fine on my side.

    Only one difference, I used Hotmail since I don's have gmail account.

     static void Main(string[] args)
            {
                var messageContents = "Some Body";
                var message = new MailMessage(
                                    @"account@hotmail.com",
                                    "account@microsoft.com",
                                    $"🐞 Fatal errors from AppName",
                                    messageContents)
                {
                    From = new MailAddress(@"account@hotmail.com", "A Display Name"),
                    ReplyToList = { @"account@microsoft.com" },
                    Sender = new MailAddress(@"account@hotmail.com"),
                };
    
        
    
                var attach = new Attachment(@"c:\temp\text.txt"); // Create  the file attachment for this e-mail message.
                message.Attachments.Add(attach);
    
                var client = new SmtpClient
                {
                    EnableSsl = true,
                    Host = @"smtp.live.com",
                    Port = 587,
                    Credentials = new NetworkCredential(@"account@hotmail.com", "password"),
                };
    
                client.Send(message);
            }

    >>Exception thrown: 'System.ArgumentNullException' in mscorlib.dll

    Additional information: Value cannot be null.

    Based on this error, I am think about why throw this error. But seems no anywhere, I only think that your attched file issue. Please try to set a breakpoint to debug your code and check every parameter not null.

    In addition, since you use gmail account, please also note the following case that I repied before.

    send email error,

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Friday, December 11, 2015 7:04 AM
  • Even if I don't have an attachment - the error is the same.

    The thing I don't understand, is that I am getting a System.ArgumentNullException from the following code (in EnsureStrongCryptoSettingsInitialized()) which specifically doesn't throw an ArgumentNullException. How am I geting it?

                    try {
                        bool disableStrongCryptoInternal = false;
                        int schUseStrongCryptoKeyValue = 0;
     
                        if (LocalAppContextSwitches.DontEnableSchUseStrongCrypto)
                        {
                            //.Net 4.5.2 and below will default to false unless the registry key is specifically set to 1.
                            schUseStrongCryptoKeyValue =
                                RegistryConfiguration.GlobalConfigReadInt(strongCryptoValueName, 0);
     
                            disableStrongCryptoInternal = schUseStrongCryptoKeyValue != 1;
                        }
                        else
                        {
                            // .Net 4.6 and above will default to true unless the registry key is specifically set to 0.
                            schUseStrongCryptoKeyValue = 
                                RegistryConfiguration.GlobalConfigReadInt(strongCryptoValueName, 1);
     
                            disableStrongCryptoInternal = schUseStrongCryptoKeyValue == 0;
                        }
                        
                        if (disableStrongCryptoInternal) {
                            // Revert the SecurityProtocol selection to the legacy combination.
                            s_SecurityProtocolType = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
                        }
                        else {
                            s_SecurityProtocolType =
                                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
     
                            string appSetting = RegistryConfiguration.AppConfigReadString(secureProtocolAppSetting, null);
     
                            SecurityProtocolType value;
                            try {
                                value = (SecurityProtocolType)Enum.Parse(typeof(SecurityProtocolType), appSetting);
                                ValidateSecurityProtocol(value);
                                s_SecurityProtocolType = value;
                            }
                            // Ignore all potential exceptions caused by Enum.Parse.
                            catch (ArgumentNullException) { }
                            catch (ArgumentException) { }
                            catch (NotSupportedException) { }
                            catch (OverflowException) { }
                        }
     
                        disableStrongCrypto = disableStrongCryptoInternal;
                        disableStrongCryptoInitialized = true;
                    }
                    catch (Exception e) {
                        if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                            throw;
                        }
                    }

    Saturday, December 12, 2015 3:54 PM
  • Hi PimentDreams,

    Based on the above code, I assume the exception is because some other class call unmanaged code to cause this issue. Please follow my code as my first reply. It works fine on my side. (Win10+4.6.1+Console.Application+x64)

    Please also have a try.

    Since I cannot reproduce your issue, if above solution still cannot get it work, could you upload your project to OneDrive if possible? Or please provide a simplified demo to help me test it.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 15, 2015 8:35 AM

  • Hi PimentDreams,

    Since we haven't heard from you for a  long time, I assume the issue is resolved, and I've tested your code, it works fine on my side, If you have any concerns, please feel free to reopen it or submit a new question. Thanks for your understanding.

    Best regards,

    Kristin



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Tuesday, December 22, 2015 1:51 AM
    Tuesday, December 22, 2015 1:50 AM
  • Hi, sorry for opening up an old thread.

    I just ran into the exact same issue as OP.

    When looking at the source code, as stated above, the issue is clearly the part stating:

    string
     appSetting =
    RegistryConfiguration.AppConfigReadString(secureProtocolAppSetting,
    null);
    SecurityProtocolType
     value;
    try
    {
     value =
    (SecurityProtocolType)Enum.Parse(typeof(SecurityProtocolType),
     appSetting);
    ValidateSecurityProtocol(value);
     s_SecurityProtocolType =
     value;
    }
    // Ignore all potential exceptions caused by Enum.Parse.
    catch
    (ArgumentNullException)
    {
    }
    catch
    (ArgumentException)
    {
    }
    catch
    (NotSupportedException)
    {
    }
    catch
    (OverflowException)
    {
    }


    The setting is read from secureProtocolAppSetting and defaults to null.

    Null is not parseable into an SecurityProtocolType and thereby an exception is thrown.

    Even if the exception is swallowed in a catch, this itself is in my eyes as wrong as wrong can be.

    Now to the reason this angers me.

    I as well as OP and others have spent hours if not days to figure out why this exception is thrown.

    What are we doing wrong? And then it turns out that this exception is an expected behavior and (to the point why it might not be visible to you) this exception is only shown if you have "Just my code" disabled in the debugging settings.

    For some people, "Just my code" or ignoring the exception is fine enough, but for a framework as big as .Net you should not be shown an exception for something as simple as reading a property.

    If you want to reproduce this, all you have to do is turn off "Just my code", set target fw to 4.6 or 4.6.1 and add the code:

    var protocol = ServicePointManager.SecurityProtocol;

    And ensure that you don't have a value to be read from secureProtocolAppSettings.

    Thanks

    Patrick


    • Edited by mr_frostfire Tuesday, April 19, 2016 10:23 PM
    • Proposed as answer by The Pademelon Tuesday, February 7, 2017 11:46 PM
    Tuesday, April 19, 2016 10:22 PM
  • It's so annoying during debugging that ServicePointManager throws ArgumentNullException and catches it. This didn't happen in .NET FX 4.5.X The new 4.6 code:

                            SecurityProtocolType value;
                            try {
                                value = (SecurityProtocolType)Enum.Parse(typeof(SecurityProtocolType), appSetting);
                                ValidateSecurityProtocol(value);
                                s_SecurityProtocolType = value;
                            }
                            // Ignore all potential exceptions caused by Enum.Parse.
                            catch (ArgumentNullException) { }
                            catch (ArgumentException) { }
                            catch (NotSupportedException) { }
                            catch (OverflowException) { }



    What?? How about Enum.TryParse and a NULL check??
    Thursday, May 12, 2016 9:38 PM
  • Still happening !! This is seriously a bad situation!

    R.J.

    Wednesday, May 24, 2017 8:59 PM
  • Hi this still happens.


    R.J.

    Wednesday, May 31, 2017 3:55 PM