在asp.net中使用SMTP.SendAsync是否明智? [英] Is it wise to use SMTP.SendAsync in asp.net

查看:78
本文介绍了在asp.net中使用SMTP.SendAsync是否明智?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码,该代码将2个代码发送到2封不同的电子邮件(以检查这两封电子邮件的所有者是同一个人).我得到了错误:

System.InvalidOperationException:异步调用已在进行中.必须先完成或取消它,然后才能调用此方法.

好吧,我可以简单地避免在第一个电子邮件发送完成后发送第二个电子邮件的错误,但是问题是,如果有那么多用户同时请求发送电子邮件(忘记密码,更改电子邮件),注册...)会引起问题吗?还是仅当它们在同一页面上重复执行时才会出现此错误?

I'm writing a code that sends 2 codes to 2 different emails (to check that owner of both emails are the same person). And I got the error:

System.InvalidOperationException: An asynchronous call is already in progress. It must be completed or canceled before you can call this method.

Well I can simply avoid this error sending the second email after the sending of the first one completes, but the question is that if so many users at the same time request email sending (forgotten password, email change, registration, ...) would it cause problem? or I will get this error only when they are executed repeatedly at the same page?

先谢谢了,
阿什坎

Thanks in advance,
Ashkan

推荐答案

SendAsync方法的目的不是阻止调用线程.但是,必须先等待SendAsync方法完成,然后才能在同一SmtpClient实例上发送新电子邮件.换句话说,创建2个实例,每一封电子邮件一个,然后根据需要异步发送它们.

The purpose of the SendAsync method is not to block the calling thread; however, you must wait for the SendAsync method to finish before you can send a new email on the same SmtpClient instance. In other words, create 2 instances, one for each email, and send them both asynchronously if you want.

从MSDN:

在调用SendAsync之后,您必须等待电子邮件传输到尝试使用发送"发送另一封电子邮件之前完成或SendAsync.

After calling SendAsync, you must wait for the e-mail transmission to complete before attempting to send another e-mail message using Send or SendAsync.

这篇关于在asp.net中使用SMTP.SendAsync是否明智?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆