imak
imak

Reputation: 6689

error when send email

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="[email protected]"> 
        <network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" /> 
      </smtp> 
    </mailSettings> 
  </system.net> 

Upvotes: 1

Views: 4736

Answers (2)

David
David

Reputation: 73554

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("[email protected]");
   mail.To.Add("[email protected]");

   //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);

}

Upvotes: 2

Icarus
Icarus

Reputation: 63956

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

Upvotes: 1

Related Questions