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

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

问题描述

我正在通过 ASP.NET MVC 使用服务组件.我想以异步方式发送电子邮件,让用户无需等待发送即可做其他事情.

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.

这里是发送方法


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

<小时>

解决方案


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

推荐答案

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

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天全站免登陆