Sunday, 23 November 2008
DotNetSpace, Asp.net 2.0 tutorials and code examples. ajax, log4net...
Home arrow Articles arrow General Articles arrow Sending emails
 
Main Menu
Home
Articles
News
Links
Search








BiraJones




Sending emails Print E-mail
Tuesday, 17 October 2006

This text only shows how to send e-mails in asp.net 2.0, including html emails.
This article do not show how to send emails with attached files. This is a subject for another article.

Follow the steps described below.

1- First, put this configuration int the web.config file in the web application root folder: 

    ...
    </system.web>
    ...
    <system.net>
        <mailSettings>
            <smtp from=" This email address is being protected from spam bots, you need Javascript enabled to view it ">
                <network host="192.168.0.6"
                         port="25"
                         password="pass"
                         userName="userx" />
            </smtp>
        </mailSettings>
    </system.net>
    ...

The following code can be used to send emails.
You must replace the '...' by the appropriated 'using xxx' lines if necessary.

    ...
    using System.Net.Mail;
    ...
    ...  
     
    SmtpClient client = new SmtpClient();
    MailAddress from = new MailAddress(" This email address is being protected from spam bots, you need Javascript enabled to view it ", "namefrom");
    MailAddress to = new MailAddress(" This email address is being protected from spam bots, you need Javascript enabled to view it ", "nameto");
    MailMessage message = new MailMessage(from, to);
    
    message.IsBodyHtml = true;
    message.Subject = "Your friend xxx blablabla..";
    
    string fullText = "this is the full mail text.... lalalla..."; 
    message.Body = fullText;
    
    try
    {
        client.Send(message);
        Response.Redirect("somepage.aspx");
    }
    catch (Exception ex)
    {
        //using some log component, you can remove this lines if you want                               log.Error("Error sending email");
        log.Error(ex);
    }
    

That's it! easy and simple! In the try block, the redirect was only used to send the user to the success page. 

Last Updated ( Wednesday, 06 December 2006 )
 
< Prev
Popular
Polls
-->*/?>

Which language will you choose for coding with Visual Studio .NET?
 
-->*/?>

Is ASP.NET better than Java / JSP frameworks?
 

© 2008 DotNetSpace
Joomla! is Free Software released under the GNU/GPL License.