簡體   English   中英

是否可以在NLog郵件目標中使用system.net/mailSettings/smtp中的“發件人”地址?

[英]Any way to use 'from' address from system.net/mailSettings/smtp in NLog mail target?

我想通過NLog郵件目標發送一些致命錯誤的電子郵件。 我還已經在web.config中配置了system.net/mailSettings/smtp

當我設置useSystemNetMailSettings="true" ,NLog會使用system.net/mailSettings/smtp中的所有內容( useSystemNetMailSettings="true" ”地址除外)。 因此,我需要再指定一次,尤其是對於郵件目標。

我做了什么? 我剛剛創建了一個變量“ MailFrom”,並在啟動應用程序時以編程方式對其進行了初始化。

<nlog>
    <variable name="MailFrom" value=""/>
    <targets>
        <target name="mail" type="Mail" from="${var:MailFrom}" html="false" subject="Subject" to="ToList" useSystemNetMailSettings="true"/>
    </targets>
</nlog>
var section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
if (section != null)
    LogManager.Configuration.Variables["MailFrom"] = section.From;

也許您知道一些更好的方法? 謝謝。

此選項已添加到NLog 4.3.2。


PS:更漂亮的方法是使用FindTargetByName

 var section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
 if (section != null)
 {

     var mailtarget = LogManager.Configuration.FindTargetByName<MailTarget>("mail");
     if (mailtarget != null)
         mailtarget.From = section.From;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM