Unable to send email from web application - asp.net

I've looked this up and it seems other people have had the same issue mainly because Gmail was blocking the application's access, however I've turned on the "allow less secure apps to connect" option and the problem remains.
Here's the complete error message I get from ASP.net when I try to connect.
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
I've tried connecting using port 465 and I get a time-out (I assumed it was the Explicit/Implicit SSL problem)
Here's the code that I'm using to send the emails.
Dim Mail As New MailMessage
Dim EmailToMail As New MailAddress(EmailTo, EmailToName)
Mail.To.Add(EmailToMail)
Dim FromE As New MailAddress(EmailFrom, "Something")
Mail.From = FromE
Mail.Body = Body
Mail.Subject = Title
Dim SmtpClient As New SmtpClient("smtp.gmail.com", 587)
SmtpClient.UseDefaultCredentials = False
Dim SmtpCredentials As New System.Net.NetworkCredential("MyEmail", "MyPassword")
SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpClient.EnableSsl = True
SmtpClient.Send(Mail)
I've also tried setting UseDefaultCredentials to True, which didn't work.

your credentials are stored only within variable not within smtp instance.
set
SmtpClient.Credentials = SmtpCredentials
also make sure you use System.Net.Mail

This can happen if your password no longer meets Gmail's complexity criteria. Try logging in and changing it.

There can be a few issues.
Your firewall could be blocking it... (bypass it)
The port is blocked... (you need to allow it)
If you have two-step verification enabled on your email account...
Your credentials you are creating are never applied to the server credentials.
I have already answered this same question before here and will answer all of your problems. It also includes a link for sending the emails that I have answered as well.

Related

The server response was: 5.7.57 SMTP - Office 365

Currently we have a web form that send's emails which has stopped working months back with the migration of Office 365 at our company. I have eliminated all other code while troubleshooting and just run email portion to get the following error : "System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM ".
I have found many solutions on the web/stack overflow with the same error code, tried changing the code to proposed solutions and have had no luck.
Tried many different email aliases that are listed in our global address list with no luck for my account, different ports(25,587), different smtp addresses, and have had no luck so far.
Multiple users are stating "This code unfortunately is no longer valid with Office 365. Error message that comes up is The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [HE1PR05CA0133.eurprd05.prod.outlook.com] " on the article linked below.
Send SMTP email using System.Net.Mail via Exchange Online (Office 365)
My code seen below.
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress("email12345#company.com")
mail.To.Add("email12345#company.com")
mail.Subject = "Test"
mail.IsBodyHtml = False
mail.Body = "Test"
Dim SmtpServer As SmtpClient = New SmtpClient
SmtpServer.Host = "smtp.office365.com"
SmtpServer.Port = 587
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New System.Net.NetworkCredential("MyEmail", "MyPass")
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.EnableSsl = True
SmtpServer.TargetName = "STARTTLS/smtp.office365.com"
Try
SmtpServer.Send(mail)
Catch ex As Exception
Response.Write(ex.ToString)
End Try
As it seems this is happening to many people, I would like to know what they are doing to resolve it?
I resolved this issue by creating app password for my outlook365 account. You can create/manage app password as following.
Go to My Account >> Security & Privacy >> Additional Security Verification >> Create and manage app passwords
Create an app password and use it in your code. Hope this will solve your issue
To enable app-password you must have two factor authentication on the account and the administrator (if your account is in an organization) must allow app-passwords on the account (or in the organization)
I'm not a 365 admin and don't know all the settings.
However, we had a similar problem where the sender account did not have the setting "Modern Authentication" enabled for SMTP AUTH.
note that this worked for a long time and suddenly stopped (not sure if Microsoft did some security tightening in late February 2021)
Modern Authentication was enabled for Outlook etc (clients?),
but not for "SMTP AUTH", "IMAP", and "POP"
(I assume this is the "defaults" and recommendation is not to enable Modern Authentication unless you need it.
see the two articles below.
https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission
Virtually all modern email clients that connect to Exchange Online mailboxes in Office 365 or Microsoft 365 (for example, Outlook, Outlook on the web, iOS Mail, Outlook for iOS and Android, etc.) don't use SMTP AUTH to send email messages.
Therefore, we highly recommend that you disable SMTP AUTH in your Exchange Online organization, and enable it only for the accounts (that is, mailboxes) that still require it. There are two settings that can help you do this:
An organization-wide setting to disable (or enable) SMTP AUTH.
A per-mailbox setting that overrides the tenant-wide setting.
Also good to know about. Will list number of "smtp auth clients".
https://learn.microsoft.com/en-us/microsoft-365/security/office-365-security/mfi-smtp-auth-clients-report?view=o365-worldwide

Error sending an email with VB.NET using gmail smtp

I have a small website, there is a contact form that uses the gmail's smtp to send an email.
here's the code:
Dim MyMailMessage As New System.Net.Mail.MailMessage()
'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("office#mydomain.com")
MyMailMessage.To.Add("test#mydomain.com")
MyMailMessage.Subject = "MSG"
MyMailMessage.IsBodyHtml = True
MyMailMessage.Body = "TEXT"
'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("user", "pass")
SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
Return True
Catch exx As SmtpException
Throw exx
End Try
The error i am getting is :
5.5.4 HELO/EHLO argument invalid, closing connection. ks5sm2862700wjb.13 - gsmtp
Google just started blocking any connection which attempts a HELO/EHLO with incorrect arguments. According to RFC 5321, there should be one argument to EHLO/HELO, and it should be the name of the machine sending the mail.
The most common broken config is not sending any argument, which is what you're doing. The error message isn't that great in that you'd have to notice the two spaces that have your "empty" argument in the middle.
For regular mail sending, giving a hostname that matches the IP address you're sending from (ie, the DNS PTR lookup) is the most correct name to use.
For smtp-msa (ie, user/password based submission to smtp.gmail.com), that's less important, but it should still be the fully qualified domain name of the server, if it's available.
Google isn't enforcing the full requirements of RFC 5321 for this field, yet, but if you're going to fix it, it's best to do so correctly.
I don't see anything on the SmtpClient object to set what to send, so it's either completely broken, or it is making some internal call to get the hostname which is returning an empty string.
I was having the same problem, starting this morning.
We were setting the sender's email address as the argument for EHLO, and it was balking at that. I believe the # was causing the problem. This must be a new requirement, because this worked for years.
I changed the EHLO command to "EHLO google" and it works again.

Configure SMTP server on windows server 2008 to send emails from web application

i have this web application written in C# under ASP.NET 4.0 that runs on a Windows 2008 server. I want to send emails to users from username#mydomain.com but i am unable to. I'm using the following method (which worked using smtp.gmail.com) and using System.Net.Mail;
string from = "user#mydomain.com";
string password = "12345";
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = title;
mail.Body = message;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "stmp ip here";
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential(from, password);
smtp.EnableSsl = false;
smtp.Send(mail);
I have installed and configure SMTP server using basic authentication and outbound security -> basic authentication (using user#mydomain.com and 12345 as password). In IIS7 on my website i clicked on SMTP E-mail and entered the ip of my SMTP server together with the credentials from above.
Before doing all this config, i run into problems like the emails being stuck in the queue folder, server does not support ssl connections and others. Now, the only error i receive when sending email is logged from my application: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.3 Client was not authenticated and the email is not arriving at destination. The Queue folder is empty and i don't have any errors from the SMTP server log!
I did alot of research on google about it but i can't fix it. Telnet is working as it should, port 25 is not blocked and disabling windows firewall is not helping either. I'm really stuck. Any ideas? Thanks!
Sounds like the server may require you to set 'EnableSsl' to 'true'. So, you could either try to disable this on your server or simply set 'EnableSSl = true' in your client. You will probably then need to change the port to 587 or (sometimes) 465.

Unable to send emails to external domain using SMTP

I am not able to send emails to external domain addresses like 'user.one#asdf.com' using the code below.
SmtpClient smtpClient = new SmtpClient(smtpMailServer);
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
//Sending mail.
smtpClient.Send(mailMessage);
I get an exception -
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for xxx#example.com
If I change the DeliveryMethod to -
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
I am able to send the emails on my local machine. But it fails on the production site with an exception -
Cannot get IIS pickup directory
Can you please suggest me what to do?
I had this issue and authenticating fixed it see below:
SmtpClient client = new SmtpClient(EmailServer, 25);
var SmtpUser = new System.Net.NetworkCredential("domain\\username", "password");
client.Credentials = SmtpUser;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
I had to use the double slash since one slash is the escape character so use two for it to work.
If you were to look up the MX record for the destination address (in your example, it is asdf.com) and then use that for the host property of SmtpClient, it should accept the message for delivery without authentication since it's to a local user. This is not easy to do since System.Net doesn't provide a managed DNS class that can return MX records but you can P/invoke unmanaged code to do it. Otherwise you will need to be sure that whatever SMTP server you are connecting to will relay for you and then set the Credentials property of SmtpClient to the appropriate credentials for connecting to that server. Setting the DeliveryMethod to PickupDirectoryFromIIS still only writes a file to the IIS pickup directory so it's only writing a file, it isn't doing an actual send.
You usually need to authenticate with the external mail server using a username/password. As you are using an external server this will not know the credentials you are passing. This may be your issue.
I faced this issue, which I solved by added a domain with "*.com" as domain name and type as "remote", under IIS 6.0 Manager/SMTP Virtual Server/Domains.
Although in my case the SMTP server allowed anonymous access.

How can I send local Email by using exchange with asp.net?

I have a project that need to sends notification for employees by local email. I use SmtpClient class to send email but didn't work! here is the code:
MailMessage message = new MailMessage();
message.From = new MailAddress("localmail1#company.com");
message.To.Add(new MailAddress("localmail2#company.com"));
message.Subject = "Sending mail";
message.Body = "Check sending email by Exchange from asp.net code <> ";
SmtpClient client = new SmtpClient("ExchangeDNS", 25);
try
{
client.Send(message);
}
catch (Exception exc)
{
Label1.Text = exc.Message.ToString();
}
When I click buttons it give me an SmtpException with message : Failure sending mail.
NB: we use Exchange server.
How can I solve it?
Use the fully qualified name for your server, for example, exchangeDNS.example.com. If that doesn't work, try the IP address. You may want to manually telnet to port 25 on the Exchange server, just to see if it is possible. Also, check whether your server requires an authenticated or encrypted connection. If so, you'll need to supply credentials with the request or change to use SSL and the secure port.
Assuming that you have the server and any required credentials correct (double check these) I would try increasing the timeout on the SMTP client.
Your code is fine. It's the configuration of your Exchange server that is suspect. Some options for your Exchange administrator:
Allow anonymous SMTP connections.
Whitelist the IPs, so that only your web servers can send mail in this manner.
Create an Active Directory account for the web server, and then have your class authenticate using a username and password.
If you post more details about your Exchange configuration, I can help, at the risk of having this question downmodded for "not programming related".

Resources