Here’s a quick tip if you plan on using NLog (the .NET logging package) with your Office 365 subscription to send email alerts. For this, I’m not going to go into how to add it to your solution - I’m assuming you know the basics.
Firstly, is your Office 365 plan using two-factor authentication?
If your admin has turned on multi-factor authentication for your organization, and you’re using apps that connect to your Office 365 account, you’ll need to generate an app password so the app can connect to Office 365. For example, if you’re using Outlook 2016 or earlier with Office 365, you’ll need to create an app password.
You can do that here. To be honest, it’s better to do it anyway, if possible, because then you can use a password which is different from your main one.
Got your app password? Now you need to set your NLog configuration file to look something similar to this. I have included more than just the core of it just to show the context.
<?xml version="1.0" encoding="utf-8" ?>
Here’s a tip to troubleshoot when you might be having problems - set the
throwExceptions flag to
true. That way, it complains if anything goes wrong - a boon when there are issues! You can see that referenced right in the top tag, line 6.
Next, take a look at my
smtpUserName is the account that you log in with. Use the full domain.
smtpPassword here you place your app password that we mentioned earlier.
from is who you would like the email to come from - most probably the same as
to the recipient.
timeout this is how many milliseconds it should wait before timing out. 10,000 is usually more than enough, but increase it if you think it will help.
If you’ve tried the above, consider creating a small throwaway project that just sends something to the log. That will isolate the issue in case something else is interfering.
If that doesn’t work, jump onto a (secure) public WiFi. It could be your corporate network’s firewall blocking you.
Other ideas I read about where to try port (
smtpPort) 25 instead of 587.
Still problems? Try an email client using the same credentials.
Hope that helps save someone some time.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com