We want to create a newsletter where user enter his full name and his email which send him email of a deaily news in website
In other word this is a way to send email to multiple user at the same time
-- First :- create database called (Database) has table named (Person_Data) has the following fields:
Id --> (int , Primary key , Identity)
FullName --> nnarchar(50)
Email --> nnarchar(50)
-- Second :- craete website has the following pages
1) AddPerson.aspx
2) DataSaved.aspx
3) SendEmail.aspx
4) EmailSend.aspx
--**AddPerson.aspx :-
add two Textbox (txt_Full , txt_Email) and two RequiredFieldValidator control and button (Save)
--** AddPerson.aspx.cs
using System;
using System.Data;
using System.Configuration;
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.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public int add_Person(string Name, string Email)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data_ConnectionString"].ConnectionString);
string quey = "INSERT INTO [Person_Data] ([FullName], [Email]) VALUES (@FullName, @Email)";
SqlCommand comm = new SqlCommand();
comm.CommandText = quey;
comm.CommandType = CommandType.Text;
comm.Connection = conn;
comm.Parameters.Add("@FullName",SqlDbType.NVarChar,50).Value=Name;
comm.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = Email;
conn.Open();
int x = comm.ExecuteNonQuery();
conn.Close();
return x;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int y = add_Person(txt_Full.Text, txt_Email.Text);
if (y > 0)
{
Response.Redirect("~/DataSaved.aspx");
}
else
{
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "");
}
}
}
--** SendEmail.aspx
add Gridview and SQLDataSource control to select all data in Gridview1
--** SendEmail.aspx.cs
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.Data.SqlClient;
using System.Web.Mail;
public partial class SendEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//-- send Email button
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Data_ConnectionString"].ConnectionString);
string query = "SELECT Email FROM [Person_Data]";
SqlCommand comm = new SqlCommand();
comm.CommandText = query;
comm.Connection = conn;
comm.CommandType = CommandType.Text;
SqlDataReader dr;
//--- Send Email ----
string Email = "yasser021@gmail.com";
string EmailPassword = "XXXXX";
string subject = "NewsLetter";
string body = "You are applied in newsletter";
//----
try
{
conn.Open();
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
string To="";
while (dr.Read())
{
To = dr["Email"].ToString();
//--
bool s = MailSender.SendEmail(Email, EmailPassword, To, subject, body, MailFormat.Html, "");
//if (s == true)
//{
// Response.Redirect("~/EmailSend.aspx");
//}
//else
//{
// ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "");
//}
}
dr.Close();
}
catch (Exception ex)
{
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
}
Don't forget i create a class called MailSender.cs
that used to send Email
Sunday, May 11, 2008
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment