Click here to Skip to main content
16,022,296 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Following is the code

try
            {
                host = ConfigurationManager.AppSettings["SMTPHost"];
                port = ConfigurationManager.AppSettings["SMTPPort"];
                sender = ConfigurationManager.AppSettings["SenderEmail"];
                password = ConfigurationManager.AppSettings["SenderPwd"];

                using (MailMessage mailMsg = new MailMessage())
                {
                    mailMsg.From = new MailAddress(sender);
                    mailMsg.To.Add(inputInfo.Recipient);
                    if (!string.IsNullOrEmpty(inputInfo.RecipientCC))
                    {
                        //if (inputInfo.CommID==(int)CommunicationID.CLAIM_Opted_INSTANTMONY_REJECT_EMAIL || inputInfo.CommID==(int)CommunicationID.CLAIM_ReInspection)
                        //{
                        //    inputInfo.RecipientCC = "";
                        //       inputInfo.RecipientCC =  ConfigurationManager.AppSettings["ZCH"] + "," + ConfigurationManager.AppSettings["SCH"];
                        //}
                        mailMsg.CC.Add(inputInfo.RecipientCC);
                    }

                    mailMsg.Subject = inputInfo.Subject;
                    mailMsg.Body = inputInfo.CommText;
                    mailMsg.IsBodyHtml = true;

                    using (SmtpClient smtp = new SmtpClient(host, Convert.ToInt32(port)))
                    {
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential(sender, password);
                        smtp.EnableSsl = true;
                        smtp.Send(mailMsg);
                    }
                }
            }

Web.config

<add key="SenderPwd" value="passkey" />


not able to send mail using SMTP 2 factor authentication in asp.net mvc


It Giving This Error:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful


What I have tried:

I tried Everything but it's not working,
Posted

1 solution

Well, you clearly didn't try everything, or you'd have found the answer already! :)

Important

We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit[^] or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub[^].

As the documentation says, the System.Net.Mail classes do not support modern protocols such as OAuth. If you need to support them, then you'll need to change your code to use a different library.

MailKit provides instructions for integrating with MS Exchange and GMail:
MailKit/ExchangeOAuth2.md at master · jstedfast/MailKit · GitHub[^]
MailKit/GMailOAuth2.md at master · jstedfast/MailKit · GitHub[^]

Other providers should follow a similar pattern.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900