在 vb.net 2010 中发送电子邮件时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18787813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:03:55  来源:igfitidea点击:

Error sending email in vb.net 2010

vb.netvb.net-2010

提问by User7291

I have to following code:

我必须遵循以下代码:

Try
    Dim mail As New MailMessage()
    Dim SmtpServer As New SmtpClient("smtp.gmail.com")
    mail.From = New MailAddress(txtid.Text)
    mail.[To].Add(TextBox1.Text)
    mail.Subject = txtsub.Text
    mail.Body = txtmess.Text
    ' mail.Attachments.Add(New Attachment(OpenFileDialog1.FileName))
    SmtpServer.Port = 587
    SmtpServer.Credentials = New System.Net.NetworkCredential(txtid.Text, txtpass.Text)
    SmtpServer.EnableSsl = True

    SmtpServer.Send(mail)
    MsgBox("E-mail Has Been Send Successfully !")
Catch ex As Exception
    MsgBox(ex.Message)
End Try

on the step

在台阶上

SmtpServer.Send(mail)

I'm always getting an error : Failure sending mail

我总是收到错误消息:发送邮件失败

Any idea how to fix it?

知道如何修复它吗?

Note that I'm using VB.NET

请注意,我使用的是 VB.NET

采纳答案by Jason Hughes

Try configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), works for me.

尝试在端口 465(使用 SSL)和端口 587(使用 TLS)上配置您的 SMTP 服务器,这对我有用。

回答by Karl Anderson

Since you are receiving an SmtpException, the SmtpClient.Send Method (MailMessage)documentation states that the following reasons could be the cause:

由于您收到的是SmtpExceptionSmtpClient.Send 方法 (MailMessage)文档指出以下原因可能是原因:

  • The connection to the SMTP server failed.
  • Authentication failed.
  • The operation timed out.
  • EnableSslis set to true, but the DeliveryMethodproperty is set to SpecifiedPickupDirectoryor PickupDirectoryFromIis.
  • EnableSslis set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.
  • 与 SMTP 服务器的连接失败。
  • 身份验证失败。
  • 操作超时。
  • EnableSsl设置为true,但DeliveryMethod属性设置为SpecifiedPickupDirectoryPickupDirectoryFromIis
  • EnableSsl设置为true,但 SMTP 邮件服务器没有在对 EHLO 命令的响应中通告 STARTTLS。

I would use Trim()with your username and password text values to remove any potential leading or trailing spaces, like this:

我将使用Trim()您的用户名和密码文本值来删除任何潜在的前导或尾随空格,如下所示:

SmtpServer.Credentials = New System.Net.NetworkCredential(txtid.Text.Trim(), 
                                                          txtpass.Text,Trim())

I would also recommend forcing the DeliveryMethodto use the SMTP server, like this:

我还建议强制DeliveryMethod使用 SMTP 服务器,如下所示:

SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network

回答by Geoff

My own experiments suggest that .NET's SmtpClient class doesn't actually support SMTP over SSL (SMTPS). Toggling the .EnableSsl flag on your client object will instead cause the client to use STARTTLS.

我自己的实验表明 .NET 的 SmtpClient 类实际上并不支持 SMTP over SSL (SMTPS)。切换客户端对象上的 .EnableSsl 标志将导致客户端使用 STARTTLS。

This isn't surprising given that SMTPS has been deprecated in favor of STARTTLS for a number of reasons, most of which have to do with seamless interoperability between new and old clients and servers.

鉴于出于多种原因,SMTPS 已被弃用而支持 STARTTLS,这并不奇怪,其中大部分原因与新旧客户端和服务器之间的无缝互操作性有关。

SMTPS: The client opens a TCP connection with the target server. The two start an SSL handshake immediately and then proceed to communicate through that SSL tunnel using the regular SMTP protocol (EHLO, AUTH, etc).

SMTPS:客户端打开与目标服务器的 TCP 连接。两者立即开始 SSL 握手,然后使用常规 SMTP 协议(​​EHLO、AUTH 等)通过该 SSL 隧道进行通信。

STARTTLS: The client opens a TCP connection with the target server. The client issues an EHLO and then a STARTTLS. This is done using plain text. The STARTTLS command prompts the client and server to start an SSL handshake over the already open socket. All future communication (AUTH command and beyond) is done through the now-established SSL tunnel.

STARTTLS:客户端打开与目标服务器的 TCP 连接。客户端发出 EHLO,然后发出 STARTTLS。这是使用纯文本完成的。STARTTLS 命令提示客户端和服务器在已经打开的套接字上启动 SSL 握手。所有未来的通信(AUTH 命令及其他)都通过现在建立的 SSL 隧道完成。

This presents a problem when the SmtpClient object is connecting to a server that is expecting SMTPS. The client will send an EHLO after opening its socket instead of starting an SSL handshake immediately which will cause the server to consider the SSL handshake to have failed and the connection will stall or die. The client will timeout and you will receive an SmtpException.

当 SmtpClient 对象连接到需要 SMTPS 的服务器时,这会出现问题。客户端将在打开其套接字后发送 EHLO,而不是立即开始 SSL 握手,这将导致服务器认为 SSL 握手失败并且连接将停止或死亡。客户端将超时,您将收到 SmtpException。

Any SMTP server listening on port 465 is probably expecting SMTPS. SMTPS has its own dedicated port and cannot optionally support STARTTLS because STARTTLS by definition requires the server to accept and respond to the opening EHLO and STARTTLS commands before using SSL.

任何侦听端口 465 的 SMTP 服务器都可能期待 SMTPS。SMTPS 有自己的专用端口,不能选择性地支持 STARTTLS,因为 STARTTLS 根据定义要求服务器在使用 SSL 之前接受并响应打开的 EHLO 和 STARTTLS 命令。

SMTP servers on traditional ports like 587 or 25 however have the option of supporting STARTTLS should the server software support it and service administrator choose to enable it. This is why many people on the Internet posting about this problem have noted that they are able to get the SmtpClient to work by changing the port from 465 to 587.

587 或 25 等传统端口上的 SMTP 服务器可以选择支持 STARTTLS,如果服务器软件支持它并且服务管理员选择启用它。这就是为什么许多在 Internet 上发帖讨论此问题的人指出,他们可以通过将端口从 465 更改为 587 来使 SmtpClient 工作。



The short version: The SmtpClient class supports STARTTLS and not SMTP over SSL (SMTPS). If you want to use the .EnableSsl flag on the client object, make sure the server and port you are connecting to supports STARTTLS. If the server and port are instead listening for and expecting true SMTPS, the client object will be unable to connect successfully and the send will fail. Port 465 is almost exclusively used for SMTPS. Ports 25 and 587 are used for regular SMTP or SMTP w/ STARTTLS support when STARTTLS is available (optional).

简短版本:SmtpClient 类支持 STARTTLS 而不是 SMTP over SSL (SMTPS)。如果要在客户端对象上使用 .EnableSsl 标志,请确保要连接的服务器和端口支持 STARTTLS。如果服务器和端口正在侦听并期待真正的 SMTPS,则客户端对象将无法成功连接并且发送将失败。端口 465 几乎专门用于 SMTPS。当 STARTTLS 可用(可选)时,端口 25 和 587 用于常规 SMTP 或带有 STARTTLS 支持的 SMTP。