Send mails from .NET

.NET offers a simple way to send mails from our applications.
We only need to create a instance of the class MailMessage, that will represent the email, and another instance of the class Smtp that will act like a Smtp Client to send the email.
Both of them are on the System.Net.Mail namespace.
The source code to use is very simple:

 MailMessage mail = new MailMessage(from, to, subject, content);

 SmtpClient client = new SmtpClient();

client.Send(mail);

And we need to specify the configuration for the Smtp client. You can configure it to use an Exchange server.
That can be done on the App.Config / Web.Config.

Here is the configuration:

<system.net>
    <mailSettings>

        <smtp deliveryMethod="Network">

            <network

             host="mailHost"

             port="25"

             userName="Domain\Name"

             password="Password"/>

        </smtp>

    </mailSettings>
</system.net>

2 Comments

Comments have been disabled for this content.