Click here to Skip to main content
15,892,805 members

sending Email issues using mailmessage in asp.net with c#

avmvijay asked:

Open original thread
Hi,

We have web application, two persons send email at same time but second person body content goes to first person. its only for same time and seconds.

server SMTP and mailmessage used to send.

How to fix this issue?

Code:

public override string SendReplyMail_To_Client(string TICKETNUMBER, string REPLYMESSAGE, int TemplateNumber, int Portal_Id, int Module_Id, string FromEmail, string ToEmail, string Att, string Added_CC, string subject, out string Error, out string ReplyError)
{
Error = "";
ReplyError = "";
if (ToEmail != "" && FromEmail != "")
{
// Added on 20th March 2009
FromEmail = Get_From_Email_Address(FromEmail, TICKETNUMBER, Portal_Id, Module_Id);

ds_ticket = new DataSet();
try
{
ds_ticket = DataProvider.Instance().Get_Ticket_Details_By_Ticket_Number_For_Email_Template(TICKETNUMBER);
}
catch (Exception ex)
{
insert_Log("====>> Error in Get_Ticket_Details_By_Ticket_Number_For_Email_Template: " + ex.Message + " Time : " + DateTime.Now.ToLongTimeString());
}

if (ds_ticket != null)
{

ds_reply_mail = new DataSet();
SqlParam = new SqlParameter[]
{
new SqlParameter("@TemplateNumber",TemplateNumber),
new SqlParameter("@Ticket_Number",TICKETNUMBER),
new SqlParameter("@Portal_Id",Portal_Id),
new SqlParameter("@Module_Id",Module_Id)
};

try
{
ds_reply_mail = DataProvider.Instance().Get_EmailTemplates_By_TemplateNumber(SqlParam);
insert_Log("====>> Reply template Ticket_Number is-: " + TICKETNUMBER);
}
catch (Exception ex)
{
insert_Log("====>> Error in Get_EmailTemplates_By_TemplateNumber: " + ex.Message + " Time : " + DateTime.Now.ToLongTimeString());
}

insert_Log("====>> Reply FromEmail is-: " + FromEmail);
insert_Log("====>> Reply ToEmail is-: " + ToEmail);
insert_Log("====>> Reply CCEmail is-: " + Added_CC);

if (ds_reply_mail.Tables[0].Rows.Count > 0)
{
// From Email Template.....
EmailSubject = ds_reply_mail.Tables[0].Rows[0]["EmailSubject"].ToString().Trim();
EmailText = DataProvider.Instance().Get_String((Byte[])ds_reply_mail.Tables[0].Rows[0]["EmailText"]);


if (EmailText.Contains("||"))
{
EmailText = EmailText.Replace("||", "'");
}



// Email Text Replacement
for (int ColumnIndex = 0; ColumnIndex < ds_ticket.Tables[0].Columns.Count; ColumnIndex++)
{
if (EmailText.Contains(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()))
{
if (ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim() != "#REQUESTDESC#")
{
EmailText = EmailText.Replace(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim(), ds_ticket.Tables[0].Rows[0][ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()].ToString().Trim());
//EmailSubject = EmailSubject.Replace(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim(), ds_ticket.Tables[0].Rows[0][ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()].ToString().Trim());
}
else
{
EmailText = EmailText.Replace(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim(), DataProvider.Instance().Get_String((Byte[])ds_ticket.Tables[0].Rows[0][ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()]));
// EmailSubject = EmailSubject.Replace(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim(), ds_ticket.Tables[0].Rows[0][ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()].ToString().Trim());
}
}
}

// Email Subject Replacement
for (int ColumnIndex = 0; ColumnIndex < ds_ticket.Tables[0].Columns.Count; ColumnIndex++)
{
if (EmailSubject.Contains(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()))
{
if (ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim() != "#REQUESTDESC#")
{
EmailSubject = EmailSubject.Replace(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim(), ds_ticket.Tables[0].Rows[0][ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()].ToString().Trim());
}
else
{
EmailSubject = EmailSubject.Replace(ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim(), ds_ticket.Tables[0].Rows[0][ds_ticket.Tables[0].Columns[ColumnIndex].ColumnName.Trim()].ToString().Trim());
}
}
}


if (EmailText.Contains("#REPLYMESSAGE#"))
{
EmailText = "" + EmailText.Replace("#REPLYMESSAGE#", REPLYMESSAGE) + "";//EmailText.Replace("#REPLYMESSAGE#", REPLYMESSAGE);
}


MailMessage Mail = new MailMessage();
Mail.From = FromEmail;
// added by saravanan
bool success;
success = _imap.UnlockComponent("SInterProBPSIMAPMAIL_X1BXirBD7ZA8");
if (success != true)
{
Error = _imap.LastErrorText;
insert_Log("====>>Error in _imap.UnlockComponent Reply Mail send " + _imap.LastErrorText);
// MessageBox.Show(_imap.LastErrorText);
//return;
}
success = _imap.Connect(Get_SMTP_Host_Server_Name().ToString());
if (success != true)
{
Error = _imap.LastErrorText;
insert_Log("====>>Error in _imap.Connect Reply Mail send " + _imap.LastErrorText);
//MessageBox.Show(_imap.LastErrorText);
//return;
}

string password;

strSql = "select distinct CRM_Email_Password from CCQMP_Rep_Mapping where CRM_Email_Address= '" + FromEmail + "' and Status='A'";
password = DataProvider.Instance().ExecuteScalar(strSql);
success = _imap.Login(FromEmail, password);
if (success != true)
{
Error = _imap.LastErrorText;
insert_Log("====>>Error in _imap.Login Reply Mail send " + _imap.LastErrorText);
//MessageBox.Show(_imap.LastErrorText);
// return;
}

Chilkat.Email _email = new Chilkat.Email();

// added by saravanan
cc = "";

try
{
if (ToEmail.Contains(";"))
{
string[] strarray = ToEmail.Split(';');
if (strarray.Length > 0)
{
Email = "";
for (int i = 0; i < strarray.Length; i++)
{
if (DataProvider.Instance().Is_CRM_Email(strarray[i].ToString(), Portal_Id, Module_Id).Trim() == "No")
{
Email += strarray[i].ToString() + ";";
}
}
}
else
{
Email = string.Empty;
}
cc = Email;

}
else
{
cc = ToEmail;
}

}
catch (Exception ex)
{
insert_Log("====>> Error in Reply send mail CC problem: " + ex.Message + " Time : " + DateTime.Now.ToLongTimeString());
cc = "";
}

Mail.To = cc;

cc = "";
Email = "";
try
{
if (Added_CC.Contains(";"))
{
string[] strarray = Added_CC.Split(';');
if (strarray.Length > 0)
{
Email = "";
for (int i = 0; i < strarray.Length; i++)
{
if (DataProvider.Instance().Is_CRM_Email(strarray[i].ToString(), Portal_Id, Module_Id).Trim() == "No")
{
Email += strarray[i].ToString() + ";";
}
}
}
else
{
Email = string.Empty;
}
cc = Email;

}
else
{
cc = Added_CC;
}

}
catch (Exception ex)
{
insert_Log("====>> Error in Reply send mail CC problem -1: " + ex.Message + " Time : " + DateTime.Now.ToLongTimeString());
cc = "";
}


if (cc.Length > 0)
{
//Mail.Cc = cc;
Mail.Cc = cc;
_email.AddCC("", cc);
}

//// Added on 16th jan 2009

//if (!Mail.To.Contains("@"))
//{
// if (Mail.Cc.Contains("@"))
// {
// Mail.To = Mail.Cc;
// }
//}
//// End on 16th jan 2009
if (subject != null && subject != "")
Mail.Subject = subject;
else
Mail.Subject = EmailSubject;
Mail.BodyFormat = MailFormat.Html;

if (Att.Length > 0)
{
if (Att.Trim() != "")
{
if (Att.Contains(","))
{
strArray = Att.Split(Convert.ToChar(","));
if (strArray.Length > 0)
{

for (int Att_Index = 0; Att_Index < strArray.Length; Att_Index++)
{
if (strArray[Att_Index].ToString().Trim() != "")
{
// Attach the newly created email attachment
MailAttachment attach = new MailAttachment(strArray[Att_Index].ToString());
Mail.Attachments.Add(attach);
_email.AddFileAttachment(strArray[Att_Index].ToString());
}
}
}
}
}
}





Mail.Body = EmailText;
try
{

Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Get_SMTP_Host_Username());
Mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Get_SMTP_Host_Password());

// ds = new DataSet();
// ds = DataProvider.Instance().Get_Server_Config_Details();

SmtpMail.SmtpServer = Get_SMTP_Host_Server_Name();


if (DataProvider.Instance().Is_CRM_Email(ToEmail, Portal_Id, Module_Id).Trim() == "No")
{
_email.SetHtmlBody(Mail.Body);
_email.Subject = subject;
_email.AddTo(ToEmail, ToEmail);
_email.From = FromEmail;
try
{

SmtpMail.Send(Mail);
success = _imap.AppendMail("Sent Items", _email);
insert_Log("====>> Reply Mail send Successfully ");
if (success != true)
{
Error = _imap.LastErrorText;
insert_Log("====>>Error in _imap.AppendMail Reply Mail send " + _imap.LastErrorText);
//MessageBox.Show(_imap.LastErrorText);
// return;
}
}
catch (Exception ex)
{
//Start To Mail
string[] ToEmalId = ToEmail.Split(';');
System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();
foreach (string strID in ToEmalId)
{
string[] straID = strID.Split(',');
foreach (string str in straID)
{
list.Add(str);
}
}
foreach (string toid in list)
{
if (toid != "")
{
if (ex.Message.Contains(toid))
{

//Mail.To = Mail.To.Replace(toid, string.Empty);
ReplyError = "The Email id " + toid + " is Invalid.Please correct and resend again";
}
}
}
//End To Mail

//Start CC Mail
string[] CCEmalId = Added_CC.Split(';');
System.Collections.Generic.List<string> list1 = new System.Collections.Generic.List<string>();
foreach (string strID in CCEmalId)
{
string[] straID1 = strID.Split(',');
foreach (string str in straID1)
{
list1.Add(str);
}
}
foreach (string CCid in list1)
{
if (CCid != "")
{
if (ex.Message.Contains(CCid))
{
//Mail.Cc = Mail.Cc.Replace(CCid, string.Empty);
ReplyError = "The Email id " + CCid + " is Invalid.Please correct and resend again";
}
}

}
//End CC Mail

insert_Log("====>>Error in catch block reply mail to client: " + ex.Message);
}
//textBox1.Text += "Email uploaded to Inbox!" + "\r\n";

//// Disconnect from the IMAP server.
_email.Dispose();
_imap.Disconnect();


/// Testing a mail append in the send item in the crm email
/// Created by saravanan
/// Created On : 18-MAY-2011
/// Using Chilkat .NET

//bool success;
//success = _imap.UnlockComponent("SInterProBPSIMAPMAIL_X1BXirBD7ZA8");
//if (success != true)
//{
// MessageBox.Show(_imap.LastErrorText);
// //return;
//}
//success = _imap.Connect(Get_SMTP_Host_Server_Name().ToString());
//if (success != true)
//{
// MessageBox.Show(_imap.LastErrorText);
// //return;
//}

//string password;

//strSql = "select distinct CRM_Email_Password from CCQMP_Rep_Mapping where CRM_Email_Address= '"+FromEmail+"' and Status='A'";
//password = DataProvider.Instance().ExecuteScalar(strSql);
//success = _imap.Login(FromEmail, password);
//if (success != true)
//{
// MessageBox.Show(_imap.LastErrorText);
//// return;
//}

//Chilkat.Email _email = new Chilkat.Email();

//_email.SetHtmlBody (Mail.Body);
//_email.Subject = EmailSubject;
//_email.AddTo(ToEmail, ToEmail);
//_email.From = FromEmail;


//if (Att.Length > 0)
//{
// if (Att.Trim() != "")
// {
// if (Att.Contains(","))
// {
// strArray = Att.Split(Convert.ToChar(","));
// if (strArray.Length > 0)
// {

// for (int Att_Index = 0; Att_Index < strArray.Length; Att_Index++)
// {
// if (strArray[Att_Index].ToString().Trim() != "")
// {
// _email.AddFileAttachment(strArray[Att_Index].ToString());
// }
// }
// }
// }
// }
//}

//if (cc.Length > 0)
//{
// //Mail.Cc = cc;
// _email.AddCC("", cc);
//}
//success = _imap.AppendMail("Sent Items",_email );

//if (success != true)
//{
// MessageBox.Show(_imap.LastErrorText);
//// return;
//}

////textBox1.Text += "Email uploaded to Inbox!" + "\r\n";

////// Disconnect from the IMAP server.
//_imap.Disconnect();



/// Ended Here Testing a mail append in the send item in the crm email


}
}
catch (Exception ex)
{
insert_Log("====>> Error Message from class: " + ex.Message + " Time : " + DateTime.Now.ToLongTimeString());
//retVal = "Error Message from class: " + e1.Message;
}

}

}
}

return retVal;
}
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900