Question

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="sender@sending.com" />
  <from value="receiver@receiving.rom" />
  <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 sender@sending.com).

What I want is a custom name, let's say Notifier, but still keep sending from sender@sending.com

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

Was it helpful?

Solution

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 <sender@sending.com>

Log4net XML configuration example:

<from value="Notifier &lt;sender@sending.com&gt;" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top