Click to learn how to rotate images dynamically with the ASP.NET Image Control
Home
Step by Step ASP.NET Design Solutions
Home
The Ultimate Website Design Guide Using ASP.NET and Adobe Photoshop

Search Engine Submission

 

They Said It Best

... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
Robert Firth

 

Web Forms Codebehind Example

This page builds on our exmple webform contact.aspx page we created in Step 1 ASP.Net Server Controls. When you created contact.aspx using Visual Studio or Visual Web Developer you should have a codebehind page called contact.aspx.cs.

Step 3. Web Form Sending Email Using System.Net.Mail

We had a working "contact us" page in Step 2. Code Behind. Now we want to email the results to the visitor as a confirmation and to the website owner or webmaster. Using the .Net's versatile system.net.mail add the follow subroutine to contact.aspx.cs.

void SendEmail(string status)
{
    try
    {
        MailMessage oMsg = new MailMessage();
        oMsg.BodyFormat = MailFormat.Html;
        oMsg.Subject = "Really Cool Web Design Request";
        oMsg.Body = "<html><body>";

        if (status == "GUEST") {
            oMsg.To = email.Text;
            oMsg.From = "info@website-designguide.com";

            oMsg.Body += "<p> This is an automatic email ";
            oMsg.Body += "indicating that we have received your request for ";
            oMsg.Body += "more information about really cool web designs.</p>";
            oMsg.Body += "Someone will be contacting you shortly.</p>";              
            
        }
        else
        {
            oMsg.Headers.Add("Reply-To", email.Text);
            oMsg.To = "info@website-designguide.com";
            oMsg.From = "webmaster@webmaster-designguide.com";
            oMsg.Body +="You have received an information request from the person ";
            oMsg.Body +="listed below.</p>;

            oMsg.Body += "<p>Full Name: " + FName.Text + " " + LName.Text;
            oMsg.Body += "<br>Address:   " + Street.Text;
            oMsg.Body += "<br>City:      " + City.Text;
            oMsg.Body += "<br>State:     " + State.Text;
            oMsg.Body += "<br>Zipcode:   " + Zip.Text;
            oMsg.Body += "<br>Email:     " + email.Text;
            oMsg.Body += "<br>Phone:     " + Telephone.Text;
            oMsg.Body += "<br>Contact Me:     " + Contactme.Text;

            if (Question.Text != null){
                oMsg.Body += "<br><b>Question(s):</b>";
                oMsg.Body+=<br><br>" + Server.HtmlEncode(Question.Text);}

            oMsg.Body += "</p>";
        }

        oMsg.Body += "<p>&nbsp;";
        oMsg.Body += "<p>Website Design Guide<br>";
        oMsg.Body += "http://www.website-designguide.com<br>";
        oMsg.Body += "<p>&nbsp;";
        oMsg.Body += "</body></html>";

        // ADD AN ATTACHMENT.
        // TODO: Replace with path to attachment.
        //String sFile = @"D:\FTP\username\Htdocs\Hello.txt"; 
        //MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
        //oMsg.Attachments.Add(oAttch);

        // TODO: Replace with the name of your remote SMTP server.
        SmtpMail.SmtpServer = "mail.yourserver.com";
        SmtpMail.Send(oMsg);

        oMsg = null;
        //oAttch = null;
    }
    catch (Exception e)
    {
        Console.WriteLine("{0} Exception caught.", e);
    }
}

 

Now in the onclick subroutine for frmValidate we include 2 calls to the SendEmail() subroutine. One call is for the guest and once to the webmaster or site owner:
SendEmail("GUEST");
SendEmail("POC");

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Mail;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class contact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    
    protected void frmValidate(object Source, EventArgs e)
    {        

 if (IsPostBack)
    if (IsValid)
    {               
    string confirmation;
                           
    confirmation = "<p><h3>THANK YOU. We have recieved your information.</h3>";
    confirmation += "<br>Please do not submit it again.<br><br>";
    confirmation += "<h3>Full Name: " + Server.HtmlEncode(FName.Text + " " + LName.Text);
    confirmation += "<br>Address:   " + Server.HtmlEncode(Street.Text);
    confirmation += "<br>City:      " + Server.HtmlEncode(City.Text);
    confirmation += "<br>State:     " + Server.HtmlEncode(State.Text);
    confirmation += "<br>Zipcode:   " + Server.HtmlEncode(Zip.Text);
    confirmation += "<br>Email:     " + Server.HtmlEncode(email.Text);
    confirmation += "<br>Phone:     " + Server.HtmlEncode(Telephone.Text);
    confirmation += "<br>Contact Me:     " + Server.HtmlEncode(Contactme.Text);
    confirmation += "</h3>";
    statusLabel.Text = confirmation;
    frmInfo.Visible = false;

    SendEmail("GUEST");
    SendEmail("POC");

    }
    else
        statusLabel.Text = "Invalid Info Submitted";

    }

    void SendEmail(string status)
    {
        try
        {
            MailMessage oMsg = new MailMessage();
            oMsg.BodyFormat = MailFormat.Html;
            oMsg.Subject = "Really Cool Web Design Request";
            oMsg.Body = "<html><body>";

            if (status == "GUEST")
            {
            oMsg.To = email.Text;
            oMsg.From = "info@website-designguide.com";

            oMsg.Body += "<p> This is an automatic email ";
            oMsg.Body += "indicating that we have received your request for ";
            oMsg.Body += "more information about really cool web designs. Thank you.</p>";
            oMsg.Body += "Someone will be contacting you shortly.</p>";

            }
            else
            {
            oMsg.Headers.Add("Reply-To", email.Text);
            oMsg.To = "info@website-designguide.com";
            oMsg.From = "webmaster@vra.net";
            oMsg.Body += "You have received an information request from the person ";
            oMsg.Body += "listed below.</p><p>"e;;

            oMsg.Body += "<p>Full Name: " + FName.Text + " " + LName.Text;
            oMsg.Body += "<br>Address:   " + Street.Text;
            oMsg.Body += "<br>City:      " + City.Text;
            oMsg.Body += "<br>State:     " + State.Text;
            oMsg.Body += "<br>Zipcode:   " + Zip.Text;
            oMsg.Body += "<br>Email:     " + email.Text;
            oMsg.Body += "<br>Phone:     " + Telephone.Text;
            oMsg.Body += "<br>Contact Me:     " + Contactme.Text;

           if (Question.Text != null){
            oMsg.Body += "<br><b>Question(s):</b>";
            oMsg.Body+=<br><br>" + Server.HtmlEncode(Question.Text);}
            
            oMsg.Body += "</p>";
            }

            oMsg.Body += "<p>&nbsp;";
            oMsg.Body += "<p>Website Design Guide<br>";
            oMsg.Body += "http://www.website-designguide.com<br>";
            oMsg.Body += "<p>&nbsp;";
            oMsg.Body += "</body></html>";

            // ADD AN ATTACHMENT.
            // TODO: Replace with path to attachment.
            //String sFile = @"D:\FTP\username\Htdocs\Hello.txt"; 
            //MailAttachment oAttch = new MailAttachment(sFile, MailEncoding.Base64);
            //oMsg.Attachments.Add(oAttch);

            // TODO: Replace with the name of your remote SMTP server.
            SmtpMail.SmtpServer = "mail.yourdomain.com";
            SmtpMail.Send(oMsg);

            oMsg = null;
            //oAttch = null;
        }
        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught.", e);
        }
    }
}
Privacy Policy Copyright © 2010 dotnetdesignguide.com  Webmaster  Sponsored by www.portable-computer-keyboard.com