Friday, April 25, 2008

Send Email (using System.Web.Mail)

First create new website project and add two pages :-
SendEmail.aspx and EmailSent.aspx

and in the page SendEmail.aspx add three TextBox (txtFrom , txtSubject , txtMessage)
and add button (btn_Send) and Lable control
and Required Validator for txtFrom , txtSubject


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.Web.Mail;

public partial class SendEmail : System.Web.UI.Page
{
string strMailOrTel;

protected void Page_Load(object sender, EventArgs e)
{

}
private bool SendMail()
{
try
{
string strTo = "y.ahmed@ewebbers.com";
string strFrom = txtFrom.Text;
string strBody = txtMessage.Text ;
string strSubject = txtSubject.Text ;
SmtpMail.SmtpServer = ("SMTP.ewebbers.com");
SmtpMail.Send(strFrom, strTo, strSubject, strBody);

return true;
}
catch (Exception ex)
{
return false;
}
}

protected void btn_Send_Click(object sender, EventArgs e)
{
bool TrueOrFalse = SendMail();
if ((TrueOrFalse == true))
{
Response.Redirect("~/MailSent.aspx");
}
else
{
Label1.Text = "Try again";
}

}


}

No comments: