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)

                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)
                        approvalemailTo = approvalemailTo + t.Email + ";";

            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?


1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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