如何使用SmtpClient.SendAsync发送带有附件的电子邮件? [英] How to send an email with attachments using SmtpClient.SendAsync?

查看:156
本文介绍了如何使用SmtpClient.SendAsync发送带有附件的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过ASP.NET MVC使用服务组件。
我想以异步方式发送电子邮件,让用户进行其他的操作,而无需等待发送。



发送消息时没有附件,它工作正常。
当我发送至少有一个内存中的附件时,它失败。



所以,我想知道是否可以使用异步方法与内存中的附件。



这是发送方法

  
public static void Send(){

MailMessage message = new MailMessage(from@foo.com,too@foo.com);
使用(MemoryStream stream = new MemoryStream(new byte [64000])){
附件附件=新附件(流,我的附件);
message.Attachments.Add(attachment);
message.Body =这是一个异步测试。

SmtpClient smtp = new SmtpClient(localhost);
smtp.Credentials = new NetworkCredential(foo,bar);
smtp.SendAsync(message,null);
}
}

这是我当前的错误

  
System.Net.Mail.SmtpException:发送邮件失败。
---> System.NotSupportedException:Stream不支持阅读。
在System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult)
在System.Net.Mail.Message.EndSend(IAsyncResult asyncResult)
在System.Net.Mail.SmtpClient.SendMessageCallback (IAsyncResult结果)
---内部异常堆栈跟踪结束---






解决方案

  public static void Send()
{

MailMessage message = new MailMessage(from@foo.com,to@foo.com);
MemoryStream stream = new MemoryStream(new byte [64000]);
附件附件=新附件(流,我的附件);
message.Attachments.Add(attachment);
message.Body =这是一个异步测试。
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);
}


解决方案

不要使用 这里。 SendAsync之后,您正在摧毁内存流,例如可能之前SMTP才能读取它(因为它是异步)。在回调中销毁您的流。


I am using a service component through ASP.NET MVC. I would like to send the email in a asynchronous way to let the user do other stuff without having to wait for the sending.

When I send a message without attachments it works fine. When I send a message with at least one in-memory attachment it fails.

So, I would like to know if it is possible to use an async method with in-memory attachments.

Here is the sending method


    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);
        }
    }

Here is my current error


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 ---


Solution

    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);
    }

解决方案

Don't use "using" here. You are destroying the memory stream immediately after calling SendAsync, e.g. probably before SMTP gets to read it (since it's async). Destroy your stream in the callback.

这篇关于如何使用SmtpClient.SendAsync发送带有附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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