I got a error while sending mail using vb.net - vb.net

the error is The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. t77sm26908892pfg.102 - gsmtp
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.Credentials = New _
Net.NetworkCredential("user#gmail.com", "user1")
mail = New MailMessage()
mail.From = New MailAddress("user#gmail.com")
mail.To.Add("user2#gmail.com")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
SmtpServer.Send(mail)
MsgBox("mail send")

As Codexer said in his comment, you are not enabling SSL by setting SmtpServer.EnableSsl = True
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Port = 587
SmtpServer.EnableSsl = True ' <---- this
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.Credentials = New _
Net.NetworkCredential("user#gmail.com", "user1")
mail = New MailMessage()
mail.From = New MailAddress("user#gmail.com")
mail.To.Add("user2#gmail.com")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
SmtpServer.Send(mail)
MsgBox("mail send")
It always a good idea to not send your credentials as plain text over the internet...

Related

How to send a file from a shared network via E-mail in VB.NET?

I'm creating a WinService to send E-mails and now I am trying to send a file via e-mail from a shared network.
When I do the debugging I can send and receive the attachment successfully, but when I run the WinService it says that Access to the path '\\SharedPath\shared_file.txt' is denied.
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential("user", "password")
SmtpServer.Host = "smtp.example.com"
SmtpServer.Port = 587
SmtpServer.EnableSsl = True
mail.From = New MailAddress("src_address#example.com")
mail.To.Add("dst_address#example.com")
mail.Subject = "E-Mail subject"
mail.Body = "Email body text."
Dim attachment As New Attachment("\\SharedPath\shared_file.txt")
mail.Attachments.Add(attachment)
SmtpServer.Send(mail)
Any suggestion?

Sending Email VB.net Exception

this is my code it was running without errors but suddenly
an exception appears
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Dim smtpserver As New SmtpClient()
Dim mail As New MailMessage()
smtpserver.Credentials = New Net.NetworkCredential("myemail#gmail.com", "mypass")
smtpserver.Host = "smtp.gmail.com"
smtpserver.Port = "587"
mail = New MailMessage
mail.From = New MailAddress("myHouseHQ#gmail.com")
mail.To.Add(Form2.TextBox1.Text)
mail.Subject = "EagleEyes"
mail.Body = "EagleEyes has detected a movement!"
If emailphoto Then
Dim attach As New Attachment("D:\hi" & sm & ".jpg")
mail.Attachments.Add(attach)
End If
smtpserver.EnableSsl = True
smtpserver.UseDefaultCredentials = False
' Try1
smtpserver.Send(mail)
'Catch ex As SmtpException
'MsgBox("Error Connection!" & ex.Message)
'End Try
sm += 1
Don't use "" by the port.
Please try this for the port:
smtpserver.Port = 587

SMTP BCC feature in VB.net

I am looking to add BCC mail feature in the following code. How can I make changes in the following code to send an auto email to more than one email's
Dim SMTPServer As New SmtpClient()
Dim Mail As New MailMessage()
SMTPServer.Credentials = New Net.NetworkCredential(" ")
SMTPServer.Port = 587
SMTPServer.Host = " "
Mail = New MailMessage
Mail.From = New MailAddress(" ")
Mail.To.Add(dr("").ToString)
Mail.Subject = "Patient Assignment"
Mail.Body = " "
SMTPServer.Send(Mail)

SMTP.Send(Mail) Error

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.

unable to send email from VB.net

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.