#StackBounty: #sharepoint-online #development #microsoft-graph #smtp Authenticate the SmtpClient using AppID and APPSecret instead of p…

Bounty: 100

I have the following SharePoint CSOM code inside my c# console application to send an email using the office 365 admin username and password using the client.Credentials = new NetworkCredential(username, passWord);, as follow :-

 static private void sendemail(ClientContext context, string subject, string body, FieldUserValue[] to, string username, SecureString passWord)
        {

            try
            {
                using (MailMessage mail = new MailMessage())
                {

                    mail.From = new MailAddress("sharepoint@***.com");
                    mail.Subject = subject;
                    mail.IsBodyHtml = true;
                    SmtpClient client = new SmtpClient("***-com.mail.protection.outlook.com", 25);
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new NetworkCredential(username, passWord);
                    client.EnableSsl = true;
                    mail.Body = body;
                    string approvalemailTo = "";

                    foreach (var t in to)
                    {
                        mail.To.Add(t.Email);
                        approvalemailTo = approvalemailTo + t.Email + ";";
                    }
                    client.Send(mail);
                    }
            }


            catch (Exception e)
            {

                   

            }

        }

but to make my code more secure, how i can authenticate the SmtpClient using AppID and APPSecret instead of passing the username and password?

Thanks


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.