Trying To Send Email via .NET function - vb.net

So, I'm trying to send an email using the code below, and I get this error:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated.
I've already allowed unsecure applications to send me emails and turned off my 2-Step Verification on my gmail, and of course I replaced Net.NetworkCredential("myvalidemail#gmail.com", "mypassword") with my real email and password, but I still get that error.
I am running the program through VB debug, but that shouldn't matter.
Private Sub SendBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SendBtn.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New _
Net.NetworkCredential("myvalidemail#gmail.com", "mypassword")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("myvalidemail#gmail.com")
mail.To.Add("myvalidemail#gmail.com")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
mail.Priority = MailPriority.High
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

The exception says that SMTP server requires a secure connection. Try enabling the SSL
SmtpServer.EnableSsl = true;

"client was not authenticated"
Just a thought: do you use Google Two Factor Authorization?

Related

Smtp error while send mail with gmail setting

I am using Gmail setting for sending mail, i got an error like "Failure sending mail." and InnerException as "Unable to read data from the transport connection: net_io_connectionclosed."
My project frame work is .Net 4.8 ,
Programming Language vb ,
OS is Windows 11 .
Anything need to change in Gmail account setting ?
What I have tried:
Imports System.Net
Imports System.Net.Mail
Public Class Form1
Public Shared Function ServerCertificateValidationCallback(ByVal sender As Object, ByVal cert As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean
Return True
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault Or SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
Dim Mail As New System.Net.Mail.MailMessage
Dim smtp As New System.Net.Mail.SmtpClient
Mail.From = New MailAddress("aravind#gmail.com", "Aravind")
Mail.To.Add("aravind1#gmail.com")
Mail.IsBodyHtml = True
Mail.Subject = "Hai Testing Mail"
smtp = New SmtpClient("smtp.gmail.com", "587")
smtp.EnableSsl = True
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
smtp.UseDefaultCredentials = False
smtp.Credentials = New System.Net.NetworkCredential("aravind#gmail.com", "xxxxxxxxxxx")
Dim userstate As Object = Mail
System.Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf ServerCertificateValidationCallback
smtp.Send(Mail)
Catch ex As Exception
MessageBox.Show(ex.InnerException.ToString)
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
End Class
Note i tried with 465 and 25 as ports,still same problem.
1.Port 465 - error as "Failure sending mail." and InnerException as "Unable to read data from the transport connection: net_io_connectionclosed."
2.Port 25 - error as "Failure sending mail." and InnerException as "Unable to connect to the remote server"
And one more thing i tried with another gmail account with enabled 2 step versification and with app password working fine, i mean from my another account can send mail.
try to use port 465 port or 25 port instead of 587,hope this will solve your issue if further details you want then please refer to this link
https://kinsta.com/blog/gmail-smtp-server/

Send email with gmail smtp vb.net

i want to send an email with my gmail account but i have this error and the email doas'nt sent !
http://up.dev-point.com/uploads1/a75d55e308e91.png
My code is :
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Dim SmtpServer As New SmtpClient("smtp.gmail.com", 587)
SmtpServer.Credentials = New Net.NetworkCredential("ihab#gmail.com", "pass")
Dim mail As New MailMessage("ihab#gmail.com", "ihab#gmail.com", "New", "txt")
SmtpServer.Send(mail)
End Sub
Please see this case, I think you need to make sure SSL is enabled and possibly modify account settings for smtp access.
Sending email through Gmail SMTP server with C#
If you need assistance converting the C# code to vb.net let me know

Contact form is not sending email to my gmail acount

I have created a simple form using Visual Studio 2012 but it's not sending email to my gmail account, the page runs fine but when I hit send button I get error:
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. j8sm1567623paz.30
Description: An unhanded exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Mail.SmtpException: 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. j8sm1567623paz.30
Source Error:
Line 14:
Line 15:
Line 16: mailClient.Send(message)
Line 17:
Line 18:
Source File: C:\Website SVN II\test\contact.aspx.vb Line: 16
Sources:
Imports System.Net.Mail
Partial Class contact
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
sendMail(txtEmail.Text, txtMessage.Text)
End Sub
Protected Sub sendMail(ByVal From As String, ByVal body As String)
Dim mailservername As String = "smtp.gmail.com"
Dim message As MailMessage = New MailMessage(From, "nabeel.f#gmail.com", "feedback", body)
Dim mailClient As SmtpClient = New SmtpClient
mailClient.Host = mailservername
mailClient.Send(message)
message.Dispose()
End Sub
End Class
HTML.
first name
<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
<br />
<br />
last name
<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
<br />
<br />
email
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<br />
<br />
message:
<asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Send" />
Two things:
You need to use HTTPS.
You need to provide credentials for your account.
Here's a C# example from here:
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
vb.net version:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential
("xyz#gmail.com", "password")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
mail = New MailMessage()
Dim addr() As String = TextBox1.Text.Split(",")
Try
mail.From = New MailAddress("xyz#gmail.com",
"Web Developers", System.Text.Encoding.UTF8)
Dim i As Byte
For i = 0 To addr.Length - 1
mail.To.Add(addr(i))
Next
mail.Subject = TextBox3.Text
mail.Body = TextBox4.Text
If ListBox1.Items.Count <> 0 Then
For i = 0 To ListBox1.Items.Count - 1
mail.Attachments.Add(New Attachment
(ListBox1.Items.Item(i)))
Next
End If
mail.DeliveryNotificationOptions =
DeliveryNotificationOptions.OnFailure
mail.ReplyTo = New MailAddress(TextBox1.Text)
SmtpServer.Send(mail)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Send multiple attachments in a folder, not just a specific filename...via email

I have managed to send an email with an attachment that is in a specific location and named specifically ("C:\New\Log.txt")
However, I want to be able to send an email with all the attachments in a given folder no mater what they are called.
All variable settings are configured elsewhere in the project using my.settings and i would like similar for the folder destination i.e. my.settings.fileloc1 for the location of the files
Below is my current code. I'm pretty sure it will involve getfiles, but am running on empty....please help!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential(My.Settings.SMTPuser, My.Settings.SMTPuser)
SmtpServer.Port = My.Settings.SMTPPort
SmtpServer.Host = My.Settings.SMTPHost
mail = New MailMessage()
mail.From = New MailAddress(My.Settings.from)
mail.To.Add(My.Settings.recipient)
mail.Subject = My.Settings.subject
mail.Body = My.Settings.body
Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment("C:\New\Log.txt")
'^^The above needs to be an actual file
'^^I want it to select all files in a given folder and attach them!
mail.Attachments.Add(Attach)
SmtpServer.Send(mail)
MsgBox("Mail Sent")
Catch ex As Exception
MsgBox("Email Settings are either incomplete or incorrect" & vbNewLine & "Please see below details:" & vbNewLine & vbNewLine & ex.ToString)
End Try
End Sub
Thanks for anything you can come up with :)
Try a For Each loop looking for all files in System.IO.Directory.GetFiles()
' ...
For Each filePath As String In Directory.GetFiles(My.Settings.FileLoc1)
Dim Attach As New Net.Mail.Attachment(filePath)
mail.Attachments.Add(Attach)
Next
SmtpServer.Send(mail)
' ...

System.Net.Mail.SmtpException The SMTP server requires a secure connection or the client was not authenticated

I am trying to send SMTP email from my vb.net form application. When applying this code, I get the error below. What am I doing wrong?
Code:
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("myemail#gmail.com", "mypassword")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("myemail#gmail.com")
mail.To.Add("sendto#hotmail.co.uk")
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
End Class
Error:
System.Net.Mail.SmtpException: 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
Check if the login and password are correct;
Use SmtpServer.EnableSsl = true;
Gmail has disabled access using credentials (user and password) by default, you need go to this page: https://www.google.com/settings/security/lesssecureapps and "Enable Less Secure Apps", this means: "Enable login by apps using user and password". More details here: https://support.google.com/accounts/answer/6010255
See if this helps; Add
SmtpServer.EnableSSL= true
I know this was last year but I thought I should publish the answer as five minutes ago I had the same issue.
Basically your log in credentials are incorrect and need changing.
Also thanks for the previous answer has that piece of code allows me to send emails over SSL encryption (I Hope LOL).