Send Email

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 FidesTech;

public partial class MailSystem_SendEmail : System.Web.UI.Page
{
DBConnection db = new DBConnection();
ErrorHandler err = new ErrorHandler();
Regx reg = new Regx();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserID"] == null)
{
Response.Redirect("~/LoginPage.aspx");
}
if (Request.QueryString["var1"] != null)
{
string Email = Request.QueryString["var1"].ToString();
Txt_AssignTo.Text = FidesTech.clsSecurityImplementation.Decrypt(Email);
TC_Mail.ActiveTab = TC_Mail.Tabs[1];
}
if (Request.QueryString["var"] != null)
{
string Email = Request.QueryString["var"].ToString();
Txt_ReqTo.Text = FidesTech.clsSecurityImplementation.Decrypt(Email);
TC_Mail.ActiveTab = TC_Mail.Tabs[0];
}

ScriptManager.RegisterStartupScript(this, GetType(), "js" + Txt_Subject.ID, "ValidatorHookupEvent(document.getElementById(\"" + Txt_Subject.ClientID + "\"), \"onblur\", \"ValidatorOnChange(event);\");", true);
ScriptManager.RegisterStartupScript(this, GetType(), "js" + Txt_StartDate.ID, "ValidatorHookupEvent(document.getElementById(\"" + Txt_StartDate.ClientID + "\"), \"onblur\", \"ValidatorOnChange(event);\");", true);
ScriptManager.RegisterStartupScript(this, GetType(), "js" + Txt_Fdate.ID, "ValidatorHookupEvent(document.getElementById(\"" + Txt_Fdate.ClientID + "\"), \"onblur\", \"ValidatorOnChange(event);\");", true);
ScriptManager.RegisterStartupScript(this, GetType(), "js" + Txt_MsgSubject.ID, "ValidatorHookupEvent(document.getElementById(\"" + Txt_MsgSubject.ClientID + "\"), \"onblur\", \"ValidatorOnChange(event);\");", true);
//if (!IsPostBack)
//{
// //FillComboBox();
// //FillComboBox1();
//}
//tbl_Show.Visible = false;
}
public void Page_PreInit()
{
this.Theme = Convert.ToString(Session["AppliedSkinName"]);
}

//public void FillComboBox()
//{
// DataSet ds = new DataSet();
// db.fillList("select UserID,CompanyEmail from tbl_usermanagement where UserID Not in ('" + Session["UserID"].ToString() + "')", ref ddl_Receiver, ref ds);
// //"select UserID,CompanyEmail from tbl_usermanagement", ddl_Receiver,"UserID","CompanyEmail" ,ds);
//}
//public void FillComboBox1()
//{
// DataSet ds = new DataSet();
// db.fillList("select UserID,CompanyEmail from tbl_usermanagement where UserID Not in ('" + Session["UserID"].ToString() + "')", ref DDL_AssginTo, ref ds);
// //"select UserID,CompanyEmail from tbl_usermanagement", ddl_Receiver,"UserID","CompanyEmail" ,ds);
//}
protected void Btn_SendEmail_Click(object sender, EventArgs e)
{
try
{
//string info = Txt_ReqTo.Text;
Txt_ReqTo.Text = Txt_ReqTo.Text.TrimEnd(';');
string[] arInfo = Txt_ReqTo.Text.Split(new string[] { ";" } , StringSplitOptions.None);
//int i = arInfo.Length;
string Email = "";
for (int i = 0; arInfo.Length > i; i++)
{
if (arInfo[i].Contains("<") || arInfo[i].Contains(">"))
{
Email = reg.ReturnEmails(arInfo[i]);
}
else
{
Email = arInfo[i];
}
DataTable DT = db.EditToTextBox("select CompanyEmail from tbl_usermanagement where UserID='" + Session["UserID"].ToString() + "'");
db.setData("insert into tbl_assignactivity (SenderID,SenderEmail,ReceiverEmail,ActivitySubject,ActivityDetail,StatusID,Accept,Reject,Completed,SentDate,ChkRead) values ('" + Session["UserID"].ToString() + "','" + DT.Rows[0]["CompanyEmail"].ToString() + "','" + Email.Trim() + "','" + Txt_MsgSubject.Text + " [ Simple Mail ] " + "','" + RE_SendMail.Html + "','2','0','0','0','" + DateTime.Now.ToString("yyyy-MM-dd") + "','1')");
HttpPostedFile myFile = FU_Attachement.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen != 0)
{
string Fname = System.IO.Path.GetFileName(myFile.FileName);
string Dname = System.IO.Path.GetDirectoryName(myFile.FileName);
Response.Write(SendMail(Email.Trim(), DT.Rows[0]["CompanyEmail"].ToString(), "", Txt_MsgSubject.Text, RE_SendMail.Html, Dname + "\\" + Fname));
return;
}

Response.Write(SendMailWithoutAttach(Email.Trim(), DT.Rows[0]["CompanyEmail"].ToString(), "", Txt_MsgSubject.Text, RE_SendMail.Html));

}
}
catch (Exception ex)
{
err.WriteError(ex.Message.ToString());
Response.Redirect("~/ErrorLogFile/ErrorPage.aspx");
}
}

protected void Btn_AssignTask_Click(object sender, EventArgs e)
{
try
{
//string st = DateTime.Now.ToShortDateString();
string str = null;
Txt_AssignTo.Text = Txt_AssignTo.Text.TrimEnd(';');
string[] arInfo = Txt_AssignTo.Text.Split(new string[] { ";" } , StringSplitOptions.None);
//int i = arInfo.Length;
string Email = "";
for (int i = 0; arInfo.Length > i; i++)
{
if (arInfo[i].Contains("<") || arInfo[i].Contains(">"))
{
Email = reg.ReturnEmails(arInfo[i]);
}
else
{
Email = arInfo[i];
}
DataTable DT = db.EditToTextBox("select CompanyEmail from tbl_usermanagement where UserID='" + Session["UserID"].ToString() + "'");
DateTime SDT = Convert.ToDateTime(Txt_StartDate.SelectedDate.ToString());
DateTime EDT = Convert.ToDateTime(Txt_Fdate.SelectedDate.ToString());

//string CPath = Server.MapPath(sFilename);
db.setData("insert into tbl_assignactivity (SenderID,SenderEmail,ReceiverEmail,ActivitySubject,ActivityType,StartDate,EndDate,BillType,ImagePosition,ImageWidth,ImageHeight,ActivityDetail,StatusID,Accept,Reject,Completed,SentDate,ChkRead) values ('" + Session["UserID"].ToString() + "','" + DT.Rows[0]["CompanyEmail"].ToString() + "','" + Email.Trim() + "','" + Txt_Subject.Text + "[Activity Request]" + "','" + DDL_ActivityType.SelectedItem.Text + "','" + SDT.ToString("yyyy-MM-dd") + "','" + EDT.ToString("yyyy-MM-dd") + "','" + DDL_BillType.SelectedItem.Text + "','" + DDL_ImagePosition.SelectedItem.Text + "','" + Txt_Width.Text + "','" + Txt_Height.Text + "','" + Txt_Detail.Html + "','2','0','0','0','" + DateTime.Now.ToString("yyyy-MM-dd") + "','1')");
//Sending Email
str = "

Sender Email:" + DT.Rows[0]["CompanyEmail"].ToString() + "
Receiver Email:" + Email.Trim() + "
Activity Subject:" + Txt_Subject.Text + "
Activity Type:" + DDL_ActivityType.SelectedItem.Text + "
Start Date:" + SDT.ToString("yyyy-MM-dd") + "
Due Date:" + EDT.ToString("yyyy-MM-dd") + "
Bill Type:" + DDL_BillType.SelectedItem.Text + "
Image Position" + DDL_ImagePosition.SelectedItem.Text + "
Width x Height:" + Txt_Width.Text + " x " + Txt_Height.Text + "
Activity Detail:" + Txt_Detail.Html + "
";
HttpPostedFile myFile = FU_TaskAttach.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen != 0)
{
string Fname = System.IO.Path.GetFileName(myFile.FileName);
string Dname = System.IO.Path.GetDirectoryName(myFile.FileName);
Response.Write(SendMail(Email.Trim(), DT.Rows[0]["CompanyEmail"].ToString(), "", Txt_Subject.Text, str, Dname + "\\" + Fname));
return;
}
Response.Write(SendMailWithoutAttach(Email.Trim(), DT.Rows[0]["CompanyEmail"].ToString(), "", Txt_Subject.Text, str));
lblOutput.Text = "Task assigned successfully";
}
}
catch (Exception ex)
{
ErrorHandler err = new ErrorHandler();
err.WriteError(ex.Message.ToString());
}
}

public string SendMail(string toList, string from, string ccList, string subject, string body, string Path)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
Attachment attachement = new Attachment(Path);
message.Attachments.Add(attachement);

// smtpClient.Host = emailServerInfo.MailServerIP;
//smtpClient.Host = "mail.tatiwalaje.com";//Use this if problem
smtpClient.Host = "mail.fides.com.pk";
//smtpClient.Port = Convert.ToInt32(emailServerInfo.MailServerPortNumber);
smtpClient.Port = 2525;
smtpClient.UseDefaultCredentials = true;
//smtpClient.Credentials = new System.Net.NetworkCredential(emailServerInfo.MailServerUserName, emailServerInfo.MailServerPassword);
//smtpClient.Credentials = new System.Net.NetworkCredential("info@tatiwalaje.com", "extraction9c!");use this if problem
smtpClient.Credentials = new System.Net.NetworkCredential("nisara@fides.com.pk", "nisar!@#");
smtpClient.Send(message);
// msg = "Successful";
}
catch (Exception ex)
{
//msg = ex.Message;
}
return msg;
}

public string SendMailWithoutAttach(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
//Attachment attachement = new Attachment(Path);
//message.Attachments.Add(attachement);

// smtpClient.Host = emailServerInfo.MailServerIP;
//smtpClient.Host = "mail.tatiwalaje.com";//Use this if problem
smtpClient.Host = "mail.fides.com.pk";
//smtpClient.Port = Convert.ToInt32(emailServerInfo.MailServerPortNumber);
smtpClient.Port = 2525;
smtpClient.UseDefaultCredentials = true;
//smtpClient.Credentials = new System.Net.NetworkCredential(emailServerInfo.MailServerUserName, emailServerInfo.MailServerPassword);
//smtpClient.Credentials = new System.Net.NetworkCredential("info@tatiwalaje.com", "extraction9c!");use this if problem
smtpClient.Credentials = new System.Net.NetworkCredential("nisara@fides.com.pk", "nisar!@#");
smtpClient.Send(message);
// msg = "Successful";
}
catch (Exception ex)
{
//msg = ex.Message;
}
return msg;
}
}


Attachments

  • Send Email (30066-7251-SendEmail.aspx.cs.txt)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: