1

I am using the code as described in this question. However get following error when send an email.

Mailbox unavailable. The server response was: please authenticate to use this mail server

Any ideas what could be wrong?

UPATE: Here is the code

System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient(); 
MailMessage Message = new MailMessage("From", "To", "Subject", "Body"); 
Client.Send(Message); 

With following in App.config.

 <system.net> 
    <mailSettings> 
      <smtp from="support@MyDomain1.com"> 
        <network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" /> 
      </smtp> 
    </mailSettings> 
  </system.net> 
Community
  • 1
  • 1
imak
  • 6,489
  • 7
  • 50
  • 73
  • Authenticate sounds that you need to provide credentials? user password? – gbianchi Mar 27 '12 at 14:41
  • I have provided those in the configuration file. I double checked and those are correct values. – imak Mar 27 '12 at 14:42
  • 1
    Show us your code. We'd like to see any edits that you've made. – Msonic Mar 27 '12 at 14:42
  • You might also want to check if you're not behind a proxy. If you are check your code is passing your credentials to the proxy. – JL. Mar 27 '12 at 14:43
  • Added the code. If I send an email to that address, I don't get any errors back from server. – imak Mar 27 '12 at 14:45

2 Answers2

2

The code posted there should work. if it doesn't, you might try setting the username and password in code-behind rather than reading them from the web.config.

Code sample from systemnetmail.com:

static void Authenticate()
{
   //create the mail message
   MailMessage mail = new MailMessage();

   //set the addresses
   mail.From = new MailAddress("me@mycompany.com");
   mail.To.Add("you@yourcompany.com");

   //set the content
   mail.Subject = "This is an email";
   mail.Body = "this is the body content of the email.";

   //send the message
   SmtpClient smtp = new SmtpClient("127.0.0.1");

   //to authenticate we set the username and password properites on the SmtpClient
   smtp.Credentials = new NetworkCredential("username", "secret"); 
   smtp.Send(mail);

}
David
  • 72,686
  • 18
  • 132
  • 173
1

Yes, the smtp server is telling you that in order to relay email for you, you need to authenticate before attempting to send the email. If you have an account with the smptp server, you can set the credentials on the SmtpClient object accordingly. Depending on the authentication mechanism supported by the smtp server, the port, etc, will be different.

Example from MSDN:

public static void CreateTestMessage1(string server, int port)
{
            string to = "jane@contoso.com";
            string from = "ben@contoso.com";
            string subject = "Using the new SMTP client.";
            string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
            MailMessage message = new MailMessage(from, to, subject, body);
            SmtpClient client = new SmtpClient(server, port);
            // Credentials are necessary if the server requires the client 
            // to authenticate before it will send e-mail on the client's behalf.
            client.Credentials = CredentialCache.DefaultNetworkCredentials;

      try {
              client.Send(message);
      }
            catch (Exception ex) {
              Console.WriteLine("Exception caught in CreateTestMessage1(): {0}", 
                    ex.ToString() );
      }              
}

The bottom line is that your credentials are not being passed to the Smtp server or else you wouldn't be getting that error.

Icarus
  • 63,293
  • 14
  • 100
  • 115