swaroop katta
swaroop katta

Reputation: 709

smtp fails to send mail sometimes in c#

I have gone through some questions on this topic. All the answers relate when sending email fails all the time. In my case, it fails only sometimes with exception message:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM...

If I try second time it works. I'm using the following configuration.

using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("emailid", "displayname");
                    mail.To.Add("TOAddress");
                    mail.Subject = subject1;
                    mail.Body = body1;
                    mail.IsBodyHtml = true;

                    using (SmtpClient smtp = new SmtpClient("Outlook.office365.com", 587))
                    {
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential("emailid", "password");
                        smtp.EnableSsl = true;
                        smtp.Send(mail);
                    }
                }

checked a similar question here , given solutions not working.

Upvotes: 2

Views: 1804

Answers (1)

F. LK
F. LK

Reputation: 75

Try this(second answer): Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

I used this code to send the email:

            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("[email protected]");
            msg.To.Add("receiver@gmail");
            msg.Subject = "Hello";
            msg.Body = "Test";

            SmtpClient smt = new SmtpClient();
            smt.Host = "smtp.gmail.com";
            System.Net.NetworkCredential ntcd = new NetworkCredential();
            ntcd.UserName = "[email protected]";
            ntcd.Password = "senderPassword";
            smt.Credentials = ntcd;
            smt.EnableSsl = true;
            smt.Port = 587;
            smt.Send(msg);

Also check if your virus scanner doens't block your email from sending.

Upvotes: 1

Related Questions