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>

Published Wednesday, January 09, 2008 11:04 AM by MarianoS

Comments

# re: Send mails from .NET

Friday, January 09, 2009 7:18 PM by Spartan

Great simple definition... THANKS

# re: Send mails from .NET

Wednesday, February 11, 2009 7:04 AM by Prity

Hello sir,

               Please tell me the full source code in asp.net with vb.net2.0  as soon as possible.

                   Thanks

# re: Send mails from .NET

Wednesday, February 11, 2009 7:41 AM by MarianoS

I think is something like this:

Dim MailMessage mail  as New MailMessage(from, to, subject, content)

Dim SmtpClient client as New SmtpClient()

client.Send(mail)

Leave a Comment

(required) 
(required) 
(optional)
(required)