DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

Sending Email Asynchronously

This is a discussion on Sending Email Asynchronously within the .Net Development forums, part of the Programming & Scripting category; ok, so i am about to implement this So the current scenario is, i have to first insert a record ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

  #11  
Old July 2nd, 2009, 08:02 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

ok, so i am about to implement this
So the current scenario is, i have to first insert a record in database, then send mails to all desired people.

So after the insert, i should get all the emails of the people from database and then call this function to send mail??

Secondly, i dont want to anything if the send mail is failed or successful, so do i need this function SmtpClient_OnCompleted?
__________________
Get the Mantra!
Reply With Quote
  #12  
Old July 2nd, 2009, 08:08 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

Yes you would need this function.

Send the emails using the async function and then it'll just send them in the background without waiting for a response...

The Smtp_OnCompleted function is called by the Async sending function.
__________________
Join the folding team
Reply With Quote
  #13  
Old July 2nd, 2009, 08:11 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

ok, will this function Smtp_OnCompleted printout something if process is failed or successful?
Reply With Quote
  #14  
Old July 2nd, 2009, 08:23 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

Quote:
Originally Posted by micky View Post
ok, will this function Smtp_OnCompleted printout something if process is failed or successful?
No, I don't believe so....I can't really remember if I'm honest m...
Reply With Quote
  #15  
Old July 2nd, 2009, 08:25 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

Quote:
Originally Posted by richyrich View Post
No, I don't believe so....I can't really remember if I'm honest m...
ok RR, i'll see what happens
thanx
Reply With Quote
  #16  
Old July 3rd, 2009, 07:33 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

Ok, i am facing this error on red line
Code:
 Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.
My code
Code:
    Public Shared Function SendMailUpdate(ByVal emails As String)
        Dim SmtpMail As New System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings("SmtpServer"), 25)
        Dim myMail As New MailMessage()

        'SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network
        Dim arr_email_to As Array = Split(emails, ";")

        For Each email_to As String In arr_email_to
            myMail.To.Add(New MailAddress(email_to))
        Next

        myMail.From = New MailAddress(ConfigurationManager.AppSettings("NoReply"))
        myMail.Body = ""
        myMail.Subject = "Update"

        Dim mailBox As Object = myMail
        AddHandler SmtpMail.SendCompleted, AddressOf SmtpClient_OnCompleted
        SmtpMail.SendAsync(myMail, mailBox)
    End Function
Function is called from a page like this
Code:
GlobalClasses.SendMailUpdate(strEmailIDs)
Any ideas?

Last edited by micky; July 3rd, 2009 at 08:04 AM.
Reply With Quote
  #17  
Old July 3rd, 2009, 07:41 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

ok added red part in .aspx file of this page in top line and it worked
Code:
<%@ Page Language="VB" Async="true" AutoEventWireup="false"

Last edited by micky; July 3rd, 2009 at 07:57 AM.
Reply With Quote
  #18  
Old July 3rd, 2009, 07:59 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

Do you think these lines in function SendMailUpdate above will send only 1 mail??
Code:
For Each email_to As String In arr_email_to
    myMail.To.Add(New MailAddress(email_to))
Next

Last edited by micky; July 3rd, 2009 at 08:08 AM.
Reply With Quote
  #19  
Old July 3rd, 2009, 08:08 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,347
Blog Entries: 6
Rep Power: 8
richyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to allrichyrich is a name known to all
Default

Quote:
Originally Posted by micky View Post
Do you think these lines in function SendMailUpdate above only will send 1 mail??
Code:
For Each email_to As String In arr_email_to
    myMail.To.Add(New MailAddress(email_to))
Next
It should send a seperate email to each address in the array created by splitting the list of email addresses you pass to the function
Reply With Quote
  #20  
Old July 3rd, 2009, 08:26 AM
micky's Avatar
Lazy Bum
 
Join Date: Jul 2008
Location: India
Posts: 566
Rep Power: 4
micky has a spectacular aura aboutmicky has a spectacular aura aboutmicky has a spectacular aura about
Default

Quote:
Originally Posted by richyrich View Post
It should send a seperate email to each address in the array created by splitting the list of email addresses you pass to the function
Ok, its sometimes sending, sometimes not
Is it a slow process RR, i mean i am receiving mails with some delay??

Also its showing all addresses in TO field in mail!!
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Sending Emails Using ASP.NET richyrich .Net Code Samples 5 July 6th, 2009 04:21 AM
[Email] Sending HTML email... bryceowen PHP Development 3 April 8th, 2009 07:28 PM
Excel and Email Chrissy ASP Development 10 February 16th, 2009 09:11 PM
Sending Email with ASP and CDOSYS jmurrayhead ASP Code Samples 1 November 13th, 2008 09:26 AM
Sending Email using CDOSYS & Classic ASP richyrich ASP Code Samples 0 March 17th, 2008 09:52 AM


All times are GMT -4. The time now is 02:38 AM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2