Sending to external email fail - asp.net

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.

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

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

ASP.net Contact Form Sending an email error

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

Fail Sending Email Unable To Connect the Remote Server

im facing error when sending email. here is the error
what is the actual error for this exception because i newbie in in this, by the way here is my code line:
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("test#gmail.com", "passwordexample")
SmtpServer.EnableSsl = True
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("test#gmail.com")
mail.To.Add("examplemail#gmail.com")
mail.Subject = "Change Request Submitted"
mail.Body = "Dear User, " & Environment.NewLine & Environment.NewLine & Environment.NewLine & Environment.NewLine & "One Notification have been submitted," & Environment.NewLine & "Please check the change request on the intranet application"
//ad this line the error SmtpServer.Send(mail)
SmtpServer.EnableSsl = True
MsgBox("Notification emailed successfully")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Please see this answer...
Sending Email from Visual Basic
If you still can't send the email....
Have you enabled POP or IMAP in Gmail?
Sign in to the Gmail web interface.Open the 'Forwarding and POP/IMAP' tab on your 'Settings' page, and configure IMAP or POP. After enabling this in Gmail, make sure you click 'Save Changes' so Gmail can communicate with your mail client.
If you have 2 step verification..
Get an Application-Specific Password
Google will automatically generate a password that you will need only once, when you set up Google Mail on your mobile device or email software. You can easily generate a password for each device or email software you want to use.
Step 1:
At the bottom of the 2-step verification screen, next to Application-specific passwords, click Manage application-specific passwords. The Authorized Access to your Google Account screen will open.
Step 2:
On the Authorized Access to your Google Account screen, in the Name field, type in a name to help you remember what application you are using to access your account and click Generate password. You will then see a password which you will use to configure your mobile device or email software. Keep this screen open until you are ready to enter the password (see part 4, below). The password creation with the device name will show at the bottom. When you are done using the password, click Done.
Step 3:
To set up a password for another device or email software, simply type in the name of it in the Name field and click Generate password. You will receive another password.
See more here...
http://www.oit.umass.edu/support/google-apps/configure-google-mail-email-software-mobile-devices
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("EMAIL FROM#gmail.com", "YOUR PASSWORD")
SmtpServer.Port = 25
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Dim omail As New MailMessage()
omail.From = New MailAddress("FROM EMAIL #gmail.com", "Message", System.Text.Encoding.UTF8)
omail.Subject = "test subject"
omail.To.Add("test#gmail.com")
SmtpServer.SendAsync(omail, Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
if that didn't work try
SmtpServer.Port = 587

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

Resources