none
Send e-mail with GMail does not work RRS feed

  • Question

  • Hello!

    I have researched the web on how to send e-mails via gmail through vb.net or asp.net code. Although I tried to follow the examples there it still does not work. What am I missing? Here is my code:

    Public Sub SendMail(ToAddress As String, Subject As String, Body As String)
    
    		Dim tmpMail As New MailMessage
    		Dim tmpSMTP As New SmtpClient(_SMTPAddress)  '_SMTPAddress = "smtp.gmail.com"
    
    		With tmpMail
    
    			.From = New MailAddress(_From)  '_From = GMail user name and e-mail address
    			.To.Add(ToAddress)
    			.Subject = Subject
    			.Body = Body
    
    		End With
    
    		With tmpSMTP
    
    			.Credentials = New System.Net.NetworkCredential(_From, _Password)  'GMail user name and e-mail address, password
    			.Port = _SMTPPort  'SMTPPort = 587
    			.EnableSsl = True
    
    		End With
    
    		Try
    
    			tmpSMTP.Send(tmpMail)
    
    		Catch
    
    			MsgBox("eMail cannot be sent.")
    
    		End Try
    
    	End Sub

    Thank you in advance!

    Christian

    Wednesday, May 10, 2017 6:44 AM

Answers

  • Chris,

    Beside what ashidacchi wrote, take care that your viruschecker/firewall is not blocking your program from access outside.

    Also know that providers like GMail block accounts if they are used as kind of spam servers (much traffic)


    Success
    Cor



    • Edited by Cor Ligthert Wednesday, May 10, 2017 12:27 PM
    • Marked as answer by ChrisP1980 Thursday, May 11, 2017 7:33 AM
    Wednesday, May 10, 2017 12:24 PM

All replies

  • Hi Christian,

    port number of smtp.gmai.com is 465, instead of 587
    Try it.
    ___________
    Ashidacchi
    Wednesday, May 10, 2017 7:43 AM
  • Chris,

    Beside what ashidacchi wrote, take care that your viruschecker/firewall is not blocking your program from access outside.

    Also know that providers like GMail block accounts if they are used as kind of spam servers (much traffic)


    Success
    Cor



    • Edited by Cor Ligthert Wednesday, May 10, 2017 12:27 PM
    • Marked as answer by ChrisP1980 Thursday, May 11, 2017 7:33 AM
    Wednesday, May 10, 2017 12:24 PM
  • You may need to setup for less secure apps.

    https://support.google.com/accounts/answer/6010255?hl=en


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 10, 2017 12:45 PM
    Moderator
  • Ashidacchi: Thanks, already tried that. To no avail. :(

    Thursday, May 11, 2017 7:29 AM
  • Thank you Karen, great to know! Tried it but did not work. Probably a firewall problem.

    Thursday, May 11, 2017 7:34 AM
  • Hi ChrisP1980,

    Please try to the code below.

     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            Try
                Dim SmtpServer As New SmtpClient()
                Dim mail As New MailMessage()
                SmtpServer.Credentials = New _
            Net.NetworkCredential("username@gmail.com", "password")
                SmtpServer.Port = 587
                SmtpServer.Host = "smtp.gmail.com"
                mail = New MailMessage()
                mail.From = New MailAddress("YOURusername@gmail.com")
                mail.To.Add("TOADDRESS")
                mail.Subject = "Test Mail"
                mail.Body = "This is for testing SMTP mail from GMAIL"
                SmtpServer.Send(mail)
                MsgBox("mail send")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
    
        End Sub

    Hope it is helpful to you.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Binooplal Friday, October 12, 2018 11:00 AM
    • Unproposed as answer by Binooplal Friday, October 12, 2018 11:01 AM
    Thursday, May 11, 2017 8:23 AM
    Moderator
  • The issue is Google blocks access for non-google applications by default which prevents your application to send the email.

    Step 1:

    Sign in to your google account and visit this URL

    https://accounts.google.com/DisplayUnlockCaptcha

    Step 2:

    Go to Security and Turn off "less secure app access"

    Step 3:

    Here is the code to send email from VB.Net:

    ****************************************************************************

    Try
                Dim Smtp_Server As New SmtpClient

                Dim e_mail As New MailMessage()

                Dim NetworkCred As New System.Net.NetworkCredential()

                NetworkCred.UserName = "YourID@Gmail.com"

                NetworkCred.Password = "Your Password"


                Smtp_Server.UseDefaultCredentials = False

                Smtp_Server.Credentials = NetworkCred
                Smtp_Server.Port = 587
                Smtp_Server.EnableSsl = True

                Smtp_Server.Host = "smtp.gmail.com"

                e_mail = New MailMessage()

                e_mail.From = New MailAddress("Your Email")

                e_mail.To.Add("Recipient Email Address")

                e_mail.Subject = "This is a test email"

                e_mail.IsBodyHtml = True

                e_mail.Body = "Sending Test email. Please ignore."

                Smtp_Server.Send(e_mail)

                MsgBox("Mail Sent Successfully")

            Catch error_t As Exception

                MsgBox(error_t.ToString)

            End Try

    ****************************************************************************

    Friday, October 12, 2018 11:09 AM
  • @Binooplal

    Please keep your replies to within the past 30 days and don't un-propose answers. Also in regards to your code, a robust solution would also cover using SendCompleted event and integrate if the current operation was a success or failure where the failure is usually a better description then that thrown in the try/catch. To go farther, unit test should be written as I explain in the following article (it's C# but easy enough to get the idea of the benefits of testing).


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, October 12, 2018 11:21 AM
    Moderator
  • Excellent Binooplal !!!

    It worked perfectly

    This stuff was troubling me for quite some time...

    Thanks a lot !!!

    Monday, October 15, 2018 6:25 AM