How to send authenticated mail in C# without using basic authentication

Viewed 79

I have a application that needs to send out email notifications. The code for doing this is fairly standard and uses basic authentication (username & password) in line 3 to verify that I have access to this mailbox:

var client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[senderemail]", "[password]");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;

var msg = new MailMessage();
var recipient = "xxx@yyy.com";
msg.To.Add(recipient);
msg.From = new MailAddress("[senderemail]");
msg.Subject = "Test";
msg.Body = "<h2>Test</h2>";
msg.IsBodyHtml = true;

client.Send(msg);

Up to now, this has worked absolutely fine. However, the IT team at my organisation are wanting to turn off basic authentication for our exchange accounts, in line with ‎Microsoft‎ recommendations (since it is less secure). Therefore, I need to find an alternative way of authenticating for sending email.

I came across this article which at first seemed to have an answer. However I can't do Option 2 as I need to send mail external to the organisation, and I don't think I can reliably do Option 3 as the application is hosted on an internal server and so doesn't have a static IP.

I've also looked at using Microsoft Graph to send emails using certificate based authentication (e.g. this post) however if I have understood this correctly, the application would be permitted to send emails on behalf of any user in the organisation (as opposed to just one specific account) which my IT team would not be happy with.

I'm struggling to find a best practice approach of how to do this (which may just be down to my poor googling skills). Can anyone help point me in the right direction?

0 Answers
Related