Developer404
Developer404

Reputation: 5962

Unable to send mail asynchronously

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?

Upvotes: 1

Views: 332

Answers (3)

Jon Sagara
Jon Sagara

Reputation: 1172

This article may be of some help to you:

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

Upvotes: 0

o.k.w
o.k.w

Reputation: 25790

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.

Upvotes: 0

Rubens Farias
Rubens Farias

Reputation: 57926

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

Upvotes: 1

Related Questions