Contact form is not sending email to my gmail acount - vb.net

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

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/

Trying To Send Email via .NET function

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?

Formatting Dataset results in email

I have a program that on load will load the dataset with info from a query. I then send the information in an email. Everything is working correctly except the output. The output is wrapped and not in table format. If I debug the program and paste the "Value" of "Payouts" into a txt document and save it as an html file it is formatted correctly (Shows in table form). Here is my Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Me.Paid_Out_TbTableAdapter.Fill(Me.DataDeliveryServiceDataSet.Paid_Out_Tb)
Dim payouts = _
<html>
<body>
<table>
<tr><th>My First Column Header</th><th>My Second Column Header</th></tr>
<%= From paidOut In Me.DataDeliveryServiceDataSet.Paid_Out_Tb.AsEnumerable _
Select <tr><td><%= paidOut.Store_Id %></td><td><%= paidOut.Paid_Out_Comment %></td></tr> %>
</table>
</body>
</html>
SmtpServer.Credentials = New _
Net.NetworkCredential("****", "****")
SmtpServer.Port = 25
SmtpServer.Host = "*****"
mail = New MailMessage()
mail.From = New MailAddress("*#*.com")
mail.To.Add("*#*.com")
mail.Subject = "Test Mail"
mail.IsBodyHtml = True
mail.Body = payouts
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Here what is sent in an email as the body:
My First Column HeaderMy Second Column Header4567OFFICE SUPPLIES4567REIMBUSEMENT FOR SERVICE PER ROB PER INCIDENT REPORT4567REFUND FOR SERVICE INVOICE# **4567OFFICE SUPPLIES AND GATORADE
As you can see it's just wrapped and not formatted... How can I get the body to be either in TXT format as a table, or in HTML Format as a table.
The CStr(XElement) function just returns the content of the element. Use:
mail.body=payouts.ToString()

How to put Dataset query results into the body of an email

I'm trying to make a program that emails the results of a query (in a dataset) to a user... My code is:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Paid_Out_TbTableAdapter.Fill(Me.dataset.Paid_Out_Tb)
Me.ReportViewer1.RefreshReport()
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("Bob", "password")
SmtpServer.Port = 25
SmtpServer.Host = "server"
mail = New MailMessage()
mail.From = New MailAddress("email#email.com")
mail.To.Add("Email#email.com")
mail.Subject = "Test Mail"
mail.Body = (Me.DataSet.Paid_Out_Tb.ToString)
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Everything works except sending the email body... How can i get the results to email as the body?
You can use Linq and VB's new inline xml literals capability to generate the html. Try this:
Dim payOuts = _
<html>
<body>
<table>
<tr><th>My First Column Header</th><th>My Second Column Header</th></tr>
<%= From paidOut In Me.DataDeliveryServiceDataSet.Paid_Out_Tb.AsEnumerable _
Select <tr>
<td><%= paidOut.MyFirstColum %></td>
<td><%= paidOut.MySecondColum %></td>
</tr> %>
</table>
</body>
</html>
mail.IsBodyHtml = True
mail.Body = payouts.ToString
Be sure to include a reference to System.Data.DataSetExtensions.dll in your project if it isn't already there. You'll also need Imports System.Linq on your class. See the Using LINQ and XML Literals to transform a DataTable into a HTML table blog posting by Éric Moreau for a good explanation.
You will need to cycle through the rows of the dataset and extract the data you wish to email. You can format it using HTML by adding the following statement:
mail.IsBodyHtml = True

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).