2017-01-23 24 views
0

これは私が電子メールを送信する必要があるときに私にエラーを与えます。私はMVC以来蓄積してきたとページのすなわち領域を追跡するためにクラスを使用しているsmtpでメールを送信するSendAsync

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

:しかし、以来、私を与え間違いはこれです。私がSendAsyncを使用した理由は、電子メールなどを送信するのに少し早くなるからです。

このエラーは、ユーザーに電子メールを送信しようとしたときにのみ発生します。私はこのようにやってみました

public static void NewPassword(string mail, string name, string password) 
    { 
     MailDefinition oMailDefinition = new MailDefinition(); 
     oMailDefinition.BodyFileName = "~/MailList/emailskabelon/NewPassword.html"; 
     oMailDefinition.From = FromMail; 

     Dictionary<string, string> oReplacements = new Dictionary<string, string>(); 
     oReplacements.Add("<<navn>>", name); 
     oReplacements.Add("<<password>>", password); 

     System.Net.Mail.MailMessage oMailMessage = oMailDefinition.CreateMailMessage(mail, oReplacements, new LiteralControl()); 
     oMailMessage.Subject = NewpasswordTitle + WebsiteName; 
     oMailMessage.IsBodyHtml = true; 

     SmtpClient smtp = new SmtpClient(AzureApi); 
     System.Net.NetworkCredential netcred = new System.Net.NetworkCredential(AzureName, AzurePassword); 
     smtp.UseDefaultCredentials = false; 
     smtp.EnableSsl = true; 

     smtp.Credentials = netcred; 
     smtp.Port = Convert.ToInt32("25"); 
     smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 

     using (var smtpClient = new SmtpClient()) 
     { 
      smtp.SendAsync(oMailMessage, null); 
     } 
    } 

public static async NewPassword(string mail, string name, string password) 
     { 
      .... 
      using (var smtpClient = new SmtpClient()) 
      { 
       await smtp.SendAsync(oMailMessage, null); 
      } 

私はここを参照してくださいしている:https://stackoverflow.com/a/35212320/7391454

+0

私が理解しているように、非同期メソッドは戻り値の型を必要とします。 – Takarii

答えて

1
にあなたの方法を変更し

public async Task SendEmail(string toEmailAddress, string emailSubject, string emailMessage) 
{ 
var message = new MailMessage(); 
message.To.Add(toEmailAddress); 

message.Subject = emailSubject; 
message.Body = emailMessage; 

using (var smtpClient = new SmtpClient()) 
{ 
    await smtpClient.SendMailAsync(message); 
} 
} 

など、それを呼び出す:

var task = SendEmail(toEmailAddress, emailSubject, emailMessage); 
var result = task.WaitAndUnwrapException(); 

が本当にあなたの元の質問を答える未こちらをご覧Asynchronously sending Emails in C#? 、ここHow to call asynchronous method from synchronous method in C#?

+0

エラーが発生しました:** 'SendAsync'メソッドのオーバーロードが1つの引数をとる** – Jasper

+0

SendAsynchをどこで使用していますか? –

+0

Uはここにあります:[link](http://imgur.com/a/6naaF) – Jasper

0

また、別のスレッド内でごasyncオプションを定義しようとすることができます。 あなたのページには既に非同期タグが挿入されていると思います。 そして、すべてが問題なければ、コードを下のブロックに入れてみてください。

this.Page.RegisterAsyncTask(new PageAsyncTask(async ctoken => { 
     var result = await SomeOperationAsync(ctoken); 
     // result operations. 
})); 
+0

あなたが書いたものと比べて、あなたがそれをやったやり方と自分のコードに挿入する方法を構築するだけではないことを理解します。 – Jasper

0

を持っていますしかし、ちょうどあなたがより良いことを強調したい呼び出し元のスレッドを待機させずにコードを送信する電子メールを呼び出す。 async/awaitを使用していますが、ユーザーの観点から見ると、サーバーが電子メールを送信している間はブラウザーで待機しています。それは数ミリ秒かもしれませんが、バックグラウンドワーカーがこれを処理させる方が良いです。

したがって、HostingEnvironment.QueueBackgroundWorkItem(x=> SendEmail());を使用すると、より良いアプローチになります。

しかし、アプリドメインが途中でリサイクルされると、非同期タスクが終了するというわずかなリスクがあります。しかし、あなたの場合はそうは思わないでしょう。そのようなことが起こったとしても、取り消しトークンを使用して回避トークンを使用することができます。

関連する問題