Using NLog with Office 365

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

<targets>
<target name="email" xsi:type="Mail"
smtpServer="smtp.office365.com"
smtpAuthentication="Basic"
smtpPort="587"
smtpUserName="logicalmoon@myhost.com"
smtpPassword="xxxxxxxxxxxx"
from="logicalmoon@myhost.com"
to="someoneinteresting@theirhost.com"
html="true"
enableSsl="true"
replaceNewlineWithBrTagInHtml="true"
subject="Hello!"
timeout="10000"
/>
</targets>

<rules>
<logger name="*" minlevel="Debug" writeTo="email" />
</rules>
</nlog>

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 email target. There are only a few things that you need to change here.

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 smtpUsername.

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