为什么NLog不发送电子邮件?

时间:2020-03-26 19:32:07

标签: c# web-applications nlog .net-framework-version

 <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#代码

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

我认为这足以发送电子邮件。我也创建了自定义错误,例如dividbyzero和找不到文件错误。但我仍然没有收到任何电子邮件。请有人能建议我,我想念的是什么。

我已经从Nuget软件包安装了Nlog.mailkit。 内部记录跟踪 enter image description here

Fusion ++ 1.1日志 enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

听起来像本地工具/环境问题。尝试努力清洁/重建解决方案:

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

退回到试验只有一个项目的简单解决方案,看看它是否能够启动。如果可行,则慢慢开始将项目从原始项目移至其他位置,然后查看中断的位置(如果完全中断)。

如果简单的单个项目解决方案也失败了,请确保更新到最新的Visual Studio(或重新安装),并尝试所有可能的方法来修复/重置IIS Express安装。

Cannot run web application on Windows 10 using IIS Express

答案 1 :(得分:0)

我看到您收到mailkit.dll的“无法加载文件或程序集或其依赖项”

这可能有以下原因:

  • 未部署mailkit.dll。
  • 您正在使用Mailkit的另一个(汇编)版本。由于Mailkit dll具有强名称/签名,因此您无法轻松更改版本。您需要一个bindingRedirect,或者只是在整个解决方案中保持相同的Mailkit版本。

检查DLL的程序集版本

您可以按照以下方式检查mailkit.dll的程序集版本:

拖入VS2019:

enter image description here

双击“中性”:

enter image description here