【问题标题】:System.Security.Cryptography.CryptographicException: The handle is invalidSystem.Security.Cryptography.CryptographicException:句柄无效
【发布时间】:2012-08-13 10:06:36
【问题描述】:

我在通过 C# 发送邮件时遇到异常

SmtpClient client = new SmtpClient() 

as System.Security.Cryptography.CryptographicException:句柄无效。

MailMessage mail = new MailMessage(from,to);

            mail.To.Add(to);
            mail.From = new MailAddress(from, "", System.Text.Encoding.UTF8);

            mail.Subject = "This is a test mail";

            mail.SubjectEncoding = System.Text.Encoding.UTF8;

            mail.Body = fr.message;

            mail.BodyEncoding = System.Text.Encoding.UTF8;

            mail.IsBodyHtml = true;

            mail.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();

            //Add the Creddentials- use your own email id and password
            client.Credentials = new System.Net.NetworkCredential(from, Password);

            client.Port = 587; // Gmail works on this port

            client.Host = "smtp.gmail.com";

            client.EnableSsl = true; //Gmail works on Server Secured Layer

                client.Send(mail);

我不明白为什么会这样?

【问题讨论】:

  • 不是ssl的端口465吗?
  • 这不是重复的。另一个问题在 MailMessage 构造函数上有异常,但在这里它发生在 SmtpClient 构造函数上。另外,另一个问题没有真正的答案。
  • 尝试将您的 MailMessage 和 SmtpClient 包装在 using 块中:using (var mail = new MailMessage(...)){ ... using (var client = new SmtpClient(...)){ ...}}
  • 我在另一个威胁中找到了解决方案。请查看stackoverflow.com/questions/3504760/…

标签: c# asp.net asp.net-mvc-3 sendmail


【解决方案1】:

检查您的项目设置并确保您已检查 NTLM 身份验证:

【讨论】:

    猜你喜欢
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多