Saturday, 31 May 2014

Asp.Net Application Send Mail Sample Code Project using System; using System.Configuration; using System.Data; using System.IO; using System.Text; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Net.Mail; using System.Runtime.InteropServices; using System.Xml; using System.Globalization; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { SqlConnection Oconnection = new SqlConnection(ConfigurationManager.AppSettings["Con"].ToString()); DataTable DTMailTrigger = new DataTable(); string MailTxt = ""; protected void Page_Load(object sender, EventArgs e) { //Session["SGID"] = "N8222335"; //N8222335 MailReports(); } protected void MailReports() { try { SqlCommand cmd = new SqlCommand("USP_PENDING_INVOICE_STATIDCS", Oconnection); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter sqldat = new SqlDataAdapter(cmd); sqldat.Fill(DTMailTrigger); if (DTMailTrigger.Rows.Count > 0) { MailTxt = "
"; MailTxt += " "; MailTxt += " "; MailTxt += " "; MailTxt += "
   "; MailTxt += " PENDING FOR INVOICE STATISTICS-SEKURT INDIA   "; MailTxt += " (Report as on: " + DateTime.Now.ToString("dd-MMM-yyyy hh:mm tt") + ") "; MailTxt += "
"; MailTxt += "
"; MailTxt += " "; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += ""; } for (int i = 0; i < DTMailTrigger.Rows.Count; i++) { MailTxt += " "; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += " "; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += " "; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += " "; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += ""; MailTxt += "
StatusTotalResponsibility
Invoice Pending for OCR" + DTMailTrigger.Rows[0][0].ToString() + "SSC-Team
Invoice Reject" + DTMailTrigger.Rows[0][1].ToString() + "FSG-Team
Invoice Pending For EDIFACT" + DTMailTrigger.Rows[0][2].ToString() + "IT-Team
Invoice Pending For Integration" + DTMailTrigger.Rows[0][3].ToString() + "IT-Team
"; Response.Write(MailTxt); //Details To send a Mail using NET MAIL MailMessage mail = new MailMessage(); mail.Subject = "PENDING INVOICE STATISTICS-SEKURT INDIA LTD.,"; //Subject mail.From = new MailAddress("Nagarajan.R2@saint-gobain.com"); //mail.From = new MailAddress("Pending Invoice Statistics-Sekurt "); //From Address //Send To Mail id string[] strmailTO = { "Nagarajan.R2@saint-gobain.com", "Nagarajan.R2@saint-gobain.com", "nagarajan027@gmail.com" }; //TO Address for (int j = 0; j < strmailTO.Length; j++) { mail.To.Add(strmailTO[j]); } //Send CC Mail id //string[] strmailCC = {""}; //for (int s = 0; s < strmailCC.Length; s++) //{ // mail.CC.Add(strmailCC[s]); //} //Send BCC Mail id string[] strmailBCC = { "Nagarajan.R2@saint-gobain.com" }; //BCC Address for (int k = 0; k < strmailBCC.Length; k++) { // mail.Bcc.Add(strmailBCC[k]); } mail.Body = (MailTxt); mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "10.87.10.11"; smtp.Send(mail); } } catch (SqlException ex) { string errorInfo = ""; errorInfo += ""; errorInfo += "
"; errorInfo += " "; errorInfo += ""; errorInfo += " "; errorInfo += "
IP Address:" + HttpContext.Current.Request.UserHostAddress + "
Error Message:" + ex.ToString() + "
"; errorInfo += "
"; MailMessage mail = new MailMessage(); mail.Subject = "PENDING INVOICE STATISTICS-SEKURT INDIA"; //Subject mail.From = new MailAddress("Pending Invoice Statistics-Sekurt "); //From Address string[] strmailTO = { "Nagarajan.R2@saint-gobain.com" }; //TO Address for (int i = 0; i < strmailTO.Length; i++) { mail.To.Add(strmailTO[i]); } //Send CC Mail id //string[] strmailCC = { "Lavanya.C@saint-gobain.com" }; //for (int s = 0; s < strmailCC.Length; s++) //{ // mail.CC.Add(strmailCC[s]); //} //send BCC mail id Error Message string[] strmailBCC = { "Nagarajan.R2@saint-gobain.com" }; //BCC Address for (int j = 0; j < strmailBCC.Length; j++) { // mail.Bcc.Add(strmailBCC[j]); } mail.Body = (errorInfo); mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "10.87.10.11"; smtp.Send(mail); } } }

No comments:

Post a Comment