Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using Thread in Global.asax file to send email.

I have created a table tblNotification - ReceiverEmail,subject,body,pendingMail (false/true)

Also I created a function SendNotification in Global.asax file.

Now when user clicks Submit button, it should send en email reading the SendNotification() using Thread in background to avoid waiting to the user.
On Submit click button, i store user details in tblNotification table and set pendingMail = false. Now in Global.asax file, on Application_BeginRequest(), i create a thread and call SendNotification() in ThreadStart. In SendNotification
(), i change status of pendingMail = true after it sends mail.

It sends email successfully and also user does'nt have to wait for long. But it sends 45 emails to the user with same content.

Is is due to Thread not aborted? or am i creating the thread in wrong event?

Regards,
Sneha
Posted
Updated 30-Sep-11 19:36pm
v2
Comments
CodingLover 1-Oct-11 1:39am    
Can you show us how did you do that?
sgjoshi85 1-Oct-11 1:56am    
Global.asax file --
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Thread td = new Thread(new ThreadStart(SendNotification));
td.IsBackground = true;
td.Start();
}
public static void SendNotification()
{
dbDataContext db = new dbDataContext();
var noti = from n in db.Notificationdbmls
where n.pendingMail == "false"
select n;
if (noti.Count() != 0)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(noti.First().ReceiverEmailID);
mail.From = new MailAddress("emailaddress");
mail.Subject = noti.First().Subject;
mail.Body = noti.First().Body;
mail.IsBodyHtml = true;
var client = new SmtpClient("smtpdetails");
client.Credentials = new System.Net.NetworkCredential("emailid", "password");
client.EnableSsl = true;
client.Send(mail);
noti.First().pendingMail = "true";
db.SubmitChanges();
}
catch (Exception ex)
{
//
}
}
}

On aspx page -

On buttonClick -
Notificationdbml nd = new Notificationdbml { Body = body, CreatedDate = DateTime.UtcNow.Date, Description = "", pendingMail = "false", ReceiverEmailID = email, ReceiverUserID = userid, Subject = subject, Type = "User Registration" };
db.Notificationdbmls.InsertOnSubmit(nd);
db.SubmitChanges();
CodingLover 1-Oct-11 2:40am    
When the following become true?

if (noti.Count() != 0)
sgjoshi85 1-Oct-11 2:44am    
In above SendNotification() code,
after it sends email, immediately, i am changing its status = true;
see the below code -

client.Send(mail);
noti.First().pendingMail = "true";
db.SubmitChanges();
CodingLover 1-Oct-11 4:37am    
You have set the pendngMail as ture, means still available?

1 solution

Here are some suggesions. This may help you to resolve your Problem.

1) You wrote - "Now when user clicks Submit button, it should send en email reading the SendNotification()"

If you want to send Emails on Submit Button Click, Then why to Call SendNotification() method in Application_BeginRequest Event, Instead call it on your "SubmitButton_Click" event.

Remember Application_BeginRequest Event gets execute for every request comming to your Web Application.
http://aspalliance.com/1114_Understanding_the_Globalasax_file.all


2) You wrote -"using Thread in background to avoid waiting to the user."

I would prefer Ajax for this, instead of Threading in Asp.Net.
Simplifying Asp.Net Core Ajax
 
Share this answer
 
Comments
sgjoshi85 1-Oct-11 6:04am    
thanks a lott..RaisKazi :)
your 1) i transfered code in btn Click event..
but running off time..so dont have time to learn on 2nd)
so cant i use WaitCallBack() ??? ThreadPool?
RaisKazi 1-Oct-11 6:14am    
I can't insist but recommend you to use Ajax for this. Just go through the "1) Asp.Net Ajax using JQuery" section of second link, Believe me it is easy and will not take your more than 30 minutes. I think 30 Minutes worth to invest, if you are learning something new and which is fundamentally correct. :)
sgjoshi85 1-Oct-11 6:17am    
okie thanks a lott :) RaisKazi.. I will learn and implement it..and let u know once done successfully..
RaisKazi 1-Oct-11 6:19am    
Perfect!
sgjoshi85 1-Oct-11 6:56am    
hey..thanx buddy :)
this solved!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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