简体   繁体   English

新的MailMessage()引发“句柄无效”

[英]new MailMessage() Throws 'The handle is invalid'

Why does this code: 为什么这样的代码:

MailMessage mm = new MailMessage();

Throw this exception: 抛出此异常:

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: 

My Web.config 我的Web.config

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

I believe instantiating the MailMessage doesn't read from the Web.config. 我相信实例化MailMessage不会从Web.config中读取。 The SmtpClient reads the SMTP set up in the web.config. SmtpClient读取在web.config中设置的SMTP。

Can you show more of your code? 您可以显示更多代码吗? Maybe the rest of the GetMailMessage method. 也许其余的GetMailMessage方法。

Why it throws it? 为什么会扔呢? probably because MS is doing something goofy under the covers. 可能是因为MS在后台进行了一些愚蠢的事情。

IIRC, that SecureString code actually makes an interop call down to advapi32.dll. IIRC,该SecureString代码实际上是对advapi32.dll的互操作调用。

My knee jerk reaction is to: 我的膝盖跳动反应是:

  1. Disable any antivirus software, and see if that fixes it. 禁用任何防病毒软件,然后查看是否可以解决该问题。

  2. Make sure you have access to that dll. 确保您有权访问该dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM