0

i am developing with vb2005, using system.net.mail to send 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?

Community
  • 1
  • 1
Smith
  • 5,765
  • 17
  • 102
  • 161

5 Answers5

2

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>
Community
  • 1
  • 1
sclarson
  • 4,362
  • 3
  • 32
  • 44
2

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.

Saif Khan
  • 18,402
  • 29
  • 102
  • 147
  • I can telnet to google at 587, but when i use my app, it says the operation has timedout – Smith Mar 13 '11 at 18:30
  • Then the problem must be in the GMail account setup. Try using an email client like Outlook and setup an account with the same credentials and see if you are able to send emails. – Saif Khan Mar 15 '11 at 03:15
  • actually, i just used another smtp server, and it worked. i don't know what is wrong with google – Smith Mar 16 '11 at 14:02
  • Ha! Then your gmail account is not properly configured for access by another client (e.g. POP3) OR your "FROM" address does not match the username so the connection is dropped. – Saif Khan Mar 16 '11 at 19:12
1

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?

dbasnett
  • 11,334
  • 2
  • 25
  • 33
0

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
Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
-1

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
Community
  • 1
  • 1
naveen
  • 53,448
  • 46
  • 161
  • 251