0

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

3
+50

This isn't a SharePoint-related question as we're just talking about SMTP mail. With Exchange Online, you have a few options you can evaluate; these will use username/password as SMTP relays are not compatible with OAuth. Option 2 or 3 should fit your needs.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.