4

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("some@email.com");
            message.To.Add(new MailAddress("other@email.com"));


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

            // NetworkCredential basicCredential = new NetworkCredential("test@test.com", "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.

meda
  • 45,103
  • 14
  • 92
  • 122
  • The error message tells you that your reverse DNS is not set up correctly. This is not a code issue, but speak to your ISP about fixing the reverse CNS. – Rowland Shaw Jul 19 '13 at 19:42
  • For me the reason was the line "client.UseDefaultCredentials = true;". Once i removed that it worked. – F.H. Jul 14 '17 at 09:19

3 Answers3

2

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • thank you for the info , please advice on how I can solve it , I dont have much knowledge in DNS – meda Jul 19 '13 at 19:47
  • 1
    try doing `nslookup your.server.ip` and see what hostname comes up. you'll have to identify yourself to the smtp as that host name. if there's NO reverse mapping, you'll have to talk to your ISP to get one set up. – Marc B Jul 19 '13 at 19:49
  • its an IIS server, a previous worker worked on setting up DNS, where can I get more info about this – meda Jul 19 '13 at 20:03
  • I put reverse dns host name in my smtp host but its not working yet – Naveen Mar 27 '18 at 13:28
2

Looking back at my question, I can tell you that if you face this issue, It is not an issue in the code really it is more of a network issue.

A quick way to test this is to do run your code in a less locked down environment. I was getting this error only on my production server. It worked fine in development.

So this means you need to close visual studio and investigate. The error could be misleading so don't rely on it 100% instead use tools to debug your issue. I used wireshark to examine the packets and I noticed an IP that being denied on my SMTP server.

I didn't realize because my server had multiple network cards in my case. Something that I could not easily guessed. So my advise is to use tools to watch traffic network and you will find the reason.

Don't guess, use tools* to give you the answers.

*tools: in addition to wireshark, sites like mxtoolbox could be very helpful

meda
  • 45,103
  • 14
  • 92
  • 122
0

Old thread, but if anybody else has this issue, for me the issue was related to the "FROM" attribute. On the smtp server there is allowed "from" address which you have to request to your email server admins.