1

为什么这段代码:

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>
4

2 回答 2

0

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

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

于 2012-03-08T02:47:06.320 回答
0

为什么它会抛出它?可能是因为 MS 在幕后做了一些愚蠢的事情。

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

我的下意识反应是:

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

  2. 确保您有权访问该 dll。

于 2012-03-08T13:50:20.940 回答