从asp.net网站发送邮件 [英] Sending Mail from asp.net website
问题描述
大家好,
我在asp.net中建立了一个网站,其中有一段代码可以发送邮件...当一个用户发送邮件时,该邮件可以正常工作,但是当另一个用户尝试发送邮件时,第一个用户发送的邮件就是反映给第二个用户也...
这是我的代码:
Hello everyone,
I have made a website in asp.net , in which there is a piece of code for sending mails... the mails works perfectly when one user sends it but when another user tries to send the mail , the mail which first user sends is reflected for the second user also...
Here is my code :
private static string mailbody="";
private void BulkMailCommand()
{
try {
string htmlBody = "";
mailbody = "";
htmlBody += "</br>";
htmlBody += "</br>";
htmlBody += "<table style=\"font-family: Calibri\" cellspacing=\"0\" cellpadding=\"2\" border=\"1\">";
htmlBody += "<tr align=center border=\"1\">";
htmlBody += "<td border=\"1\"><b>Sr.No.</b></td>";
htmlBody += "<td border=\"1\"><b>Complaint Number</b></td>";
htmlBody += "<td border=\"1\"><b>Product Type</b></td>";
htmlBody += "<td border=\"1\"><b>School Name</b></td>";
htmlBody += "<td border=\"1\"><b>Complainant Name</b></td>";
htmlBody += "<td border=\"1\"><b>State</b></td>";
htmlBody += "<td border=\"1\"><b>Region</b></td>";
htmlBody += "<td border=\"1\"><b>Contact Number</b></td>";
htmlBody += "<td border=\"1\"><b>Complaint Description</b></td>";
htmlBody += "<td border=\"1\"><b>Date Of Closure</b></td>";
htmlBody += "<td border=\"1\"><b>Number Of Days</b></td>";
htmlBody += "<td border=\"1\"><b>Engineers Name</b></td>";
htmlBody += "<td border=\"1\"><b>Complaint Status</b></td>";
htmlBody += "<td border=\"1\"><b>Remarks</b></td>";
htmlBody += "</tr>";
int i = 0;
int tempComplaintId = 0;
foreach (GridViewRow gr in gdvComplaints.Rows)
{
CheckBox chk = (CheckBox)gr.FindControl("chkSelect");
if (chk.Checked)
{
if (tempComplaintId != Convert.ToInt32(chk.ToolTip))
{
i++;
tempComplaintId = Convert.ToInt32(chk.ToolTip);
sq = new SQLDataAccessHelper();
DataSet ds = new DataSet();
SqlParameter[] param = new SqlParameter[1];
param[0] = new SqlParameter("@complaintid", SqlDbType.Int);
param[0].Value = Convert.ToInt32(chk.ToolTip);
ds = sq.ExecuteDataSet("sp_FillComplaintsWithSBU", param);
htmlBody += "<tr align=center border=\"1\" >";
htmlBody += "<td border=\"1\">" + i.ToString() + "</td>";
htmlBody += "<td border=\"1\">" + ds.Tables[0].Rows[0]["ComplaintNumber"].ToString() + "</td>";
complaintnumbers += ds.Tables[0].Rows[0]["ComplaintNumber"].ToString() + ",";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["Asset"].ToString() + "</td>";
if (ds.Tables[0].Rows[0]["SchoolName"].ToString().Contains("Others"))
{
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["EmployeeComplaint"].ToString() + "</td>";
}
else
{
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["SchoolName"].ToString() + "</td>";
}
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["ComplainantName"].ToString() + "</td>";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["StateName"].ToString() + "</td>";
if (ds.Tables[0].Rows[0]["City"].ToString().Contains("Others"))
{
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["EmployeeComplaintCity"].ToString() + "</td>";
}
else
{
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["City"].ToString() + "</td>";
}
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["ComplainantContactNumber"].ToString() + "</td>";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["ComplaintDescription"].ToString() + "</td>";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["VarDateOfClosure"].ToString() + "</td>";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["NumberOfDays"].ToString() + "</td>";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["EngineersName"].ToString() + "</td>";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["ComplaintStatus"].ToString() + "</td>";
htmlBody += "<td border=\"1\" >" + ds.Tables[0].Rows[0]["Remarks"].ToString() + "</td>";
htmlBody += "</tr>";
}
}
}
htmlBody += "</table>";
htmlBody += "</br>";
htmlBody += "</br>";
htmlBody += "<table>";
htmlBody += "<tr><td></td></tr>";
htmlBody += "<tr><td></td></tr>";
htmlBody += "<tr><td><b>Regards,</b></td></tr>";
htmlBody += "<tr><td><b>Help Desk Team</b></td></tr>";
htmlBody += "<tr><td><b>Mexus Education Pvt. Ltd.</b></td></tr>";
htmlBody += "<tr><td><b>1800-300-12244</b></td></tr>";
htmlBody += "</table>";
mailbody = htmlBody;
}
catch { }
finally { }
}
private void SendMail()
{
try
{
MailMessage mymessage = new MailMessage();
mymessage.From = new MailAddress(ConfigurationManager.AppSettings["Fromid"].ToString());
string toids = txtMailTo.Text.ToString();
string ccids = txtMailCC.Text.ToString();
string bccids = txtMailBCC.Text.ToString();
string[] mailsto = toids.Split(',');
string[] mailscc = ccids.Split(',');
string[] mailsbcc = bccids.Split(',');
for (int i = 0; i < mailsto.Length; i++)
{
mymessage.To.Add(mailsto[i].ToString());
}
for (int j = 0; j < mailscc.Length; j++)
{
if (mailscc[j].ToString() != ""&& mailscc[j].Length !=0)
{
mymessage.CC.Add(mailscc[j].ToString());
}
}
for (int k = 0; k < mailsbcc.Length; k++)
{
if (mailsbcc[k].ToString() != "" && mailsbcc [k].Length !=0)
{
mymessage.Bcc.Add(mailsbcc[k].ToString());
}
}
string textbody = "";
textbody += "<table>";
textbody += "<tr><td>";
textbody += txtMailBody.Text.ToString().Replace("\n", "<br/>");
textbody += "</td></tr>";
textbody += "</table><br/>";
string finalmailbody = textbody + mailbody;
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(
finalmailbody, null, MediaTypeNames.Text.Html);
// mailbody += txtMailBody.Text.ToString();
mymessage.Body = finalmailbody.ToString();
//mymessage.Subject = "Complaints on :- "+DateTime.Today.Day.ToString ()+"-"+DateTime.Today.Month.ToString ()+"-"+DateTime.Today.Year.ToString ();
mymessage.Subject = txtSubject.Text.ToString();
mymessage.AlternateViews.Add(htmlView);
SmtpClient emailClient = new SmtpClient("mail.bhpl.in");
emailClient.Send(mymessage);
InsertMailedComplaintsInDB();
lbMessage.Text = "Complaints mailed succesfully !!";
lbMessage.ForeColor = System.Drawing.Color.Green;
ResetCommand();
}
catch (Exception ex) { throw ex; }
finally { }
}
What happens here is when user 1 select complaints for a particular region from Gridview , and on the same time user 2 selects complaints for another region.. the one who clicked last for mailing complaints is reflected in both the person sending mails...
请帮忙.
问候,
Krunal
What happens here is when user 1 select complaints for a particular region from Gridview , and on the same time user 2 selects complaints for another region.. the one who clicked last for mailing complaints is reflected in both the person sending mails...
Please Help.
Regards,
Krunal
推荐答案
The problem is simple: you are storing the message body in astatic
string:
The problem is simple: you are storing the message body in astatic
string:
private static string mailbody="";
private void BulkMailCommand()
{
try {
string htmlBody = "";
That means that there is a single instance of the string shared across all threads using the class. While this can''t be reliably used as a communications medium between sessions (for reasons I won''t go into here) it can (and in your case is) interfering between users.
Change from static
to class instance and the problem should go away.
That means that there is a single instance of the string shared across all threads using the class. While this can''t be reliably used as a communications medium between sessions (for reasons I won''t go into here) it can (and in your case is) interfering between users.
Change from static
to class instance and the problem should go away.
这篇关于从asp.net网站发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!