system.net.mail不发送邮件

时间:2011-03-13 13:48:20

标签: vb.net smtpclient

我正在使用vb2005进行开发,使用system.net.mail使用google配置发送邮件。我的代码的完整示例是posted here之前的

        With smtp
            .Host = "smtp.google.com"
            .Port = 465
            .UseDefaultCredentials = False
            .Credentials = New System.Net.NetworkCredential(mygoogleemail, mypass)
            .EnableSsl = true
            .Timeout = 60000
        End With

我得到的操作已超时。如果我将端口更改为587,则表示the server does not support secure connection

[编辑] 防火墙会阻止它吗?

无论如何

指定发送应用程序名称?

5 个答案:

答案 0 :(得分:2)

您是否尝试过:Sending email through Gmail SMTP server with C#

看看是否有效。

请注意,在他的解决方案的答案中,他提到web.config更改在帖子中更低,不接受作为答案

<system.net>
  <mailSettings>
    <smtp from="myusername@gmail.com" deliveryMethod="Network">
      <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" password="password" userName="myusername@gmail.com"/>
    </smtp>
  </mailSettings>  
</system.net>

答案 1 :(得分:2)

尝试

Dim myLogin As New NetworkCredential("username@gmail.com", "password")
Dim msg As New MailMessage("username@gmail.com", "to@somedomain.com", "subjectLine", "emailBodyText")

msg.IsBodyHtml = True

Try
    Dim client As New SmtpClient("smtp.gmail.com", 587)
    client.EnableSsl = True
    client.UseDefaultCredentials = false
    client.Credentials = myLogin
    client.Send(msg)
Catch ex As SmtpException
    msgbox(ex.Message)
End Try

如果您收到超时,则排除故障

  1. 转到gmail.com并确保您可以使用这些凭据进行登录。
  2. 登录Gmail帐户并转到设置并确保已设置POP / IMAP。除非启用此功能,否则我认为他们不允许您通过帐户发送邮件。
  3. 尝试使用SSL将端口更改为25。
  4. 尝试TELNET邮件服务器以查看您是否能够通过ISP发送中继邮件

    打开命令提示符并键入(不带引号)telnet smtp.gmail.com 25

  5. 如果超时,则问题是本地局域网阻止邮件或您的ISP。

答案 2 :(得分:1)

端口在OP中是正确的,但主机是错误的

    With smtp
        .Port = 465 '465 for ssl
        .EnableSsl = True
        .UseDefaultCredentials = False
        .Credentials = New System.Net.NetworkCredential("myusername@gmail.com", "yourpassword")
        .Host = "smtp.gmail.com"
    End With

只是为了验证,你在gmail中启用了pop,对吗?

答案 3 :(得分:0)

如果您对gmail使用两步验证,请先禁用它,然后再使用此代码重试

Dim myLogin As New NetworkCredential("username@gmail.com", "password")
Dim msg As New MailMessage("username@gmail.com", "to@somedomain.com", "subjectLine", "emailBodyText")

msg.IsBodyHtml = True

Try
    Dim client As New SmtpClient("smtp.gmail.com", 587)
    client.EnableSsl = True
    client.UseDefaultCredentials = false
    client.Credentials = myLogin
    client.Send(msg)
Catch ex As SmtpException
    msgbox(ex.Message)
End Try

答案 4 :(得分:-1)

端口号是587。

另外,请按照this solution

尝试此订单
    With smtp
        .UseDefaultCredentials = False
        .Credentials = New System.Net.NetworkCredential("myusername@gmail.com", "mypwd")
        .Host = "smtp.gmail.com"
        .Port = 587
        .EnableSsl = true
    End With