简体   繁体   English

SmtpClient.SendAsync()不再起作用

[英]SmtpClient.SendAsync() does not work anymore

I have recently purchased a new computer, and now my e-mails never get sent, and there are NEVER any exceptions thrown or anything. 我最近购买了一台新计算机,现在我的电子邮件再也没有发送过,也绝不会引发任何异常。

Can somebody please provide some samples that work using the SmtpClient class? 有人可以提供一些使用SmtpClient类的示例吗? Any help at all will be greatly appreciated. 任何帮助将不胜感激。

Thank you 谢谢

Updates 更新


Ok - I have added credentials now. 好的-我现在添加了凭据。 And can SUCCESSFULLY SEND e-mail synchronously. 并且可以同步成功发送电子邮件。 But I can still not send them asynchronously. 但是我仍然不能异步发送它们。

Old: After trying to send e-mail synchronously, I receive the following exception: 旧:尝试同步发送电子邮件后,我收到以下异常:

Transaction failed. 交易失败。 The server response was: 服务器响应为:

5.7.1 <myfriend@hotmails.com> : Relay access denied. 5.7.1 <myfriend@hotmails.com> :中继访问被拒绝。

You can send mail through Async(). 您可以通过Async()发送邮件。 How means you should follow the below code, 您应如何遵循以下代码,

smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
smtpClient.SendAsync(mailMessage, mailMessage);

and, if you are using async, you need to also have the event handler like, 并且,如果您使用的是异步操作,则还需要使用类似的事件处理程序,

static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        //to be implemented        
    }

By using this, you can send Mail. 通过使用此,您可以发送邮件。

You could first try the synchronous Send method to verify that everything is setup correctly with the SMTP server and that you don't get any exceptions: 您可以先尝试使用同步Send方法来验证是否已使用SMTP服务器正确设置了所有设置,并且没有任何异常:

var client = new SmtpClient("smtp.somehost.com");
var message = new MailMessage();
message.From = new MailAddress("from@somehost.com");
message.To.Add(new MailAddress("to@somehost.com"));
message.Subject = "test";
message.Body = "test body";
client.Send(message);

Remark: In .NET 4 SmtpClient implements IDisposable so make sure you wrap it in a using statement. 备注:在.NET 4中, SmtpClient实现IDisposable,因此请确保将其包装在using语句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM