我正在尝试使用 c# 通过 asp.net 中的应用程序发送电子邮件。我搜索了很多,主要是在 asp.net 中找到以下代码来使用 c# 发送电子邮件:
MailMessage objEmail = new MailMessage();
objEmail.From = new MailAddress(txtFrom.Text);
objEmail.To.Add(txtTo.Text);
objEmail.CC.Add(txtCC.Text);
objEmail.Bcc.Add(txtBCC.Text);
objEmail.Subject = txtSubject.Text;
try
{
SmtpClient mail = new SmtpClient();
mail.EnableSsl = true;
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
mail.Timeout = 20000;
mail.UseDefaultCredentials = false;
mail.Host = "smtp.gmail.com";
mail.Port = 587;
mail.Send(objEmail);
Response.Write("Your Email has been sent sucessfully - Thank You");
}
catch (Exception exc)
{
Response.Write("Send failure due to : <br />" + exc.ToString());
}
但我经常收到以下错误:
“System.Net.Mail.SmtpException:发送邮件失败。---> System.IO.IOException:无法从传输连接读取数据:现有连接被远程主机强行关闭。---> System.Net .Sockets.SocketException:现有连接被远程主机在 System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read 处强行关闭(字节 [] 缓冲区,Int32 偏移量,Int32 大小)--- 内部异常堆栈跟踪结束 --- 在 System.Net.Sockets.NetworkStream.Read(Byte[] 缓冲区,Int32 偏移量,Int32 大小)在 System.Net System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) 在 System.Net.Mail.SmtpReplyReaderFactory 处的 .DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)。ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.StartTlsCommand.Send( SmtpConnection conn) 在 System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) 在 System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 在 System.Net.Mail.SmtpClient.GetConnection() 在 System.Net.Mail。 SmtpClient.Send(MailMessage 消息)"GetConnection(ServicePoint servicePoint) 在 System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 在 System.Net.Mail.SmtpClient.GetConnection() 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)"GetConnection(ServicePoint servicePoint) 在 System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) 在 System.Net.Mail.SmtpClient.GetConnection() 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)"