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; Originally Posted by micky Ok, its sometimes sending, sometimes not Is it a slow process RR, i mean i am ...

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

  #21  
Old July 3rd, 2009, 08:35 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,345
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, 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!!
Oh....whoops....sorry micky....

Just realised that the for each loop is just adding each address to the to property, not sending the email as well....

The delay may be in your mail server...If you still have the whole function I posted, try setting the async flag (check_error, errorCheck or something) in the email function to check for errors and not send asyncronously to see if it makes any difference...
__________________
Join the folding team
Reply With Quote
  #22  
Old July 3rd, 2009, 08:44 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
Oh....whoops....sorry micky....

Just realised that the for each loop is just adding each address to the to property, not sending the email as well....

The delay may be in your mail server...If you still have the whole function I posted, try setting the async flag (check_error, errorCheck or something) in the email function to check for errors and not send asyncronously to see if it makes any difference...
so how do i edit it to not show all addresses in TO field?

I'll try to check without async.
__________________
Get the Mantra!
Reply With Quote
  #23  
Old July 3rd, 2009, 08:50 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,345
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
so how do i edit it to not show all addresses in TO field?

I'll try to check without async.
Try something like:-
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, ";")

        myMail.From = New MailAddress(ConfigurationManager.AppSettings("NoReply"))
        myMail.Body = ""
        myMail.Subject = "Update"
 
        Dim mailBox As Object = myMail
        AddHandler SmtpMail.SendCompleted, AddressOf SmtpClient_OnCompleted

        For Each email_to As String In arr_email_to
            myMail.To.Add(New MailAddress(email_to))
            SmtpMail.SendAsync(myMail, mailBox)
            myMail.Remove(0) 'not sure about the remove method, so might have to check if you use the index or the email address string.
        Next
 
    End Function
Reply With Quote
  #24  
Old July 3rd, 2009, 08:55 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'll try it RR.
But there is no remove property for myMail.
Reply With Quote
  #25  
Old July 3rd, 2009, 09:08 AM
richyrich's Avatar
Administrator
 
Join Date: Mar 2008
Real name: Rich
Location: Somewhere only we know...
Posts: 1,345
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

Sorry micky...Not really thinking straight today...

Try
Code:
For Each email_to As String In arr_email_to
myMail.To = email_to
SmtpMail.SendAsync(myMail, mailBox) 
Reply With Quote
  #26  
Old July 4th, 2009, 03:40 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
Sorry micky...Not really thinking straight today...

Try
Code:
For Each email_to As String In arr_email_to
myMail.To = email_to
SmtpMail.SendAsync(myMail, mailBox) 
If i keep the SmtpMail.SendAsync line in For loop RR, i get this strange error!!
Quote:
System.InvalidOperationException: RegisterForEventValidation can only be called during Render();

Last edited by micky; July 4th, 2009 at 03:44 AM.
Reply With Quote
  #27  
Old July 4th, 2009, 07:52 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 micky View Post
If i keep the SmtpMail.SendAsync line in For loop RR, i get this strange error!!
Workaround for this error was to add this
Code:
<%@ Page Language="VB" Async="true" EnableEventValidation="false" 
But then it says that already a async procedure is running.

So i assume that i cant have that SmtpMail.SendAsync line in For loop.

But it just keep sending all emails in TO field.
I think its the problem with system.net.mail.mailmessage.to, its a collection address

Any ideas anyone?
Reply With Quote
  #28  
Old July 6th, 2009, 03:45 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 made some changes and it seems to be working now.
Its sending mail one by one now.
Thanx RR
Code:
        Dim arr_email_to As Array = Split(emails, ";")

        For Each email_to As String In arr_email_to
            Dim SmtpMail As New SmtpClient(ConfigurationManager.AppSettings("SmtpServer"), 25)
            Dim myMail As New MailMessage()

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

            myMail.To.Add(New MailAddress(email_to))
            SmtpMail.SendAsync(myMail, Nothing)
        Next
Reply With Quote
  #29  
Old July 7th, 2009, 07:52 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Real name: Jason
Location: Washington, D.C.
Posts: 1,964
Blog Entries: 8
Rep Power: 15
jmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud ofjmurrayhead has much to be proud of
Default

Quote:
Originally Posted by micky View Post
Workaround for this error was to add this
Code:
<%@ Page Language="VB" Async="true" EnableEventValidation="false" 
M, my only comment on this thread is regarding disabling Event Validation. Event Validation reduces the risk of unauthorized postbacks and callbacks. I would look into a better solution for this.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

If you like it here...throw us a few bones to help
support us.

Join our Folding team: DeveloperBarn Folding

Reply With Quote
  #30  
Old July 7th, 2009, 08:12 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 jmurrayhead View Post
M, my only comment on this thread is regarding disabling Event Validation. Event Validation reduces the risk of unauthorized postbacks and callbacks. I would look into a better solution for this.
I can remove that.

But ironically, now the code is showing emails in TO field again and also sending mails with different number of recipients to same person.
Darn
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 01:51 AM.


Copyright ©2008-2010, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.2