12

I have been trying to send an email by C#. I have Googled for various examples and have taken bits and pieces from each and from the standard code which everyone would most probably be using.

string to = "receiver@domain.com";
string from = "sender@domain.com";
string subject = "Hello World!";
string body =  "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);

However, I keep getting an error stating

System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: Access denied - Invalid HELO name (See RFC2821 4.1.1.1)

So, what do I do now? Is SmtpClient supposed to be special and only work on specific SMTP servers?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
user303907
  • 543
  • 2
  • 9
  • 18

6 Answers6

10

It seems your username/password pair is not authenticating successfully with your SMTP server.

EDIT

I think, I found what's wrong here. I have corrected your version below.

string to = "receiver@domain.com";

//It seems, your mail server demands to use the same email-id in SENDER as with which you're authenticating. 
//string from = "sender@domain.com";
string from = "test@domain.com";

string subject = "Hello World!";
string body =  "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);
this. __curious_geek
  • 42,787
  • 22
  • 113
  • 137
  • In that case there IS something wrong in his code. If his code does not provide certain information the code is "wrong". – Gertjan Jul 01 '10 at 05:37
  • @Gertjan: Yes, I agree with you. Updated the answer. – this. __curious_geek Jul 01 '10 at 05:51
  • Thanks! That solved it! Didn't know it was some weird settings that was setup on the SMTP side. – user303907 Jul 01 '10 at 08:46
  • @user303907: Well the message might seem odd, but the client identifies with the HELO command to the server, so if the server cannot log you on he can give you an access denied (which happened) and some might inform you where it happened (and that was in the HELO part of the conversation). – Gertjan Jul 01 '10 at 09:04
  • I'm facing the same issue. Earlier it was working but now client is complaining this issue. Client said they have not changed any setting. Any suggestion? – Jagz W Apr 20 '19 at 11:50
4

Have you tried setting your auth credentials in the web.Config?

  <system.net>
    <mailSettings>
      <smtp from="test@foo.com">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

and your code behind

MailMessage message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");
message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
message.To.Add(new MailAddress("recipient3@foo.bar.com"));
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
3

Try this:

string to = "receiver@domain.com";
string from = "sender@domain.com";
string subject = "Hello World!";
string body =  "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
// explicitly declare that you will be providing the credentials:
client.UseDefaultCredentials = false;
// drop the @domain stuff from your user name: (The API already knows the domain
// from the construction of the SmtpClient instance
client.Credentials = new NetworkCredential("test", "password");
client.Send(message);
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
2

In my case, it was a wrong port. The configuration provided by the hosting didn't worked both SSL (465) and no SSL (25). I used MS Outlook to "crack" the configuration, and then copied to my application. It was 587 SSL.

RonaldPaguay
  • 337
  • 3
  • 12
1

This can happen if you don't set EnableSsl.

client.EnableSsl = true;
TheJonz
  • 394
  • 2
  • 11
  • 1
    This solved my problem. In documentation there was writen port 587 for non-ssl connection, but that was returning HELO error. After switching ssl to true it worked :) – luka_matkic Jul 12 '21 at 15:10
0

If you have a webmail client available and you see a cPanel logo it could be a setting there as well.

enter image description here

We got the exception and asked our hosting company to go into:

"Root WHM > Service Configuration > Exim Configuration Manager > Basic Editor > ACL Options"

and set the Require RFC-compliant HELO setting to Off.

This worked for us after fixing the next error:

SMTP AUTH is required for message submission on port 587

Source:

https://serverfault.com/a/912351/293367

Ogglas
  • 62,132
  • 37
  • 328
  • 418