对于asp.net–有没有办法使用System.Net.Mail.SendAsync()捕获异常感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍sniffer捕获http数据包,并为您提供关于.
对于asp.net – 有没有办法使用System.Net.Mail.SendAsync()捕获异常感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍sniffer捕获http数据包,并为您提供关于.net – 如何使用SmtpClient.SendAsync发送带有附件的电子邮件?、ASP.NET MVC 用户管理器 SendEmailAsync:如何更改发件人电子邮件、asp.net – .Net System.Mail.Message添加多个“To”地址、asp.net – Async / Await和AsyncController?的有用信息。
本文目录一览:- asp.net – 有没有办法使用System.Net.Mail.SendAsync()捕获异常(sniffer捕获http数据包)
- .net – 如何使用SmtpClient.SendAsync发送带有附件的电子邮件?
- ASP.NET MVC 用户管理器 SendEmailAsync:如何更改发件人电子邮件
- asp.net – .Net System.Mail.Message添加多个“To”地址
- asp.net – Async / Await和AsyncController?
asp.net – 有没有办法使用System.Net.Mail.SendAsync()捕获异常(sniffer捕获http数据包)
如果电子邮件失败,我使用这个相当标准的代码:
static void CheckExceptionAndResend(SmtpFailedRecipientsException ex,SmtpClient client,MailMessage message) { for (int i = 0; i < ex.InnerExceptions.Length -1; i++) { var status = ex.InnerExceptions[i].StatusCode; if (status == SmtpStatusCode.MailBoxBusy || status == SmtpStatusCode.MailBoxUnavailable || status == SmtpStatusCode.TransactionFailed) { System.Threading.Thread.Sleep(3000); client.Send(message); } } }
但是,我正在尝试使用SendAsync()实现相同的目标.这是我到目前为止的代码:
public static void SendAsync(this MailMessage message) { message.ThrowNull("message"); var client = new SmtpClient(); // Set the methods that is called once the event ends client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); // Unique identifier for this send operation string userState = Guid.NewGuid().ToString(); client.SendAsync(message,userState); // Clean up message.dispose(); } static void SendCompletedCallback(object sender,AsyncCompletedEventArgs e) { // Get the unique identifier for this operation. String token = (string)e.UserState; if (e.Error.IsNotNull()) { // Do somtheing } }
问题是使用令牌和/或e.Error如何获取异常,以便我可以对StatusCode进行必要的检查然后重新发送?
我整个下午一直在谷歌搜索,但没有找到任何积极的东西.
任何建议表示赞赏
解决方法
更新:
检查Exception是否为SmtpException类型,如果是,则可以查询StatusCode.就像是
if(e.Exception is SmtpException) { SmtpStatusCode code = ((SmtpException)(e.Exception)).StatusCode; //and go from here... }
和check here了解更多细节.
.net – 如何使用SmtpClient.SendAsync发送带有附件的电子邮件?
我想以异步方式发送电子邮件,让用户做其他的东西,而不必等待发送。
当我发送消息没有附件它工作正常。
当我发送包含至少一个内存中附件的消息时,它会失败。
所以,我想知道是否可以使用异步方法与内存中的附件。
这里是发送方法
public static void Send() { MailMessage message = new MailMessage("from@foo.com","too@foo.com"); using (MemoryStream stream = new MemoryStream(new byte[64000])) { Attachment attachment = new Attachment(stream,"my attachment"); message.Attachments.Add(attachment); message.Body = "This is an async test."; SmtpClient smtp = new SmtpClient("localhost"); smtp.Credentials = new NetworkCredential("foo","bar"); smtp.SendAsync(message,null); } }
这是我当前的错误
System.Net.Mail.SmtpException: Failure sending mail. ---> System.NotSupportedException: Stream does not support reading. at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result) --- End of inner exception stack trace ---
解
public static void Send() { MailMessage message = new MailMessage("from@foo.com","to@foo.com"); MemoryStream stream = new MemoryStream(new byte[64000]); Attachment attachment = new Attachment(stream,"my attachment"); message.Attachments.Add(attachment); message.Body = "This is an async test."; SmtpClient smtp = new SmtpClient("localhost"); //smtp.Credentials = new NetworkCredential("login","password"); smtp.SendCompleted += delegate(object sender,System.ComponentModel.AsyncCompletedEventArgs e) { if (e.Error != null) { System.Diagnostics.Trace.TraceError(e.Error.ToString()); } MailMessage userMessage = e.UserState as MailMessage; if (userMessage != null) { userMessage.dispose(); } }; smtp.SendAsync(message,message); }
解决方法
ASP.NET MVC 用户管理器 SendEmailAsync:如何更改发件人电子邮件
如何解决ASP.NET MVC 用户管理器 SendEmailAsync:如何更改发件人电子邮件?
我将 ASP.NET MVC5 与 Identity 2 一起使用,有一个名为 IdentityConfig.cs 的文件,其中包含我已实现的 EmailServive:
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
using (var client = new SmtpClient())
{
using (var mailMessage = new MailMessage("info@mydomain.com",message.Destination,message.Subject,message.Body))
{
mailMessage.IsBodyHtml = true;
await client.SendMailAsync(mailMessage);
}
}
}
}
通过此设置,我可以通过在 UserManager 上调用此方法来发送用户电子邮件:
await UserManager.SendEmailAsync(user.Id,emailSubject,emailBody);
到目前为止一切顺利,但我希望根据主题从不同的发件人发送电子邮件。例如,账户注册/重置密码的发件人为account@mydomain.com,信息查询邮件的发件人为info@mydomain.com,销售/下单的发件人为sales@mydomain.com。
不幸的是,方法 SendEmailAsync 无法设置发件人,我也不知道如何在 EmailService 中实现这一点。任何人都可以帮助解决这个问题吗?有没有办法向 UserManager 或 EmailSerivce 添加扩展方法,以便我可以选择指定不同的发件人?
解决方法
如果我在你的位置上,我会将我所有的电子邮件放在 web.config 文件中,并在返回相关电子邮件的 EmailService 类中的私有方法中访问它基于主题,然后在电子邮件参数的位置调用此方法。例如:
public async Task SendAsync(IdentityMessage message)
{
using (var client = new SmtpClient())
{
//calling getEmail() instead of email
using (var mailMessage = new MailMessage(getEmail(message.Subject),message.Destination,message.Subject,message.Body))
{
mailMessage.IsBodyHtml = true;
await client.SendMailAsync(mailMessage);
}
}
}
private string getEmail(string subject)
{
var emails = ConfigurationManager.AppSettings["Emails"];
string[] emailAddresses = emails.Split('','');
//your logic here
return email;
}
asp.net – .Net System.Mail.Message添加多个“To”地址
因此,我不是采取适当的步骤在受控的环境中重现问题,而是依赖于用户信息和“它在我的机器上工作”的心态。
好的提醒我自己和任何人在那里有时是一个白痴。
我只是碰到我认为不一致的东西,想看看我做错了什么,如果我是个白痴,或者…
MailMessage msg = new MailMessage(); msg.To.Add("person1@domain.com"); msg.To.Add("person2@domain.com"); msg.To.Add("person3@domain.com"); msg.To.Add("person4@domain.com");
真的只发送这封电子邮件给1个人,最后一个。
要添加多个,我必须这样做:
msg.To.Add("person1@domain.com,person2@domain.com,person3@domain.com,person4@domain.com");
我不明白。我以为我要添加多个人到地址集合,但我正在做的是替换它。
我想我只是意识到我的错误 – 添加一个项目到集合,使用
.To.Add(new MailAddress(“person@domain.com”))
如果你只使用一个字符串,它会取代它在它的列表中的一切。
编辑:其他人已经测试,没有看到这种行为。这是我的特定版本的框架中的错误,或者更可能是我的白痴操作。
啊。我认为这是一个相当大的骗子!因为我回答了我自己的问题,但我认为这是有价值的在stackoverflow存档,我仍然会问。也许有人甚至有一个想法,你可以陷入其他陷阱。
解决方法
var message = new MailMessage(); message.To.Add("user@example.com"); message.To.Add("user2@example.com"); message.From = new MailAddress("test@example.com"); message.Subject = "Test"; message.Body = "Test"; var client = new SmtpClient("localhost",25); client.Send(message);
倾销的内容To:MailAddressCollection:
MailAddressCollection (2 items)
displayName User Host Addressuser example.com user@example.com
user2 example.com user2@example.com
而由此产生的电子邮件被抓住smtp4dev:
Received: from mycomputername (mycomputername [127.0.0.1]) by localhost (Eric Daugherty's C# Email Server) 3/8/2010 12:50:28 PM MIME-Version: 1.0 From: test@example.com To: user@example.com,user2@example.com Date: 8 Mar 2010 12:50:28 -0800 Subject: Test Content-Type: text/plain; charset=us-ascii Content-transfer-encoding: quoted-printable Test
您确定您的代码或SMTP服务器没有发生其他问题吗?
asp.net – Async / Await和AsyncController?
解决方法
Provided for backward compatibility with ASP.NET MVC 3.
这个类本身是空的.
换句话说,你不需要它.
今天的关于asp.net – 有没有办法使用System.Net.Mail.SendAsync()捕获异常和sniffer捕获http数据包的分享已经结束,谢谢您的关注,如果想了解更多关于.net – 如何使用SmtpClient.SendAsync发送带有附件的电子邮件?、ASP.NET MVC 用户管理器 SendEmailAsync:如何更改发件人电子邮件、asp.net – .Net System.Mail.Message添加多个“To”地址、asp.net – Async / Await和AsyncController?的相关知识,请在本站进行查询。
本文标签: