C# new MailMessage()抛出';句柄无效';

C# new MailMessage()抛出';句柄无效';,c#,asp.net,asp.net-4.0,system.net.mail,C#,Asp.net,Asp.net 4.0,System.net.mail,为什么该代码: MailMessage mm = new MailMessage(); 抛出此异常: System.Security.Cryptography.CryptographicException was caught Message=The handle is invalid. Source=mscorlib StackTrace: at System.Security.SecureString.ProtectMemory() at System.Security.Se

为什么该代码:

MailMessage mm = new MailMessage();
抛出此异常:

System.Security.Cryptography.CryptographicException was caught
Message=The handle is invalid.

Source=mscorlib
StackTrace:
   at System.Security.SecureString.ProtectMemory()
   at System.Security.SecureString.InitializeSecureString(Char* value, Int32 length)
   at System.Security.SecureString..ctor(Char* value, Int32 length)
   at System.Net.UnsafeNclNativeMethods.SecureStringHelper.CreateSecureString(String plainString)
   at System.Net.Configuration.SmtpNetworkElementInternal..ctor(SmtpNetworkElement element)
   at System.Net.Configuration.SmtpSectionInternal..ctor(SmtpSection section)
   at System.Net.Configuration.SmtpSectionInternal.GetSection()
   at System.Net.Mail.SmtpClient.get_MailConfiguration()
   at System.Net.Mail.MailMessage..ctor()
   at csEmail.GetMailMessage(String subject, String htmlbody) in c:\Users\Greg\My Dropbox\Intern Files\mobiledesign\App_Code\Utilities.cs:line 364
InnerException: 
我的Web.config

<smtp deliveryMethod="Network" from="correctAddress">
    <network host="smtp.gmail.com" userName="correctAddress" password="correctPassword" enableSsl="true" port="587"/>
</smtp>

我相信实例化邮件消息不会从Web.config读取。SmtpClient读取web.config中设置的SMTP


你能展示更多你的代码吗?可能是GetMailMessage方法的其余部分。

为什么会抛出它?可能是因为MS在幕后做了些傻事

IIRC,该SecureString代码实际上对advapi32.dll进行了互操作调用

我的下意识反应是:

  • 禁用任何防病毒软件,看看是否可以修复它

  • 确保您有权访问该dll


  • 你的web.config在smtp部分说了什么?real answer
    MS在幕后做了一些傻事
    它今天神奇地工作得很好。