我用SMTPClient.Send(mail)的方法来发送电子邮件,但后来我看到,如果电子邮件ID不存在(不存在),我的应用程序等待,直到它接收到异常,然后允许用户执行其他任务.
所以我想到了使用SMTPClient.SendAsync方法.
我怀疑!! 这个userToken对象在哪里可以作为参数传递给方法?我在网上搜索了很多东西,但没有找到一个很好的例子.即使在MSDN中,他们也会这样使用它
string userState = "test message1";
client.SendAsync(message, userState);
Run Code Online (Sandbox Code Playgroud)
但那么它真正可以用于什么呢?
先感谢您.
当我使用SmtpClient的SendAsync发送电子邮件时,如何smtpclient正确处理实例?
让我们说:
MailMessage mail = new System.Net.Mail.MailMessage()
{
Body = MailBody.ToString(),
IsBodyHtml = true,
From = new MailAddress(FromEmail, FromEmailTitle),
Subject = MailSubject
};
mail.To.Add(new MailAddress(i.Email, ""));
SmtpClient sc = new SmtpClient(SmtpServerAddress);
//Add SendAsyncCallback to SendCompleted
sc.SendCompleted += new SendCompletedEventHandler(SendAsyncCallback);
//using SmtpClient to make async send (Should I pass sc or mail into SendAsyncCallback?)
sc.SendAsync(mail, sc);
Run Code Online (Sandbox Code Playgroud)
在SendAsyncCallback方法中,我应该打电话sc.Dispose(),还是mail.Dispose()?
我检查了MSDN文件,一个例子调用了MailMessage.Dispose(),但是这个dispose方法还会配置SmtpClient实例吗?
非常感谢.
我需要通过控制台应用程序异步发送电子邮件.我需要在回调上做一些数据库更新,但我的应用程序在回调代码运行之前退出!
我怎样才能以一种好的方式阻止这种情况发生,而不是简单地猜测在退出前等待多长时间.我会想象异步调用会被置于某种形式的线程中吗?有可能检查是否还有等待被叫?
示例代码
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) e.UserState;
if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
}
else
{
// update DB
Console.WriteLine("Message sent.");
}
}
public static void Main(string[] args)
{
var users = Repository.GetUsers();
SmtpClient client = new SmtpClient("Host");
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
MailAddress from = new MailAddress("system@domain.com", "System", Encoding.UTF8); …Run Code Online (Sandbox Code Playgroud) 在我的MVC 3 Razor应用程序中,Controller中的ActionResult Create()方法处理用户HttpPost.在那时候:
由于电子邮件是最耗时的活动,我试图使用SmtpClient.SendAsync(),而不是SmtpClient().发送.
如果继承来自AsyncController,这种情况是否可行?谁能提供一个例子?
谢谢,
阿诺德
有什么方法在MVC中使System.Net.Mail SendAsync工作,而不是阻塞发送方法?
我尝试使用它,但点击"启动异步操作的页面必须具有Async属性"错误,我显然无法解决(或者我可以?)因为没有带有@Page指令的ASPX页面我可以添加Async属性.
非常感谢帮助:(
我有一个Web项目(C#,MVC5但没有WebAPI)和一个简单的HTTP REST客户端,它调用外部REST服务并获取accessToken etcing.
我想检查我的所有Get/PostAsync调用对statusCode 401的响应,但我发现我只能SendAsync在实现时覆盖该方法DelegatingHandler.
class CustomDelegatingHandler : DelegatingHandler
{
async protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Run Code Online (Sandbox Code Playgroud)
我还能做些什么,以免改变我所有的异步调用的实现SendAsync吗?
(我真正想要做的是刷新accessToken.)
private void ProcessReceive(SocketAsyncEventArgs e)
{
// Check if the remote host closed the connection.
if (e.BytesTransferred > 0)
{
if (e.SocketError == SocketError.Success)
{
Token token = e.UserToken as Token;
token.SetData(e);
Socket s = token.Connection;
if (s.Available == 0)
{
Boolean willRaiseEvent = false;
// GET DATA TO SEND
byte[] sendBuffer = token.GetRetBuffer();
// this.bufferSize IS SocketAsyncEventArgs buffer SIZE
byte[] tempBuffer = new byte[this.bufferSize];
int offset = 0;
int size = (int)Math.Ceiling((double)sendBuffer.Length / (double)this.bufferSize);
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我正在C#中构建一个应用程序(客户端),它使用Ping.SendAsync(host,3000,null)在本地网络中ping服务器,以验证连接和发送数据.该应用程序将安装在大约200台电脑中.
问题是:
是否安全地每天从这200台PC中每隔5秒ping一次服务器?
提前致谢
在锡上说的是什么.
我正在编写一个代码,将2个代码发送到2个不同的电子邮件(以检查两个电子邮件的所有者是否是同一个人).我收到错误:
System.InvalidOperationException:异步调用已在进行中.必须先完成或取消它才能调用此方法.
好吧,我可以简单地避免在发送第一封电子邮件完成后发送第二封电子邮件时出现此错误,但问题是,如果有这么多用户同时请求发送电子邮件(忘记密码,电子邮件更改,注册......)它会引起问题吗?或者只有在同一页面反复执行时才会出现此错误?
在此先感谢,
Ashkan
应用程序要求使用SendAsync而不仅仅是Send.所以我做了一个类CEmailServer并设置了一切.到目前为止发送工作正常,但更改它以使用SendAsync时,它没有.我创建了一个在发送邮件时调用的方法,并且userToken就位,但它仍然失败.我找不到我的错误.这是我的代码:
static bool mailSent = false;
//Method for Sending with attachment.
public void SendEmail(string Address, string Recipient, string Subject, string Body, string Dir)
{
var fromAddress = new MailAddress("someadress.kimberley@gmail.com", "My Company");
var toAddress = new MailAddress(Address, Recipient);
const string fromPassword = "password";
string subject = Subject;
string body = Body;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message …Run Code Online (Sandbox Code Playgroud) sendasync ×11
c# ×9
smtpclient ×4
asynchronous ×2
smtp ×2
asp.net ×1
dispose ×1
httpclient ×1
ping ×1
sendmail ×1
sockets ×1
winforms ×1