Error sending an email with VB.NET using gmail smtp - asp.net

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.

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

CDO.Message To and From Fields Changed to "Friendly Name" and Email Address

When I first created this Classic ASP script, with the help of W3Schools, to send email, it worked fine. Now I'm having issues with sending the actual email; it appears to hang on the .Send method.
I noticed that when I set the To and From email address to just the email address, it reformats it to a "Friendly Name"/Email Address format:
myMail.From="Support#myDomain.com"
Response.Write myMail.From
The output of the Response Write is:
"Support#myDomain.com" <Support#myDomain.com>
I don't know if this was happening before, or if I should be setting the To and From fields in this format. Just to check if this is causing my problem, is there anyway to prevent these fields from being changed from just the email address?
Maybe the e-mail sending from the server now needs some kind of authentication such as setting these fields:
' Outgoing SMTP server.
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mydomain.com"
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objCDO.Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
' Type of authentication, 0=NONE, 1-Basic (Base64 encoded), and 2=NTLM.
objCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
' UserID on the SMTP server
objCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "support#mydomain.com"
' Password on the SMTP server
objCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "myemailpassword"
' Update config.
objCDO.Configuration.Fields.Update
The issue stemmed from the fact that the "From" email address was actually a distribution list and the account credentials used to login to the email server were not authorized to "Send As". Once that was rectified emails sent without further problems.
Thanks for the response.

Unable to send email from web application

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.

Email sent from web server causes gmail to treat as phishing. How to get rid of this?

I am sending account activation email from my .net app.
I set the from address to "xyz.support#gmail.com" and from name "xyz" where xyz is the name of the domain i.e. our website.
It was not a problem when we were using Google's SMTP server as I provided credentials to google during sending. But now I am using my own web server's SMTP to send the email.
When I view the activation email in gmail, I get this:
This message may not have been sent by: xyz.support#gmail.com Learn more Report phishing
Is there a way to get rid of this so that gmail and other client don't show this message?
Here is the code:
var smtpClient = new SmtpClient();
var message = new MailMessage();
smtpClient.Host = _config.SMTPServer;
message.From = new MailAddress("xyz.support#gmail.com", "xyz");
message.To.Add("newuser#gmail.com");
message.IsBodyHtml = true;
message.Subject = "Test subject";
message.Body = "Test Body";
smtpClient.Send(message);
Thanks
The domain of the FROM address has to match the domain of the SMTP server that is sending the email, otherwise your message is treated as as spam.
This explains why you avoid the "error" by sending via Google's SMTP server.
The suggestion by IrishChieftain to use SPF helped me, so here is a summary of the steps I did:
1.) First, I also received emails in my GMail inbox that I sent from my sever and that got the "This message may not have been sent by..." warning.
2.) Next, I looked at the source of the email inside GMail (clicke the arrow next to the message and select "Display original"). An excerpt from there was:
Received-SPF: fail (google.com: domain of me#mydomain.com does not
designate 211.113.37.19 as permitted sender) client-ip=211.113.37.19;
So Google directly told me what to do: Add some SPF records in the DNS of my domain "mydomain.com" to get rid of this warning.
3.) Therefore I logged into the control panel of my DNS provider and added two TXT records, something like this:
*.mydomain.com. 180 v=spf1 +a +mx ip4:211.113.37.19 -all
mydomain.com. 180 v=spf1 +a +mx ip4:211.113.37.19 -all
Please note that I entered each line in three separate fields:
One field for *.mydomain.com.
One field for 180 (the TTL, 3 minutes in my example)
One field for v=spf1 +a +mx ip4:211.113.37.19 -all
4.) After that, I waited some time and tried to resend. This succeeded. Google now shows in the original:
Received-SPF: pass (google.com: domain of Received-SPF: pass (google.com: domain of me#mydomain.com designates 211.113.37.19 as permitted sender) client-ip=211.113.37.19;
Please note that I choose the SPF version since the mail server is on a different machine as the web server, so I could not perform the other solution as Mulmot wrote.
There is also an SPF Wizard from Microsoft to correctly generate SPF records. Alternatively, here is yet another SPF generator.

Classic ASP, Sending E-mail on windows server 2008 - error '8004020f'

I am trying to send e-mail with classic ASP (I am stuck with an old app so I have to use classic ASP here) on Windows Server 2008 R2, IIS 7.5.
I guess lots of thing change from win server 2003 to 2008. I am using the following code but I am getting this error (BTW, error message is so "informative");
error '8004020f'
/poo.asp, line 32
Here is the code;
<!--
METADATA
TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
NAME="CDO for Windows 2000 Library"
-->
<%
'Declare variables
Dim sch, cdoConfig, cdoMessage
sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "mail.example.com"
'Set SMTP port which is 25 by default
.Item(sch & "smtpserverport") = 587
.Update
End With
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
.From = "info#example.com"
.To = "me#example2.com"
.Subject = "Sample CDO Message"
.TextBody = "This is a test for CDO.message"
.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
Response.write "<HTML><head><title>A message has been sent.</title></head><body>A message has been sent.</body></HTML>"
%>
Also, I just installed Microsoft Exchange Server MAPI Client and Collaboration Data Objects 1.2.1 from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=1004 into my server but it didn't change anything.
NOTE: I could send e-mail over localhost if it works. doesn't matter.
You might come across the following error:
error '8004020f' The event class for this subscription is in an
invalid partition
This error message comes from cdosys.h, and has nothing to do with any sort of "partition" - it is actually lumped in with other errors in an overloaded message. The error code is actually attributed to the following:
CONST LONG CDO_E_RECIPIENTS_REJECTED = 0x8004020FL
Which means that the e-mail was rejected by the server for some reason. Here are some things you can try to alleviate the problem:
Make sure the SMTP server allows anonymous (non-authenticated) relaying. If your SMTP requires outgoing authentication, see Article #2026.
Check if the problem is specific to the domain name(s) used in the e-mail addresses of the recipients. For example, some users have complained that they can send to users on their own domain only; others have said that they can send to any domain except their own (see Article #2511 for some potential causes and solutions).
It may be simply that the e-mail address is being rejected, but other configuration settings on the SMTP server are preventing the true error message from being relayed propely back to the ASP script ... so verify that the address is valid.
If you have a proxy or firewall, make sure the web server is set up to correctly pass through it, that the SMTP server knows about it, and that the proxy allows access to port 25.
Try using a SendUsing value of 1 (pickup) instead of 2 (port). E.g. the following line:
.Item(cdoSendUsingMethod) = cdoSendUsingPort
Becomes
.Item(cdoSendUsingMethod) = cdoSendUsingPickup
http://classicasp.aspfaq.com/email/why-does-cdo-message-give-me-8004020f-errors.html

Resources