|
/*-----------------------------------------------------------------------------------------------------------
* Author :Sujith John Thomas
* Project :SMTP Mail System Using ASP.NET 2.0
* Module Name :Send Mail
* Dated :16/07/2007
* Modified on :
* ----------------------------------------------------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
#region DECLARATIONS
private string _mMailServer;
private string _mTo;
private string _mFrom;
private string _mMessage;
private string _mSubject;
private int _mPort;
private MailMessage objMessage;
#endregion
#region EVENTS
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
Send();
}
protected void btnReset_Click(object sender, EventArgs e)
{
Clear();
}
#endregion
#region METHODS
//----------------------------------------------------------
//Method to show the message
//----------------------------------------------------------
private void Message(string strMessage)
{
Label lblMsg = new Label();
lblMsg.Text = strMessage;
phMessage.Controls.Add(lblMsg);
}
//----------------------------------------------------------
//Method to send the mail.
//----------------------------------------------------------
private void Send()
{
//Assigning value to the variables
_mTo = txtTo.Text.Trim();
_mFrom =txtFrom.Text.Trim();
_mSubject = txtSubject.Text.Trim();
_mMessage = txtBody.Text.Trim();
//Getting the mail server ip from the web.config file
_mMailServer = ConfigurationManager.AppSettings["MailServer"].ToString();
//Getting the mail server port from the web.config file.
_mPort = Int32.Parse(ConfigurationManager.AppSettings["MailServerPort"].ToString());
try
{
//Passing parameters to the Mail Message object.
objMessage = new MailMessage(_mFrom, _mTo, _mSubject, _mMessage);
//Instantiating Attachement object
Attachment attach = new Attachment(fileAttach.PostedFile.FileName);
//Adding attachment to the message.
objMessage.Attachments.Add(attach);
//Instantiating the SmtpClient
SmtpClient objSmtpClient = new SmtpClient(_mMailServer, _mPort);
//Setting default credentials to true
objSmtpClient.UseDefaultCredentials = true;
//Sending the mail.
objSmtpClient.Send(objMessage);
Message("Mail has been sent successfully.");
//Resetting the form
Clear();
}
catch (FormatException ex)
{
Message("Format Exception:" + ex.Message);
}
catch (SmtpException ex)
{
Message("SMTP Exception:" + ex.Message);
}
catch (Exception ex)
{
Message("General Exception:" + ex.Message);
}
}
//---------------------------------------------------------
//Method for clearing the fields.
//---------------------------------------------------------
private void Clear()
{
txtTo.Text = string.Empty;
txtFrom.Text = string.Empty;
txtSubject.Text = string.Empty;
txtBody.Text = string.Empty;
}
#endregion
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.