Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailbox unavailable. The server response was: relay not permitted

I am sending emails via an external SMTP server. Sending the email is handled with this code:

try
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.From = new MailAddress(froma);
        mail.To.Add(toc);
        mail.Subject = subject;
        mail.IsBodyHtml = true;
        mail.Body = body;
        SmtpClient smtp = new SmtpClient(ClientServer);
        DataSet ds = new DataSet();
        int retCode = Email.getSmtp(ref ds, DatabaseName);
        string User="";
        string Password="";

        if (ds.Tables["Value"].Rows.Count > 0)
        {
            User = ds.Tables["Value"].Rows[0]["UserName"].ToString();
            Password = ds.Tables["Value"].Rows[0]["PasswordName"].ToString();
        }
        else
        {
            MessageBox.Show("Invalid SMTP settings!");
            return;
        }

        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential(User, Password);
        smtp.EnableSsl = false;
        smtp.Send(mail);
    }
    catch (System.Web.HttpException exHttp)
    {
        System.Console.WriteLine("Exception occurred:" + exHttp.Message);
    }

Testing this code on my server, with my own SMTP server on the same network, this returns all my emails. However, using an external SMTP server causes the error:

Mailbox unavailable. The server response was: relay not permitted.

I have read around and it appears that the admin for SMTP must allow relays for my server. However, using the authentication credentials provided, I can't seem to connect, and am still receiving the relay error.

like image 229
Patrick Developer Avatar asked Dec 26 '22 12:12

Patrick Developer


2 Answers

Yes, it sounds like the external server that you are using is not allow relay. Even if you have the proper authentication credentials, you will not be able to send the email because the relay function is still disabled. Are you the admin of this external server? If you are then you can enable it. This LINK HERE explains how to set up SMTP and the relay. If you are not the admin of this external server, then you will have to contact who is so they can enable the SMTP and relay for you.

It sounds like the server on your network has SMTP installed and the relay is set up properly since you are able to send. I had to install SMTP and configure the relay on all three of servers here (development box, staging box, and production box) to send emails. I hope this information helps.

like image 153
Humpy Avatar answered Feb 07 '23 09:02

Humpy


I had the same error and commented out :

//smtp.UseDefaultCredentials = true;

And the email was sent successfully.

like image 26
A Ghazal Avatar answered Feb 07 '23 10:02

A Ghazal