8

I've got the error "Relay access denied", but I can connect my account with some email programs.

My code:

        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential basicCredential =
            new NetworkCredential("xx@xx.com", "xxx");
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress("xx@xx.com");

        smtpClient.Host = "mail.xx.com";
        smtpClient.Port = 587;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;

        message.From = fromAddress;
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        message.To.Add("aa@aa.com");

        try    
        {
            smtpClient.Send(message);
        }
        catch (Exception ex)
        {
            //Relay access denied...
        }

Does anyone know the reason for this?

Aage
  • 5,932
  • 2
  • 32
  • 57
Mustafa KIRILMAZ
  • 131
  • 1
  • 3
  • 7

5 Answers5

4

Since your smtp host port is 587, I think you should set smtpClient.EnableSsl to true before calling its Send method.

Yong Tiger
  • 121
  • 3
3

Although this question is almost a year old now, I just stumbled upon the solution to a similar problem today. Apparently, the System.Net.Mail.SmtpClient class does not support the AUTH PLAIN method. Unfortunately, the SMTP server I was trying to use only offered AUTH PLAIN. Since the SmtpClient class and my server couldn't agree on an authentication method, the SmtpClient tried to send mails without authentication which got rejected by the SMTP server...

You should make sure that the SMTP server you are using offers at least one of the authentication mechanisms that SmtpClient supports. I couldn't find an exhaustive list of supported mechanisms but my server is now offering AUTH LOGIN which works with SmtpClient.

PoByBolek
  • 3,775
  • 3
  • 21
  • 22
1

Please try

        SmtpClient client = new SmtpClient("mail.xx@xx.com");

        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("xx@xx.com", "xxx");
        client.Port = 587;
        client.EnableSsl = true;

        MailMessage maili = new MailMessage();
        maili.Body = body;
        maili.Subject = subject;
        maili.IsBodyHtml = true;
        maili.From = new MailAddress("xx@xx.com");
        maili.To.Add("aa@aa.com");

        try
        {
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            client.Send(maili);
        }
        catch (Exception ex)
        {
            throw ex;
        }

        maili.Dispose();
pravprab
  • 2,301
  • 3
  • 26
  • 43
Mustafa KIRILMAZ
  • 131
  • 1
  • 3
  • 7
1

In my case, the smtp password had changed, and gave me this error...

Erik
  • 119
  • 1
  • 3
0

Usually it's very simple: the "From" e-mail address must have the same domain name as the domain name of the account from which the e-mail is being sent. So, if you send e-mail through a Hotmail account with the e-mail address e.g. "john.doe@hotmail.com", the "From" address must be set to the same.

Loco Barocco
  • 121
  • 7