Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending emails and getting a return sent message 1

Status
Not open for further replies.

saintedmunds

Technical User
Apr 7, 2006
78
GB
Hi

I have an asp.net page that send a HTML email to my database.

This all works great but i only get to know how many I have sent and how many where succesfull when it has gone throuh the database say 200 contacts.

Is there a way to get the page to refresh every time an email is sent so i can see a live count of success or not but to keep the process going in the background?

Any help would be great

Cheers
 
You are going to have to send messages on a New Thread. Not positive this is right, but something like this...

Code:
    Sub sendMail()
        'code that sends mail and updates DB
    End Sub

    'Starts sendMail on a new independant Thread.
    Dim threadSend As New System.Threading.Thread(AddressOf sendMail)
    threadSend.Start()


Senior Software Developer
 
I'm not sure how the threading approach would help actually. It's very useful in a windows app where the client application is interacting with the user, but in a web application the user's interface will only update once you send them the HTML. The new thread will still run the code, but the HTML will never be sent to the client until the full request has completed. You could however use similar logic to perform AJAX requests.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
So, you think that the request will wait for the new thread to complete processing? Could be... I'm not sure I've done that in a web app before.

Your service idea would definitely work though. Probably the better way to go, even if the threading worked.


Senior Software Developer
 
So, you think that the request will wait for the new thread to complete processing?
Yes, I think so (after all, once the thread completes how would it know that it should send the response to the client?). I'll try it out later just to make sure though.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
If you do try it, make sure that the IsBackgroud property is True.

Also, I thought they wanted to watch this in a sort of real-time fashion. So I guess I was thinking that the page (or a portion of the page) would auto refresh every few seconds to requery the data and report the current progress until the job was found to be complete.

Maybe I'm not entirely sure of what they want.


Senior Software Developer
 
I've actually built an emailing web app that uses threading.. works like a charm.. I would recommend the threading over writing a service.. the threading is async and the web page can interact easily with it..

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
in brief, it works something like this:

Code:
// Class that starts and handles the thread
SenderClass sc = new SenderClass();
... 
... set it's properties 
...
// Start the sending thread and finish page load
sc.StartSend();
// Store the class object in a Session variable so we can retrieve values later
Session["mySenderClass"] = sc;

Have a javascript timer to auto-refresh the page after xx seconds/minutes/hours....

then grab the values:

Code:
SenderClass sc = (SenderClass)Session["mySenderClass"];
Label1.Text = sc.SendProgress.ToString();

Now, you can take it one step further and AJAX this baby and make it fully real-time by implementing custom Progress event and update the progress text each time the event fires.

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
Hi

Wow so many replies.
If some one has any time could they explain what threading is and the difference to a service.

Is Gorkem saying this?

I have a seperate Class file that does all the email sending and looping through the database and then returns the results to a session?

// Class that starts and handles the thread
SenderClass sc = new SenderClass();
...
... Loop database here and send?????
...
// Start the sending thread and finish page load
sc.StartSend();
// Store the class object in a Session variable so we can retrieve values later
Session["mySenderClass"] = sc;

So the thread keeps ticking by in the background and then using Javascript I refresh the page and
SenderClass sc = (SenderClass)Session["mySenderClass"];
Label1.Text = sc.SendProgress.ToString();

Show the results?

But if I refresh the page will it not resend the job?


Sorry to sound so naive but never used Threads or services before.

Thank you all so far for your input.
 
You can check this easily.. on every refresh, check to see if the Session["mySenderClass"] == null if it is null, you haven't started sending yet, and you can execute the first part of the code.. if it's not null, then jump to the second part of the code and display the results.

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
A thread is essentially a simple program. A thread can only do one thing at a time in the same way you follow a basic flowchart. Occasionally though, you come across a need to kick off another process flow and run 2 processes side-by-side that are each doing their own thing at the same time.

Thus your need to have the emailing process either somewhat or totally disconnected from your web application's ability to update/return your page. You want the process of emailing to run in the background, while at the same time keeping updated as to the status of that process via the main web application process.

Does that make sense?

Anywho, a Service is another method of accomplishing that same task of passing off a process to another thread because a Service is already a separate application running on its own thread. A Service is always waiting to receive the instruction to start working on the task it was designed for.

It sounds like Gorkem has a solution for internally starting your email processing on a new thread. The service would work as well and both have some benefits and some drawbacks. The service would be more flexible in allowing you to call it from other applications in the future, but it would require that you create a new service application with an installer. The threading keeps all of the code in the same application, but it could only be started/used by your web application.

Either way you go, you will be starting a thread that is sort of invisible as it runs silently in the background... so you will want to log errors to the event viewer or a file of some sort. If you go to the Event Viewer (recommended) make sure that if you create a new event log that you set it up to Overwrite as needed.


Senior Software Developer
 
Sorry for the delay

I have gone down the route of a Thread and it seems to work quite well.

If you would like to see what I came up with let me know and I will post it here.

Thank you all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top