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);
}
}
}
Wednesday, December 30, 2009
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
}
}
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
--%>
--%>
Subscribe to:
Posts (Atom)