.NET and SMTP Configuration

Sometimes I feel stupid about discovering .NET features that have been there since an old release (2.0 in this case)...

Apparently you can just use this configSecion “mailSettings” and never have to configure your SmtpClient instance in code again (no, not hard-coded):

<system.net>
    <
mailSettings
>
        <
smtp deliveryMethod="Network" from="My Display Name &lt;myname@mydomain.com&gt;"
>
            <
network host="mail.server.com"
/>
        </
smtp
>
    </
mailSettings
>
</
system.net
>

Now you can go all like:

new SmtpClient().Send(mailMessage);

…and everything is configured for you, even the from address (which you can obviously override).

2 Comments

Comments have been disabled for this content.