SMTP email (gmail)
-
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
-
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
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.
-
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
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
-
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
-
Hassan, could you please share what number did you use for the port and what did you use for SMTPServer.EnableSsl!! Thanks, Aman
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) -
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.