Wednesday, 8 February 2012

HOW TO SEND A MAIL IN ASP.NET



public partial class mailer : System.Web.UI.Page
{
MailMessage mailmsg;
SqlConnection con;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] == null)
{
Response.Redirect("index.aspx");
}
//if (Request.QueryString["id"] != null)
//{
//    txtToAddress.Text = Request.QueryString["id"];
//}
if(!IsPostBack)
{
String user = Session["admin"].ToString();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["name"].ConnectionString);
con.Open();
cmd = new SqlCommand("select email from registration where username='" + user + "'",con );
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
txtFromAddress.Text = dr.GetString(0);
}
dr.Close();
}
}
protected void btnSendmail_Click(object sender, ImageClickEventArgs e)
{
try
{
String to = txtToAddress.Text;
String from = txtFromAddress.Text;
String cc = txtCCAddress.Text;
String bcc = txtBCC.Text;
String subject = txtSubject.Text;
String body = txtMessage.Text;
mailmsg = new MailMessage();
mailmsg.From =new MailAddress( from );
mailmsg.To.Add ( new MailAddress(to));
if ((cc != null) && (cc != string.Empty))
{
mailmsg.CC.Add(new MailAddress(cc));
 
if ((bcc != null) && (bcc != string.Empty))
{
mailmsg.Bcc.Add(new MailAddress(bcc));
}
mailmsg.Subject = subject;
mailmsg.Body = body;
if (fileAttachment.HasFile)
{
mailmsg.Attachments.Add(new Attachment(fileAttachment.PostedFile.FileName));
}
// Set the format of the mail message body as HTML
mailmsg.IsBodyHtml = true;
// Set the priority of the mail message to normal
mailmsg.Priority = MailPriority.Normal;
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = ConfigurationManager.AppSettings["hostname"];
smtpclient.Send(mailmsg);
}
catch (Exception ex)
{
lblmsg.Text = "Your mail has been sent..";
}
}
}

No comments:

Post a Comment