Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Security.Cryptography.CryptographyException:句柄无效_C#_Asp.net_Asp.net Mvc 3_Sendmail - Fatal编程技术网

C# System.Security.Cryptography.CryptographyException:句柄无效

C# System.Security.Cryptography.CryptographyException:句柄无效,c#,asp.net,asp.net-mvc-3,sendmail,C#,Asp.net,Asp.net Mvc 3,Sendmail,我通过C#for发送邮件时遇到异常 asSystem.Security.Cryptography.CryptographyException:句柄无效。 MailMessage mail = new MailMessage(from,to); mail.To.Add(to); mail.From = new MailAddress(from, "", System.Text.Encoding.UTF8); mail.Su

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

asSystem.Security.Cryptography.CryptographyException:句柄无效。

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

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

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


ssl的端口不是
465
的可能重复项吗?这不是重复项。另一个问题在
MailMessage
构造函数上有一个例外,但在这里它发生在
SmtpClient
构造函数上。另外,另一个问题没有真正的答案。请尝试将您的邮件和SmtpClient包装在
使用
块中:
使用(var mail=new MailMessage(…){…使用(var client=new SmtpClient(…){…}
我在另一个威胁中找到了解决方案。请查收
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);