Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

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

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top