ASP.NET WebForms C#邮件发送失败问题排查求助
问题背景
ASP.NET WebForms站点发送邮件时,能正常发送到部分地址,但无法送达目标地址,抛出连接中止相关异常。
邮件发送代码
protected void btnSend_Click(object sender, EventArgs e) { string Name = txtName.Text; string EmailAddress = txtEmail.Text; string phone = "Number not supplied"; if (txtPhone.Text != "") { phone = txtPhone.Text; } string SendAddress = @"emailsender@address.com"; string Subject = "Message from Top Tree Fellas Website"; string Message = Regex.Replace(txtMsg.Text, @"\r\n?|\n", "<br />"); if (String.IsNullOrEmpty(Name) || String.IsNullOrEmpty(EmailAddress) || String.IsNullOrEmpty(Message)) { lblError.Text = "Please ensure all required fields are filled in."; } else { try { MailMessage msg = new MailMessage(); msg.To.Add(new MailAddress(@"email@address.co.uk")); msg.CC.Add(new MailAddress(@"email@address2.com")); msg.Subject = Subject; msg.From = new MailAddress(SendAddress); msg.IsBodyHtml = true; msg.Body = "<p><strong>Contact Name:</strong> " + Name + "<br />"; msg.Body += "<strong>Email Address:</strong> " + EmailAddress + "<br />"; msg.Body += "<strong>Phone:</strong> " + phone + "<br />"; msg.Body += "<strong>Message:</strong><br />" + Message + "</p>"; msg.ReplyToList.Add(new MailAddress(EmailAddress)); SmtpClient smtpClnt = new SmtpClient("smtp.ionos.co.uk"); //smtpClnt.UseDefaultCredentials = false; smtpClnt.Credentials = new NetworkCredential(@"emailsender@address.com", "password"); smtpClnt.EnableSsl = true; smtpClnt.Send(msg); lblError.ForeColor = System.Drawing.Color.Green; txtName.Text = ""; txtEmail.Text = ""; txtMsg.Text = ""; txtPhone.Text = ""; Response.Write("<script>alert('Message sent');</script>"); } catch (Exception er) { string er1 = er.Message + "<br />" + er.InnerException; lblError.Text = er1; Response.Write("<script>alert('Something went wrong. Please try again later or call us on: 07719 306404');</script>"); } } }
报错信息
Failure sending mail
内部异常
System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Mail.SmtpPooledStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
排查思路与解决建议
- 指定SMTP端口:Ionos的SMTP服务启用SSL时常用465或587端口,当前代码未设置端口,默认25端口可能被服务商禁用。添加
smtpClnt.Port = 465;或587测试。 - 修复凭据配置:取消注释
smtpClnt.UseDefaultCredentials = false;,确保先禁用默认凭据再设置自定义SMTP凭据,避免冲突。 - 检查目标邮箱反垃圾策略:目标地址
email@address.co.uk可能将邮件标记为垃圾,查看该邮箱的垃圾邮件文件夹,或把发件地址加入白名单。 - 验证服务器网络限制:站点所在服务器的防火墙/安全组可能阻止了SMTP出站连接,联系主机商确认是否允许访问Ionos的SMTP端口。
- 强制TLS版本:旧版.NET Framework默认SSL/TLS版本可能不兼容Ionos服务,在创建SmtpClient前添加代码:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;。 - 简化收件人测试:暂时移除CC地址,只发送到目标地址,排除多收件人引发的问题。
- 验证SMTP凭据有效性:用邮件客户端(如Outlook)配置相同的SMTP服务器、账号和密码,测试能否发送,确认凭据是否正确。
内容的提问来源于stack exchange,提问作者M_Griffiths

