ASP.net Contact Form Sending an email error - asp.net

i really need help on one thing. I have a website and in that website i have contact us page in which the users can contact the admin about the page or any other stuff.
The problem is when i try to render the page, it gives me an error saying
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
Description: An unhandled 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.5.1 Authentication Required. Learn more at
Source Error:
Line 21: smtp.Credentials = NetworkCred
Line 22: smtp.Port = 587
Line 23: smtp.Send(mm)
Line 24: lblMessage.Text = "Email Sent SucessFully."
Line 25: End Sub
Source File: C:\Users\user\Desktop\ContactUsForm\VB.aspx.vb Line: 23
I really don't know wt to do. My VB behind code is :
Imports System.Net
Imports System.Net.Mail
Partial Class VB
Inherits System.Web.UI.Page
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim mm As New MailMessage("sender#gmail.com", "reveiver#gmail.com")
mm.Subject = txtSubject.Text
mm.Body = "Name: " & txtName.Text & "<br /><br />Email: " & txtEmail.Text & "<br />" & txtBody.Text
If FileUpload1.HasFile Then
Dim FileName As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
mm.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileName))
End If
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As New System.Net.NetworkCredential()
NetworkCred.UserName = "sender#gmail.com"
NetworkCred.Password = "senderpassword"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
lblMessage.Text = "Email Sent SucessFully."
End Sub
End Class
The error is at line 23 where it is saying smtp.Send(mm)
I am really in a need. Please can anyone help me.

Change
smtp.UseDefaultCredentials = False
your don't want to use the default credentials, you want to use the credentials you created in your code.
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.usedefaultcredentials(v=vs.110).aspx

Related

Sending email from webform using SMTP server

I have a ASP.NET web form for sending emails using SMTP. The hosting provider is 1and1. It worked for year. It stopped working several weeks ago. I receive the following error:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"
1and1 support is unable to explain the reason for the change. I would appreciate your help (see the VB.NET code below).
I tried to change the port to 465 and 587, as well, I tried to change "EnableSsl" flag to true state - nothing worked.
I wrote another code that works, but it allows to send an email with my email address in the "from" field (see the code below). But I would like to send an email with visitors' address in the "from" field.
' code that worked once
Dim mailMessage As New MailMessage()
Dim MailAddress As String
Try
'creating an instance of the MailMessage class
mailMessage.From = New MailAddress(txtEmail.Text)
'senders email address
mailMessage.To.Add("me#mysite.net")
'recipient's email address
mailMessage.Subject = "Form Submission: " & txtSubject.Text
'subject of the email message
mailMessage.IsBodyHtml = False
'message text format. Can be text or html
mailMessage.Body = txtName.Text & vbCr & txtEmail.Text
'message body
mailMessage.Priority = MailPriority.Normal
'email priority. Can be low, normal or high
Dim smtp As New SmtpClient("smtp.ionos.com")
smtp.Port = 25
smtp.EnableSsl = False
smtp.Send(mailMessage)
'using the static method "Send" of the SmtpMail class to send the mail
Response.Write("Mail sent")
Catch ex As Exception
lblError.Text = "Form processing error occurred." & vbCr & "Please send your request to: me#mysite.net"
End Try
' code that is working today, but my email address must be in the "from" field
Dim mailMessage As New MailMessage()
Dim MailAddress As String
Try
'creating an instance of the MailMessage class
mailMessage = New MailMessage()
mailMessage.From = New MailAddress("me#mysite.net")
'senders email address
mailMessage.To.Add("me#mysite.net")
'recipient's email address
mailMessage.Subject = "Form Submission: " & txtSubject.Text
'subject of the email message
mailMessage.IsBodyHtml = False
'message text format. Can be text or html
mailMessage.Body = txtName.Text & vbCr & txtEmail.Text
'message body
mailMessage.Priority = MailPriority.Normal
'email priority. Can be low, normal or high
Dim smtp As New SmtpClient'("smtp.ionos.com")
smtp.UseDefaultCredentials = False
smtp.Credentials = New Net.NetworkCredential("me#mysite.net", "MyPassword")
smtp.Host = "smtp.ionos.com"
smtp.Port = 587
smtp.EnableSsl = True
smtp.Send(mailMessage)
'using the static method "Send" of the SmtpMail class to send the mail
Response.Write("Mail sent")
Catch ex As Exception
lblError.Text = "Form processing error occurred." & vbCr & "Please send your request to: me#mysite.net"
End Try

Unable to send email with IIS7

I'm migrating a website from IIS6 to IIS7. I have everything working but the site will not send emails.
I have confirmed that the server is handling emails correctly, because when I drop a text document into the pickup folder it is delivered, but when I try to send a message via code I either get an error or just nothing.
The SMTP virtual server is setup with standard settings.
The website in question is using 4.0 Framework, Integrated Application pool, I've tried both the ApplicationPoolIdentity, and the NetworkService with no effect. For the site's SMTP module I've tried an SMTP server of 127.0.0.1, localhost and the server's domain name, all on port 25.
For testing I've been using VBS and classic asp to send an email as well as asp.net.
I've been trying very basic scripts copied off the internet where all the comments say that it works. For example
vb.net. This code shows no error, but no message is sent and I've found nothing in the event viewer
Public Shared errorEmailTo As String = System.Configuration.ConfigurationManager.AppSettings("errorEmailTo")
Public Shared errorEmailFrom As String = System.Configuration.ConfigurationManager.AppSettings("errorEmailFrom")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim email As String = "This is a test email."
show.Text = "To: " + System.Configuration.ConfigurationManager.AppSettings("errorEmailTo") + "<br/>"
show.Text += "From: " + System.Configuration.ConfigurationManager.AppSettings("errorEmailFrom") + "<br/>"
Try
Helper.SendError(email)
show.Text += "No Error"
Catch ex As Exception
show.Text += "Error: " + ex.Message
End Try
End Sub
Public Shared Sub SendError(ByVal strBody As String)
Dim Email As New System.Net.Mail.MailMessage(errorEmailFrom, errorEmailTo)
Email.Subject = "Error Message"
Email.Body = strBody
Dim mailClient As New System.Net.Mail.SmtpClient()
mailClient.UseDefaultCredentials = True
mailClient.EnableSsl = False
Try
mailClient.Send(Email)
Catch ex As Exception
End Try
End Sub
VBS. This code returns the error "The transport failed to connect to the server." For which I've not found anything other than a new nearly identical code block
strSMTPFrom = "donotreply#here.com"
strSMTPTo = "me#here.com"
strSMTPRelay = "localhost"
strTextBody = "Body of your email"
strSubject = "Subject line"
Set oMessage = CreateObject("CDO.Message")
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update
oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.TextBody = strTextBody
oMessage.Send
Classic ASP gives the same error message.
<%
Dim sch, cdoConfig, cdoMessage
sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(sch & "sendusing") = 2 ' cdoSendUsingPort
.Item(sch & "smtpserver") = "localhost"
.Item(sch & "smtpserverport") = 25
.update
End With
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
.From = "donotreply#here.com"
.To = "me#here.com"
.Subject = "Email test"
.TextBody = "This is the test body of the email"
.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
%>
I've found example after example that give these code blocks as working examples, what am I doing wrong?
Brought in a fresh pair of eyes, and we found the answer.
The virtual SMTP server was listening on port 465(SSL), not 25. So with that corrected, classic ASP and VBS worked, but not VB.Net. We created a new Virtual SMTP server, listening on port 587. And as long as VB.Net had SSL disabled it would work. It appears that the new server didn't receive the SSL certificate during migration.

Sending e-mail using VB on ASP.NET through Gmail

I am trying to send an email from an ASP page using gmail, but when I try to send it, i just get "Error sending mail", with no description other than that.
SMTP is enabled on the gmail box I'm using and also working from mobile devices.
If anyone could give me a hand with this, I'll appreciate it.
Thanks.-
Friend Shared Sub Send_Mail(Destinatario As String)
Dim Message As New MailMessage()
Dim SmtpC = New SmtpClient("smtp.gmail.com")
Message.From = New MailAddress("xxxx#gmail.com")
Message.To.Add(Destinatario)
Message.Subject = "Some Subject"
Message.Body = "Message body"
SmtpC.Port = 465
SmtpC.Credentials = New System.Net.NetworkCredential("xxxx#gmail.com", "somepassword")
SmtpC.EnableSsl = True
Try
SmtpC.Send(Message)
Catch ex As Exception
End Try
End Sub

A recipient must be specified error when sending email from ASP.Net

We are trying to send the out the output of a html string to a particular test email address and found this error at runtime:
A recipient must be specified.
Here is the coding from the code-behind file.
Protected Sub EmailTheList()
' Get the rendered HTML.
'-----------------------
Dim SB As New StringBuilder()
Dim SW As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)
GridViewSummary.RenderControl(htmlTW)
' Get the HTML into a string.
' This will be used in the body of the email report.
'---------------------------------------------------
Dim dataGridHTML As String = SB.ToString()
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("myEmailAddress#gmail.com", "myPassword")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
ObjMailMessage = New MailMessage()
Try
ObjMailMessage.From = New MailAddress("myEmailAddress#gmail.com", "Some text is here.", System.Text.Encoding.UTF8)
ObjMailMessage.Subject = "Test message from Emad"
ObjMailMessage.ReplyToList.Add("john.doe#example.com")
ObjMailMessage.Body = dataGridHTML
ObjMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
SmtpServer.Send(ObjMailMessage)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
We suspect we are not using the correct syntax for this line:
ObjMailMessage.From = ObjMailMessage.ReplyToList.Add("john.doe#example.com")
You're missing the To: address, which is causing the error regarding a recipient.
ObjMailMessage.To.Add(New MailAddress("mail#somemail.com", "An error happened", System.Text.Encoding.UTF8))
Reference: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx

Sending to external email fail

Here is the codes:
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Dim attachment As System.Net.Mail.Attachment
SmtpServer.Credentials = New _
Net.NetworkCredential("administrator#company.com", "1234")
SmtpServer.Port = 25
SmtpServer.Host = "SmtpServer"
mail = New MailMessage()
mail.From = New MailAddress("user#company.com.my")
mail.To.Add("recipient#external.com")
mail.CC.Add("user1#company.com")
mail.CC.Add("user2#company.com")
mail.Headers.Add("Disposition-Notification-To", "user1#company.com") 'Read receipt
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure 'Fail delivery notification
mail.Subject = "Sending Documents"
mail.Body = "To Whom May Concern," & vbCrLf _
& vbCrLf & "Please refer to the attachment for the documents." & vbCrLf & _
"NOTE : This is an automatically generated email and will be sent daily."
For Each path As String In attch
attachment = New System.Net.Mail.Attachment(path)
mail.Attachments.Add(attachment)
Next
Try
SmtpServer.Send(mail)
SmtpServer = Nothing
Catch ex As Exception
Response.Write(ex.ToString)
Exit Sub
End Try
Problem is only internal email receiving, not external email.
No errors shown during the code execution.
Any idea on how to solve this?
Or do I need to configure something at the Microsoft Exchange Server?
Also the server using the MailMarshal to do the filtering.
An advanced thanks for the contributing feedback.
Make sure that "mail.From" value is in the list allowed by SMTP server. It could be that relaying is not allowed.

Resources