Page

How to send an Email using C# – complete features

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Net;
using System.Net.Mail;

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

    }
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=rnp;Integrated Security=True");
    SqlDataAdapter objDA = new SqlDataAdapter();
    DataSet objDS = new DataSet();
    DataTable objDT = new DataTable();
    Int32 TotalRecords = 0, CurrentRecord = 0;
 protected void Button1_Click(object sender, EventArgs e)
    {
 MailMessage msg;
        SqlCommand cmd = new SqlCommand();
        string ActivationUrl = string.Empty;
        string emailId = string.Empty;

        try
        {
            string date = DropDownList2.SelectedItem.Text + "/" + DropDownList3.SelectedItem.Text + "/" + DropDownList4.SelectedItem.Text;
            cmd = new SqlCommand("insert into Login (Name,DOB,Email_id,Passwd,Gender,Location,Mobile_no) values (@Name,@DOB,@Email_id,@Passwd,@Gender,@Location,@Mobile_no) ", con);

            cmd.Parameters.AddWithValue("@Name", TextBox1.Text.Trim());
            cmd.Parameters.AddWithValue("@DOB", date.Trim());
            cmd.Parameters.AddWithValue("@Email_id", TextBox2.Text.Trim());
            cmd.Parameters.AddWithValue("@Passwd", TextBox40.Text.Trim());
            cmd.Parameters.AddWithValue("@Gender", RadioButtonList1.SelectedValue.Trim());
            cmd.Parameters.AddWithValue("@Location", DropDownList5.SelectedValue.Trim());
            cmd.Parameters.AddWithValue("@Mobile_no", TextBox5.Text.Trim());

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            cmd.ExecuteNonQuery();

            //Sending activation link in the email
            msg = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            emailId = TextBox2.Text.Trim();
            //sender email address
            msg.From = new MailAddress("xyz@gmail.com");
            //Receiver email address
            msg.To.Add(emailId);
            msg.Subject = "Confirmation email for account activation";
            //For testing replace the local host path with your lost host path and while making online replace with your website domain name
            ActivationUrl = Server.HtmlEncode("http://localhost:8345/medi/ActivateAccount.aspx?id=" + Fetchid(emailId) + "&Email_id=" + emailId);

            msg.Body = "Hi " + TextBox1.Text.Trim() 
            
            + "!\n Your Id Is : "  + "\n Your Password Is : " + TextBox40.Text.Trim() +

                "Thanks for showing interest and registring in <a href='http://www.eBusinessSolution.com'> eBusinessSolution.com<a> " +
                  " Please <a href='" + ActivationUrl + "'>click here to activate</a>  your account and enjoy our services. \nThanks!";
            msg.IsBodyHtml = true;
            smtp.Credentials = new NetworkCredential("sendermail@gmail.com", "password");
            smtp.Port = 25;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Send(msg);

            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Confirmation Link to activate your account has been sent to your email address');", true);
        }
       
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
            return;
        }
        finally
        {
            ActivationUrl = string.Empty;
            emailId = string.Empty;

            con.Close();
            cmd.Dispose();
        }
    }
    


    private string Fetchid(string emailId)
    {
        SqlCommand cmd = new SqlCommand();
        cmd = new SqlCommand("SELECT id FROM Login WHERE Email_id=@Email_id", con);
        cmd.Parameters.AddWithValue("@Email_id", emailId);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        string id = Convert.ToString(cmd.ExecuteScalar());
        con.Close();
        cmd.Dispose();
        return id;
    }
    }


// SQL Table //




    

    

No comments:

Post a Comment