tags:

views:

89

answers:

3

Hello,

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 form the standard code which everyone would most probably be using.

string to = "[email protected]";
string from = "[email protected]";
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("[email protected]", "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?

Thanks!

+3  A: 

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 = "[email protected]";

//It seems, your mail server demands to use the same email-id in SENDER as with which you're authenticating. 
//string from = "[email protected]";
string from = "[email protected]";

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("[email protected]", "password");
client.Send(message);
this. __curious_geek
In that case there IS something wrong in his code. If his code does not provide certain information the code is "wrong".
Gertjan
@Gertjan: Yes, I agree with you. Updated the answer.
this. __curious_geek
@this. __curious_geekThanks! That solved it! Didn't know it was some weird settings that was setup on the SMTP side.
@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
+3  A: 

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

  <system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <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("[email protected]");
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
message.To.Add(new MailAddress("[email protected]"));
message.CC.Add(new MailAddress("[email protected]"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
rockinthesixstring
+2  A: 

Try this:

string to = "[email protected]";
string from = "[email protected]";
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