系统安全性加密异常:句柄无效。

6

我在使用C#发送邮件时遇到了异常

SmtpClient client = new SmtpClient() 

出现 System.Security.Cryptography.CryptographicException: The handle is invalid. 错误。

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);

我不明白为什么会发生这种情况?


可能是重复的问题:new MailMessage() Throws 'The handle is invalid' - V4Vendetta
端口465不是用于SSL吗? - V4Vendetta
2
尝试在MailMessage和SmtpClient中使用“using”块进行包装:using (var mail = new MailMessage(...)){... using (var client = new SmtpClient(...)){ ...}} - John Saunders
1
我在另一个线程中找到了解决方案,请查看以下链接:https://dev59.com/CU_Ta4cB1Zd3GeqPENf_ - tHARA_sOFT
哪一行抛出了异常?是设置凭据的那一行吗?如果是,我猜问题与密码属性有关。另外,请展示您的配置。空构造函数使用mailSettings配置元素。 - Ikaso
显示剩余2条评论
1个回答

1

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

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接