Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send colorful mail in nlog

below config was my nlog setting, this setting send mail is OK .

<target name="mail" xsi:type="Mail"
            smtpServer="SMTP SERVER"
            smtpPort="25"
            smtpAuthentication="None"   
            enableSsl="false"   
            from="email address"  
            to="email address"
            html="true"
            encoding="UTF-8"
            addNewLines="true"
            replaceNewlineWithBrTagInHtml ="true"
            subject="SYSTEM MESSAGE:${machinename} 於 ${shortdate} ${time} create ${level} message "
            header="========================================================================="
            body="${newline} 
            time:${longdate} ${newline}${newline}
            Log level:${level:uppercase=true} ${newline}${newline}
            Logger:${logger} ${newline}${newline}
            Source:${callsite:className=true} ${newline}${newline}
            Exception:${exception:format=type} ${newline}${newline}
            Error message:${message} ${newline}${newline}"     
            footer="========================================================================="
    />

</targets>

 <rules>
    <logger name="*" minlevel="Fatal" writeTo="mail" />
 </rules>

but I want to send colorful mail. How to setting configure ?

like image 541
square Avatar asked Jan 23 '17 06:01

square


1 Answers

For markup like colors in your e-mail, you need a html mail and CSS styling.

e.g. this html:

<body>
    <b style="color:red">Bold and red text</b>
</body>

You need to set the html option on the mail target to true and in your nlog.config you need to XML encode the html, so in result:

<target name="mail" xsi:type="Mail"
            html="true"
            ...

            body="&lt;body&gt;
&lt;b style=&quot;color:red&quot;&gt;Bold and red text&lt;/b&gt;
&lt;/body&gt;"
    />

Please note that not all CSS is supported in all email clients. See CSS Support Guide for Email Clients

like image 192
Julian Avatar answered Oct 13 '22 22:10

Julian