Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. SMTP email (gmail)

SMTP email (gmail)

Scheduled Pinned Locked Moved Visual Basic
csharpcomsysadmin
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amanjot
    wrote on last edited by
    #1

    Hi, I am trying to send an email for my gmail account using SMTP code in VB.net; however, the email is not getting sent. I am using the code shown below. Kindly suggest what am I doing wrong. Thanks. Aman

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        'Start by creating a mail message object
        Dim MyMailMessage As New MailMessage()
    
        'From requires an instance of the MailAddress type
        MyMailMessage.From = New MailAddress("abc@gmail.com")
    
        'To is a collection of MailAddress types
        MyMailMessage.To.Add("abc@yahoo.com")
    
        MyMailMessage.Subject = "GMail Test"
        MyMailMessage.Body = "This is the test text for Gmail email"
    
        'Create the SMTPClient object and specify the SMTP GMail server
        Dim SMTPServer As New SmtpClient("smtp.gmail.com")
        SMTPServer.Port = 465
        SMTPServer.Credentials = New System.Net.NetworkCredential("abc", "cba")
        SMTPServer.EnableSsl = True
    
        Try
            SMTPServer.Send(MyMailMessage)
            MessageBox.Show("Email Sent")
        Catch ex As SmtpException
            MessageBox.Show(ex.Message)
        End Try
    
    End Sub
    
    C H 2 Replies Last reply
    0
    • A Amanjot

      Hi, I am trying to send an email for my gmail account using SMTP code in VB.net; however, the email is not getting sent. I am using the code shown below. Kindly suggest what am I doing wrong. Thanks. Aman

      Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

          'Start by creating a mail message object
          Dim MyMailMessage As New MailMessage()
      
          'From requires an instance of the MailAddress type
          MyMailMessage.From = New MailAddress("abc@gmail.com")
      
          'To is a collection of MailAddress types
          MyMailMessage.To.Add("abc@yahoo.com")
      
          MyMailMessage.Subject = "GMail Test"
          MyMailMessage.Body = "This is the test text for Gmail email"
      
          'Create the SMTPClient object and specify the SMTP GMail server
          Dim SMTPServer As New SmtpClient("smtp.gmail.com")
          SMTPServer.Port = 465
          SMTPServer.Credentials = New System.Net.NetworkCredential("abc", "cba")
          SMTPServer.EnableSsl = True
      
          Try
              SMTPServer.Send(MyMailMessage)
              MessageBox.Show("Email Sent")
          Catch ex As SmtpException
              MessageBox.Show(ex.Message)
          End Try
      
      End Sub
      
      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Looks reasonable to me. I'd assume the issue is something to do with gmail, and not your code, assuming your settings are all correct.

      Christian Graus Driven to the arms of OSX by Vista.

      I 1 Reply Last reply
      0
      • A Amanjot

        Hi, I am trying to send an email for my gmail account using SMTP code in VB.net; however, the email is not getting sent. I am using the code shown below. Kindly suggest what am I doing wrong. Thanks. Aman

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

            'Start by creating a mail message object
            Dim MyMailMessage As New MailMessage()
        
            'From requires an instance of the MailAddress type
            MyMailMessage.From = New MailAddress("abc@gmail.com")
        
            'To is a collection of MailAddress types
            MyMailMessage.To.Add("abc@yahoo.com")
        
            MyMailMessage.Subject = "GMail Test"
            MyMailMessage.Body = "This is the test text for Gmail email"
        
            'Create the SMTPClient object and specify the SMTP GMail server
            Dim SMTPServer As New SmtpClient("smtp.gmail.com")
            SMTPServer.Port = 465
            SMTPServer.Credentials = New System.Net.NetworkCredential("abc", "cba")
            SMTPServer.EnableSsl = True
        
            Try
                SMTPServer.Send(MyMailMessage)
                MessageBox.Show("Email Sent")
            Catch ex As SmtpException
                MessageBox.Show(ex.Message)
            End Try
        
        End Sub
        
        H Offline
        H Offline
        Hassan Eido
        wrote on last edited by
        #3

        I've just tested your code on my smtp account and it works perfectly. I only removed the following cause they don't apply in my case: ' SMTPServer.Port = 465 ' SMTPServer.Credentials = New System.Net.NetworkCredential("abc", "cba") ' SMTPServer.EnableSsl = True

        A 1 Reply Last reply
        0
        • H Hassan Eido

          I've just tested your code on my smtp account and it works perfectly. I only removed the following cause they don't apply in my case: ' SMTPServer.Port = 465 ' SMTPServer.Credentials = New System.Net.NetworkCredential("abc", "cba") ' SMTPServer.EnableSsl = True

          A Offline
          A Offline
          Amanjot
          wrote on last edited by
          #4

          Hassan, could you please share what number did you use for the port and what did you use for SMTPServer.EnableSsl!! Thanks, Aman

          B 1 Reply Last reply
          0
          • A Amanjot

            Hassan, could you please share what number did you use for the port and what did you use for SMTPServer.EnableSsl!! Thanks, Aman

            B Offline
            B Offline
            Bharat Jain
            wrote on last edited by
            #5

            The port number for Gmail's SMTP is 587 , i think that is the problem following is the code i am using in my application and it works

            Dim smtpClient As New Net.Mail.SmtpClient()
            Dim mail As New Net.Mail.MailMessage()
            'create the message to be sent
            mail.To.Add("test@test.com") ' Enter the Email of the person you want to send the mail to
            mail.From = New Net.Mail.MailAddress("username@gmail.com", "Your Display Name") ' Enter Your email address
            mail.Subject = "Test Message"
            mail.Body = "This is a test message"
            ' Prepare the client to send the above message
            smtpClient.Host = "smtp.gmail.com"
            smtpClient.EnableSsl = True
            smtpClient.Port = 587
            smtpClient.Credentials = New Net.NetworkCredential("username@gmail.com", "password") 'Enter username and password of the account , you want to use to send mail
            smtpClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
            smtpClient.Send(mail)

            1 Reply Last reply
            0
            • C Christian Graus

              Looks reasonable to me. I'd assume the issue is something to do with gmail, and not your code, assuming your settings are all correct.

              Christian Graus Driven to the arms of OSX by Vista.

              I Offline
              I Offline
              i i i
              wrote on last edited by
              #6

              Is their anyway to Send Email in Exchange 2007 without using SMTP because client doesnot want to enable it

              Best Regards, SOFTDEV If you have knowledge, let others light their candles at it

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups