0
 <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="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<extensions>
  <add assembly="NLog.MailKit"/>
</extensions>

 <targets async="true">
<target name="errorLogger" xsi:type="Mail"
        smtpServer="mail.sample.com"
        smtpPort="3500"
        from="no-reply@mdg.com"
        to="example@demo.com"
        subject="test subject" />
</targets>
<rules>
   <logger name="EmailLogger" level="Warn" writeTo="errorLogger" />
  <logger name="EmailLogger" level="Error" writeTo="errorLogger" />
</rules>
</nlog>

C# code

private static Logger SendEmailLogger = LogManager.GetLogger("EmailLogger");
 if (exception != null)
                {
                    SendEmailLogger.Warn("Sending email from NLog");
                    SendEmailLogger.Error(exception, "Sending email from NLog");
                }

I think this is enough to send an email. I created custom errors too like dividebyzero and file not found errors. but still I am not getting any emails. Please can anyone suggest me, What I am missing.

I have already installed Nlog.mailkit from Nuget package. Internal logging Trace enter image description here

Fusion++1.1 Logs enter image description here

enter image description here

enter image description here

Mhd
  • 771
  • 1
  • 8
  • 15
  • 1
    Have you activated NLog InternalLogger check for warnings and errors? See also https://github.com/NLog/NLog/wiki/Logging-troubleshooting – Rolf Kristensen Mar 26 '20 at 20:21
  • @RolfKristensen I just updated ques. Added internal log file nlog.mailkit info. It is already installed in my solution but still showing cannot find the file specified. What is the reason? – Mhd Mar 27 '20 at 13:12
  • please clarify what the first snippet is.. seems like it's part of NLog.Config.. but I'm not certain. Also, include your csproj reference to the NLog.MailKit assembly\package (which is probably the underline issue.. that your project does specifically including it when you build.. i.e. hinting). – Brett Caswell Mar 27 '20 at 13:32
  • also, what type of application is this? what targetframework(s)? are you using Microsoft.NET.Sdk with your project? – Brett Caswell Mar 27 '20 at 13:50
  • 1
    @BrettCaswell It's a web application, using visual studio 2019, C#, Asp.net – Mhd Mar 27 '20 at 14:01
  • @Mhd Do you have a `NLog.Mailkit.dll` and a `Mailkit.dll` and a `MimeKit.dll` and a `BouncyCastle.Crypto.dll` in the bin-folder where you application is deployed? – Rolf Kristensen Mar 27 '20 at 14:24
  • @RolfKristensen Yes, I checked now. All dll's are present in my bin. One thing I want to mention. I created a class library for Nlog implementation and giving that class library referece to my multiple projects becuase I am using NLog in many projects. – Mhd Mar 27 '20 at 14:32
  • delete your web app's bin/debug and obj/debug folders (don't just clean it), then do a rebuild of your web app, verify those dlls are in your web app's bin/debug.. what is your targetframework? netcoreapp3.1? are you using docker container with this project? – Brett Caswell Mar 27 '20 at 14:55
  • also. please update your question and tags to include this sort information, don't just respond in comments. the concept here is to improve the question to make it answerable. As it exist now, it just raises these questions – Brett Caswell Mar 27 '20 at 14:59
  • 1
    .Net Framework 4.8 - Target Framework – Mhd Mar 27 '20 at 15:18
  • Guess you need to activate the fusion log, and see what assembly-dependency NLog.Mailkit.dll has that cannot be resolved at application startup. https://github.com/awaescher/Fusion?WT.mc_id=-blog-scottha – Rolf Kristensen Mar 27 '20 at 15:24
  • @RolfKristensen I updated question with Fustion log. Looks like there is error – Mhd Mar 27 '20 at 16:03
  • @BrettCaswell, I deleted bin/debug and obj/delete and build it again. but still no luck, I have all dll's – Mhd Mar 27 '20 at 16:06
  • @Mhd Did you try to click/double-click on the line with `NLog.Mailkit.dll` error? Maybe see if there was more details? Very aware that you have problems with loading NLog.Mailkit.dll – Rolf Kristensen Mar 27 '20 at 18:24

2 Answers2

1

Sounds like a local tooling/environment issue. Try to make an effort into cleaning/rebuilding the solution:

Could not load file or assembly - working on local not on production - ASP.net

Fallback to experimenting with a simple solution that has just a single project, and see if it is able to start. If it works, then slowly start moving items over from the original project and see where it breaks (if it breaks at all).

If the the simple single project solution is also failing, then make sure to update to latest Visual Studio (or reinstall), and try all possible ways to repair/reset your IIS Express installation.

Cannot run web application on Windows 10 using IIS Express

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • yea. I concur.. this isn't reproducible. I created a solution in vs2019 of a MVC WebAPI c# web application targeting net48, added NLog.Config and NLog.MailKit nuget packages.. copied the OP config snippet into it, added a Warning usage of that sample using the same static logger declaration, built and run and received the email.. – Brett Caswell Mar 27 '20 at 21:34
  • Wow, Thank you so much for looking this issue very personally, I really need to find the solution. – Mhd Mar 30 '20 at 12:44
  • cleaned and rebuild the solution, IIS server, visual management studio, nlog is updated – Mhd Mar 30 '20 at 16:18
0

I see you get a "Could not load file or assembly or one of its dependencies" for mailkit.dll

This could have the following reasons:

  • The mailkit.dll is not deployed.
  • You are using another (assembly) version of mailkit. As the Mailkit dll is strong named / signed, you can't easily change version. You need a bindingRedirect, or just keep the version of Mailkit in your whole solution the same.

Check assembly version of a DLL

You could check the assembly version of mailkit.dll as follow:

drag into VS2019:

enter image description here

Double click on Neutral:

enter image description here

Community
  • 1
  • 1
Julian
  • 33,915
  • 22
  • 119
  • 174
  • I have mailkit in project bin folder and using NLog.Mailkit 3.1.0 – Mhd Mar 27 '20 at 20:08
  • in the assembly version of mailkit.dll exactly 2.3.0? See screenshots – Julian Mar 27 '20 at 20:25
  • 2
    are there no old assembly redirects somewhere? Are you using packages.config or SDK csproj style? – Julian Mar 27 '20 at 20:38
  • versions are all up to date, I recently added nlog.mailkit and I checked Pagages.config, have all nlog dll's present there. – Mhd Mar 30 '20 at 12:39