Poor command sequence. Server response: This mail server requires authentication when trying to send to a non-local email

I am trying to submit to asp.net, so I have a function you can see here:

public void SendEmail(string subject, string messageBody, string toAddress)
        {
            bool ssl = false;

            Repository.EmailConfRepository emailConf=new EmailConfRepository();
            DomainClass.EmailConfiguration e = emailConf.GetAll().First();
            if (e.EnableSsl == "true") ssl = true;
            MailMessage mail = new MailMessage();
            mail.To.Add(toAddress);
            mail.From = new MailAddress(e.EmailFrom);
            mail.Subject = subject;
            string Body = messageBody;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.UseDefaultCredentials = false;
            smtp.Host = e.SmtpServer;
            smtp.Credentials = new System.Net.NetworkCredential
                (e.Username, e.Password);

            smtp.Port = int.Parse(e.SmtpPort);
            smtp.EnableSsl = ssl;
            smtp.Send(mail);
        }

      

When I call this function I got this error:

Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.

      

Exception Details:

Exception Details: System.Net.Mail.SmtpException: Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server

      

+3


source to share


1 answer


The solution is to write to the web.config the correct parameters from which the email is sent.

<system.net>
 <mailSettings>
  <smtp from=from@yourdomain.com>
  <network host="localhost" password="" userName=""/>
  </smtp>
 </mailSettings>
</system.net>

      



Now if your server grants permissions to the sender localhost you don't need the password and username, but if you don't post your email info there

0


source







All Articles