开发者

system.net.mail does not send mail

开发者 https://www.devze.com 2023-02-16 07:56 出处:网络
i am developing with vb2005, using system.net.mail to send 开发者_StackOverflow社区mail using google configuration. a full sample of my code was posted here earlier

i am developing with vb2005, using system.net.mail to send 开发者_StackOverflow社区mail using google configuration. a full sample of my code was posted here earlier

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

i get the operation has timed out. if i change the port to 587, it says that the server does not support secure connection

[EDIT] could firewall be blocking it?

is there anyway to specify sending application name?


Try

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

If you get the timeout then troubleshoot

  1. Go to gmail.com and make sure you can loging with those credentials.
  2. Login to gmail account and go to settings and make sure POP/IMAP is setup. I don't think they allow you to send mail thru your account unless this is turned on.
  3. Try changing the port to 25 with SSL.
  4. Try to TELNET the mail server to see if you are able to send relay mails via your ISP

    Open command prompt and type (without quotes) telnet smtp.gmail.com 25

If you get a timeout then the problem is either your local LAN is blocking mails or your ISP.


Have you tried the answer at : Sending email through Gmail SMTP server with C#

Just to see if that works.

Note that in the answer to his solution he mentions web.config changes lower in the post not accepted as the answer

<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>


The port was right in the OP, but the host was wrong

    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

And just to verify, you have pop enabled in gmail, correct?


If you are using TWO STEP VERIFICATION for gmail, then disable it first and then try again with this code

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


The Port number is 587.

And also, try this order as per 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号