Create a web page
and in Default.aspx.cs add the following code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
namespace Email
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblFrom;
protected System.Web.UI.WebControls.Label lblCC;
protected System.Web.UI.WebControls.TextBox txtFrom;
protected System.Web.UI.WebControls.TextBox txtCC;
protected System.Web.UI.WebControls.TextBox txtTo;
protected System.Web.UI.WebControls.Label lblTo;
protected System.Web.UI.WebControls.Label lblEmailService;
protected System.Web.UI.WebControls.TextBox txtBCC;
protected System.Web.UI.WebControls.Label lblBCC;
protected System.Web.UI.WebControls.Label lblSubject;
protected System.Web.UI.WebControls.TextBox txtSubject;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.TextBox txtMessage;
protected System.Web.UI.WebControls.Button btnSend;
protected System.Web.UI.HtmlControls.HtmlInputFile FileBrowse;
protected System.Web.UI.WebControls.Label lblAttachment;
protected System.Web.UI.WebControls.RadioButton rbtnAttach;
protected System.Web.UI.WebControls.RadioButton rbtnDetachment;
protected System.Web.UI.WebControls.Button btnCancel;
public string strAttachment;
private void Page_Load(object sender, System.EventArgs e)
{
lblMessage.Text="";
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.rbtnDetachment.CheckedChanged +=
new System.EventHandler(this.rbtnDetachment_CheckedChanged);
this.btnCancel.Click +=
new System.EventHandler(this.btnCancel_Click);
this.btnSend.Click +=
new System.EventHandler(this.btnSend_Click);
this.rbtnAttach.CheckedChanged +=
new System.EventHandler(this.rbtnAttach_CheckedChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//Method for sending the email
//***************************************************************
private void SendMail()
{
try
{
//Creating the Mail Message object.
MailMessage Email=new MailMessage();
//Storing the To value in the object reference.
Email.To=txtTo.Text;
//Storing the From value in the object reference.
Email.From=txtFrom.Text;
//Storing the CC value in the object reference.
Email.Cc=txtCC.Text;
//Storing the BCC value in the object reference.
Email.Bcc=txtBCC.Text;
//Storing the Subject value in the object reference.
Email.Subject=txtSubject.Text;
//Specifies the email body.
Email.Body=txtMessage.Text;
//Setting priority to the mail as high,low,or normal
Email.Priority=MailPriority.High;
//Formatting the mail as html or text.
Email.BodyFormat=MailFormat.Text;
//Checking whether the attachment is needed or not.
if(rbtnAttach.Checked)
{
//Adding attachment to the mail.
Email.Attachments.Add(
new MailAttachment(FileBrowse.Value));
}
//specifying the real SMTP Mail Server.
SmtpMail.SmtpServer.Insert(0,"127.0.0.1");
SmtpMail.Send(Email);//Sending the mail.
//calling the reset method to erase all the data
//after sending the mail.
Reset();
lblMessage.ForeColor=Color.Navy;
//User information after submission.
lblMessage.Text=
"*Your email has been sent successfully-Thank You";
}
//Catching Exception
catch(Exception exc)
{
Reset();
lblMessage.Text="Send error:"+exc.ToString();
lblMessage.ForeColor=Color.Red;
}
}
//Method to reset the text fields.
//***********************************************************
private void Reset()
{
//Seeking for the controls in the active webform.
Control myForm=Page.FindControl("Form1");
//Iterating each control.
foreach(Control ctl in myForm.Controls)
{
//Checking whether it is a text
//box and clear when it is a textbox.
if(ctl.GetType().ToString().Equals(
"System.Web.UI.WebControls.TextBox"))
((TextBox)ctl).Text="";
}
}
//Event fires for sending the mail.
//***********************************************************
private void btnSend_Click(object sender, System.EventArgs e)
{
SendMail();
}
//Event for cancelling the mail.
//***********************************************************
private void btnCancel_Click(object sender, System.EventArgs e)
{
Reset();
}
//Event for enabling or disabling the File browser.
//************************************************************
private void rbtnAttach_CheckedChanged(object sender,
System.EventArgs e)
{
FileBrowse.Disabled=false;
}
//Event for enabling or disabling the File browser.
//************************************************************
private void rbtnDetachment_CheckedChanged(object sender,
System.EventArgs e)
{
FileBrowse.Disabled=true;
}
//*************************************************************
}
}


No comments:
Post a Comment