I have tried different port numbers but I keep getting the error message and no email sent. I aim to be able to send the email successfully and the message will contain details from an appointment booked. Any help please?
Dim UserName As String = "example#gmail.com"
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress(UserName)
mail.To.Add(New MailAddress(txtEmailAddress.Text))
mail.Subject = "Appointment Details"
mail.Body = "Test message"
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 465)
client.EnableSsl = True
client.Credentials = New System.Net.NetworkCredential(UserName, "***")
Try
client.Send(mail)
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
End Try
What you need to do is add client.UseDefaultCredentials = False, but make sure you set it to false before calling client.Credentials = New System.Net.NetworkCredential(UserName, "***")
Also, for GMail, you need to use port 587 which is the port that supports the STARTTLS extension (which is the only way that System.Net.Mail.SmtpClient supports SSL).
In other words, change your code to this:
Dim UserName As String = "example#gmail.com"
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress(UserName)
mail.To.Add(New MailAddress(txtEmailAddress.Text))
mail.Subject = "Appointment Details"
mail.Body = "Test message"
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
client.EnableSsl = True
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential(UserName, "***")
Try
client.Send(mail)
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
End Try
Try this,
go to your gmail account (www.gmail.com)
login, and check that you have "enabled Pop for all mail".
you can find this under the tab "Forwarding and POP/IMAP"
Also try switching the port number to 587
Related
I want to send an email when user click btnSubmit. My problem is when I click the btnSubmit it gives me error at Catch ex As Exeption. I'm using Web Form.aspx.vb
Here is my code
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress("example#gmail.com")
mail.To.Add(New MailAddress("example#gmail.com"))
mail.Subject = "TEST SUBJECT"
mail.Body = "Test Message"
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
client.EnableSsl = True
Try
client.Send(mail)
MsgBox("Sending Email succeeded") 'Return True if there is nothing error while sending email.
Catch ex As Exception
MsgBox("Sending Email failed. Please try again")
End Try
I wanna send an Email from my Webservice, written in vb.net, but I get an Timeout.
What is wrong?
Dim toaddress As MailAddress = New MailAddress("xxx")
Dim fromaddress As MailAddress = New MailAddress("yyy")
' The structure for MailMessage(from, to)
Dim message As MailMessage = New MailMessage(fromaddress, toaddress)
message.Subject = "I have sent you a message from a program!"
message.Body = "Hello World!"
Dim messanger As SmtpClient = New SmtpClient("smtpxxx", 995)
messanger.Credentials = New NetworkCredential("user", "password")
messanger.EnableSsl = True
messanger.Send(message)
You are doing many things wrong and some of the information you have given are not clear enough. host name and port names have to give properly. it chooses which mail service you are using, from your code it is not correctly provided, so i will give a snippet that uses gmail to send mail. please go through this and make changes as per your SmtpHost.
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("email", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtemail.Text)
e_mail.To.Add(txtemail.Text)
e_mail.Subject = "Email Sending"
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
This link help you to find the smtp.Server and Port numbers, you can use this thread for check the limitations of smtp.servers
When I send an email
By pressing the button
No error will not be
Dim mm As MailMessage = New MailMessage("ashkanramedani1370#gmail.com", "ashkanramedani1370#gmail.com", "HELLO", "test")
Dim smtp As New SmtpClient("gmail.com")
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New System.Net.NetworkCredential()
NetworkCred.UserName = "ashkanramedani1370#gmail.com"
NetworkCred.Password = "123456789#"
smtp.UseDefaultCredentials = False
smtp.Credentials = NetworkCred
smtp.Port = 587
Try
smtp.Send(mm)
Catch ex As Exception
'----
End Try
But did not receive the email
Please help me
gmail.com is not an SMTP server.
You want smtp.gmail.com.
Here's my code (yes I censored my email/password) (when you click button)
Dim Mail As New MailMessage
Mail.Subject = "test email"
Mail.To.Add("*****")
Mail.From = New MailAddress("*****") '
Mail.Body = "This is an email!"
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("*****", "*****")
SMTP.Port = 587
SMTP.Send(Mail)
MsgBox("Sent Successfully")
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at (link which doesn't help)
SOLVED!
I never would have guessed.
Google wasn't letting me use my account because it was hacked. It apparently was hacked when I was writing code...
FYI, a safer version would be
Using mail As MailMessage = New MailMessage
mail.Subject = "test email"
mail.To.Add("*****")
mail.From = New MailAddress("*****") '
mail.Body = "This is an email!"
Using smtp As New SmtpClient("smtp.gmail.com")
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("*****", "*****")
smtp.Port = 587
smtp.Send(mail)
End Using
End Using
MsgBox("Sent Successfully")
This would ensure that the mail and smtp objects get cleaned up, whether or not an exception occurs.
This is my code to send email using GMAIL and VB.NET:
Try
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("mygmailid#gmail.com", "mypassword")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress("mygmailid#gmail.com", "Asked for help", System.Text.Encoding.UTF8)
Mail.To.Add("sendtoemail#id")
Mail.Subject = "A help query has been raised"
mail.Body = frm_dashboard.user_data_fetch.Item(1, 0).Value.ToString + " " + ask_for_help.txt_message_ask_help.Text
SmtpServer.Send(Mail)
MessageBox.Show("Mail sent")
Catch ex As Exception
MsgBox(ex.ToString())
MessageBox.Show("Oops something went wrong.")
End Try
But this shows me this error:-
This is the order I have it in, you might want to try this:
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.Credentials = New System.Net.NetworkCredential("gmailID", "password")
SMTP.EnableSsl = True
SMTP.Port = "587"
SMTP.Send(Mail)
Also note that I don't actually use the "#gmail.com" when passing in my credentials.