Wednesday, December 30, 2009

Send Mail

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.Mail;
using System.Net;
using System.IO;
using System.Web.Mail;


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

}

#region Mail
public void SendMailv1(String sTo, String sSubject, String sBody, String sCC)
{
try
{
System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage();
oMail.Subject = sSubject;
oMail.Body = sBody;
if (sCC.Length > 0)
{
oMail.CC.Add(sCC);
}

SendMailV2(sTo, sSubject, oMail);
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
}
public void SendMailv1(String sTo, String sSubject, String sBody, String sCC, string sBcc)
{
System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage();
oMail.Subject = sSubject;
oMail.Body = sBody;
//oMail.Attachments.Add(oAttch);
if (sCC.Length > 0)
{
oMail.CC.Add(sCC);
}
if (sBcc.Length > 0)
{
oMail.Bcc.Add(sBcc);
}
SendMailV2(sTo, sSubject, oMail);
}

public void SendMailv1(String sTo, String sSubject, String sBody, String sCC, System.Net.Mail.Attachment oAtch)
{
System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage();
oMail.Subject = sSubject;
oMail.Body = sBody;
oMail.Attachments.Add(oAtch);

if (sCC.Length > 0)
{
oMail.CC.Add(sCC);
}
SendMailV2(sTo, sSubject, oMail);
}

public void SendMailV2(string sTo, string sSubject, System.Net.Mail.MailMessage oMail)
{
String sErrMessage;
try
{
// Add To, From an Subject

oMail.To.Add(sTo);
string sFrom = System.Configuration.ConfigurationManager.AppSettings["AdminMailId"].ToString();
oMail.From = new MailAddress(sFrom);
oMail.Subject = sSubject;

string sReplyTo = System.Configuration.ConfigurationManager.AppSettings["ReplyToMailId"].ToString();
//oMail.ReplyTo = new MailAddress(sReplyTo);

//oMail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
oMail.IsBodyHtml = true;

// Set Mail Server Credentials

SmtpClient smtpc = new SmtpClient(ConfigurationSettings.AppSettings["SMTPIP"].ToString());
//smtpc.Port = 25;

smtpc.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SMTPPort"]);
//string sUserName = System.Configuration.ConfigurationManager.AppSettings["AdminMailId"].ToString();
//string sPassword = System.Configuration.ConfigurationManager.AppSettings["AdminPId"].ToString();
// smtpc.Credentials = new NetworkCredential(sUserName, sPassword);
//smtpc.Timeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SMTPTimeOut"]);// 180 sec
//smtpc.Send(oMail);

smtpc.Send(oMail);
//if You want to save the mail in your database.
//Email.SaveMail(oMail.From.ToString(), oMail.To.ToString(), oMail.CC.ToString(), oMail.Bcc.ToString(), oMail.Subject, oMail.Body, InitilizePage());
oMail.Dispose();

}
catch (SmtpFailedRecipientsException ex)
{
sErrMessage = "Mail send failed. Mail could not be delivered to all recipients.";
throw new Exception(sErrMessage);
}
catch (SmtpException ex)
{
sErrMessage = "Mail send failed. Error Code: " + ex.StatusCode.ToString() + ex.InnerException.Message;

throw new Exception(sErrMessage);
}
catch (Exception ex)
{
sErrMessage = "Mail send failed. Unknown error occured.";
throw new Exception(sErrMessage);
}
}
#endregion

protected void btnsend_Click(object sender, EventArgs e)
{

SendMailv1(txtTo.Text,txtsub.Text,txtbody.Text,txtcc.Text);

}

}
}

DB Class

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;

namespace MyCodeSnippet.DAL
{
public class DataBase
{
#region "member variable"
static SqlConnection conn = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");
private SqlCommand cmd;
private SqlDataAdapter da;
#endregion


#region "member function"


public static void ExecuteNonQuery(string query)
{
try
{
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandType = CommandType.Text;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.ExecuteNonQuery();

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();

}
}



public static DataTable ExecuteQuery(string query)
{
DataTable dt = new DataTable();
try
{

SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}

da.Fill(dt);

}
catch(Exception ex)
{

throw new Exception(ex.Message);
}
finally
{

if (conn.State == ConnectionState.Open)
conn.Close();



}
return dt;


}



#endregion
}
}

Grid view Desgin

<%--<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GV.aspx.cs" Inherits="MyCodeSnippet.GV1" %>







Sample Grid View











User Details



DataKeyNames="UserId" ForeColor="#333333" GridLines="None" ShowFooter="true"
AllowPaging="True" PageSize="5" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound"
OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="grdview_RowEditing" OnRowCancelingEdit="grdview_RowCancelingEdit" OnDataBound="grdview_DataBound" OnRowCommand="grdview_RowCommand" OnSorting="grdview_Sorting"
>









































Runat="server" ID="delete" onclientclick="return confirm('Are you sure you want to delete this Record?');" />


























Pages:

1
3
5
10
20





runat="server" AlternateText="First Page"

CommandName="Page" CommandArgument="First"

ImageUrl="~/Pics/left2.GIF" />


runat="server" AlternateText="Previous Page"

CommandName="Page" CommandArgument="Prev"

ImageUrl="~/Pics/left.GIF" />



Page 
AutoPostBack="true"

OnSelectedIndexChanged="PageNumberDropDownList_OnSelectedIndexChanged"

runat="server">


 of 
ID="PageCountLabel" runat="server" />




runat="server" AlternateText="Next Page"

CommandName="Page" CommandArgument="Next"

ImageUrl="~/Pics/right.GIF" />


runat="server" AlternateText="Last Page"

CommandName="Page" CommandArgument="Last"

ImageUrl="~/Pics/right2.GIF" />

















--%>