Sending email through SMTP fails : MailBox name not allowed

meda picture meda · Jul 19, 2013 · Viewed 9.3k times · Source

I am trying to send an email through an SMTP server but it fails giving me the following error:

MailBox name not allowed. The server response was Senders must have valid reverse DNS

enter image description here Unfortunately I could not find meaningfull information to solve the problem

Here is my method:

public void SendSmtp()
{
    try
    {
        using (MailMessage message = new MailMessage())
        {
            message.From = new MailAddress("[email protected]");
            message.To.Add(new MailAddress("[email protected]"));


            message.Subject = "subject";
            message.Body = "body";
            message.IsBodyHtml = true;

            // NetworkCredential basicCredential = new NetworkCredential("[email protected]", "password");
            try
            {
                using (SmtpClient client = new SmtpClient())
                {
                    client.Host = "mail.host.com";
                    client.Port = 25;
                    client.UseDefaultCredentials = true;
                    //  client.Credentials = basicCredential;
                    client.Send(message);
                    MessageBox.Show("Success!!");
                }

            }

            finally
            {
                //dispose the client
                message.Dispose();
            }

        }
    }
    catch (SmtpFailedRecipientsException ex)
    {
        for (int i = 0; i < ex.InnerExceptions.Length; i++)
        {
            SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
            if (status == SmtpStatusCode.MailboxBusy ||
                status == SmtpStatusCode.MailboxUnavailable)
            {
                Console.WriteLine("Delivery failed - retrying in 5 seconds.");
                System.Threading.Thread.Sleep(5000);
                //client.Send(message);
            }
            else
            {
                Console.WriteLine("Failed to deliver message to {0}",
                    ex.InnerExceptions[i].FailedRecipient);
            }
        }
    }
}

This works well when I try it on a different server or my local machine not sure why. I set up my SMTP grant access to my server. Please advice.

Answer

Marc B picture Marc B · Jul 19, 2013

The error's a direct response from the SMTP server that your code is attempting to connect to. Your client machine does not have a valid reverse DNS mapping (e.g. 127.0.0.1 -> localhost), so the SMTP server is rejecting the connection.

It could be something as simple as your client identifying itself as example.com, but when the SMTP server does a reverse lookup, the server's IP comes back as system-1-2-3.4.hostingprovider.com or similar.