0

I'm using a separate class and unique method for sending mail.All my web pages, will call the method to send the mail. But, I'm using Client.SendAsync() to send the mail. The following error occurs while sending the mail asynchronously.

"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"

I set Async=true in @page directive, but, as I'm using separate class, so no use of it. Is there any other way to overcome this problem?

Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
Developer404
  • 5,716
  • 16
  • 64
  • 102

3 Answers3

1

Seems you just need to start your async task before PreRenderComplete event; do you mind to post some relevant ASP.NET code?

Also, read this: Running an asynchronous operation triggered by an ASP.NET web page request

Community
  • 1
  • 1
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
0

Setting Async to true is OK if that separated class is declared, instantiated and within the context of the page request.

However you probably need to handle SendCompleted event.

See the sample codes in this MSDN Reference.

o.k.w
  • 25,490
  • 6
  • 66
  • 63
0

This article may be of some help to you:

Fire and Forget Email, Webservices and More in ASP.NET

Jon Sagara
  • 1,122
  • 2
  • 11
  • 16