Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set log4net SmtpAppender sender name in .config file

Tags:

c#

smtp

log4net

I tried to find a solution for this minor problem for quite a while but couldn't find an answer.

I want to set the sender's name of my e-mails that I send using log4net SmtpAppender, but I can't figure out how.

This is my log4net appender config:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="[email protected]" />
  <from value="[email protected]" />
  <subject value="test logging message" />
  <smtpHost value=" ... " />
  <authentication value="Basic" />
  <port value="587" />
  <bufferSize value="1" />
  <username value=" ... " />
  <password value=" ... " />
  <EnableSsl value="true"/>
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="FATAL"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
  </layout>
</appender>

It works, but when I receive the e-mail, the name of the sender is whatever is in front of the @ in the "from" parameter, in this case "sender" (as it's [email protected]).

What I want is a custom name, let's say Notifier, but still keep sending from [email protected]

I tried different parameters (just random guesses, since I couldn't find any good ideas while searching the net)... like from_name or sender_name ... nothing works...

It's my first question on SO, hope I met all criteria and someone can help me :)

Cheers

like image 386
skhro87 Avatar asked Jan 23 '14 07:01

skhro87


1 Answers

SmtpAppender (Line 469) attaches a from address by using new MailAddress(m_from), which accepts an email address. Fortunately, you can specify a name in the address field, stated under the Remarks section on MSDN.

Email format:

Notifier <[email protected]>

Log4net XML configuration example:

<from value="Notifier &lt;[email protected]&gt;" />
like image 132
matth Avatar answered Nov 01 '22 14:11

matth