You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET VB调用SMTP发邮件报System.Net.Mail.SmtpException错误如何解决

SMTP发送邮件触发SmtpException错误修复方案

错误说明

你遇到的System.Net.Mail.SmtpException: Failure sending mail错误,根因是内部SSL握手失败:System.IO.IOException: The handshake failed due to an unexpected packet format,该错误由SMTP配置不匹配直接导致。

现存配置问题

你的代码存在三处可直接触发报错的问题:

  1. 凭据配置冲突:同时开启了默认凭据和自定义凭据,UseDefaultCredentials = True会覆盖后续设置的账号密码,导致SMTP认证失败
  2. 端口与SSL配置不匹配:不同SMTP端口对SSL的支持规则不同,混用会直接导致握手失败
  3. 代码语法错误:Catch异常输出的代码末尾多了一个多余的双引号,会引发编译错误

修复步骤

  • 调整凭据配置顺序,禁用默认凭据:将smtp_server.UseDefaultCredentials = True改为smtp_server.UseDefaultCredentials = False,且必须放在smtp_server.Credentials赋值语句之前
  • 匹配端口与SSL规则:根据你的SMTP服务商配置二选一测试:
    • 若服务商支持587端口STARTTLS:保留smtp_server.Port = 587和smtp_server.EnableSsl = True
    • 若服务商仅支持465隐式SSL:注意旧版.NET Framework的SmtpClient原生不支持465端口,可替换为端口25+EnableSsl=False临时测试,生产环境建议升级框架或使用第三方邮件发送库
  • 修正语法错误:将报错行的多余双引号删除,改为Response.Write("Failed to deliver message to " & i & " error::: " & ex.InnerExceptions(i).FailedRecipient)

修正后参考代码

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Net.Mail" %>
<script runat="server"> 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim Subject As String = "This is test mail with html web page"
        Dim ToEmail As String = "toemail@sample.com"
        Dim SMTPUser As String = "info@mysite.com"
        Dim SMTPPassword As String = "mysamplepass"
        Dim e_mail As New System.Net.Mail.MailMessage()
        e_mail.From = New MailAddress(SMTPUser)
        e_mail.To.Add(ToEmail)
        e_mail.Subject = "testEmail"
        e_mail.IsBodyHtml = True
        e_mail.Body = "<p>send test message to email</p>"
        e_mail.Priority = Net.Mail.MailPriority.High
        Dim smtp_server As New SmtpClient
        ' 先禁用默认凭据,再设置自定义账号密码
        smtp_server.UseDefaultCredentials = False
        smtp_server.Credentials = New Net.NetworkCredential(SMTPUser, SMTPPassword)
        smtp_server.Port = 587
        smtp_server.EnableSsl = True
        smtp_server.Host = "mail.mysite.com"
        Try
            smtp_server.Send(e_mail)
        Catch ex As SmtpFailedRecipientsException
            For i = 0 To ex.InnerExceptions.Length Step 1
                Dim status As SmtpStatusCode = ex.InnerExceptions(i).StatusCode
                If status = SmtpStatusCode.MailboxBusy Or status = SmtpStatusCode.MailboxUnavailable Then
                    Response.Write("Delivery failed - retrying in 5 seconds.")
                    System.Threading.Thread.Sleep(5000)
                    smtp_server.Send(e_mail)
                Else
                    Response.Write("Failed to deliver message to " & i & " error::: " & ex.InnerExceptions(i).FailedRecipient)
                End If
            Next i
        Catch ex As Exception
            Response.Write("Exception caught in RetryIfBusy(): " & ex.ToString())
        End Try
    End Sub
</script>

内容的提问来源于stack exchange,提问作者Farrokh Seyrafian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 21:15:03