如何使用SmtpClient.SendAsync发送带附件的电子邮件?

时间:2008-11-11 03:49:51

标签: .net asp.net asp.net-mvc email

我通过ASP.NET MVC使用服务组件。 我想以异步方式发送电子邮件,让用户做其他事情,而不必等待发送。

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

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

这是发送方法


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

3 个答案:

答案 0 :(得分:34)

此处不要使用“使用”。您在调用SendAsync后立即销毁内存流,例如可能在SMTP开始阅读之前(因为它是异步的)。在回调中销毁你的流。

答案 1 :(得分:0)

我已尝试过您的功能,即使对于内存附件中的电子邮件也适用。但这里有一些评论:

  • 您尝试发送什么类型的附件? Exe?
  • 发件人和收件人都在同一个电子邮件服务器上吗?
  • 你应该“抓住”异常而不是吞下它,而不是你会得到更多关于你的问题的信息。
  • 例外是什么?

  • 使用Send而不是SendAsync是否有效?您正在使用'using'子句并在发送电子邮件之前关闭Stream。

以下是关于此主题的优秀文字:

Sending Mail in .NET 2.0

答案 2 :(得分:0)

原始问题中提供的解决方案的扩展也可以正确清理可能还需要处理的附件。

    public event EventHandler EmailSendCancelled = delegate { };

    public event EventHandler EmailSendFailure = delegate { };

    public event EventHandler EmailSendSuccess = delegate { };
    ...

        MemoryStream mem = new MemoryStream();
        try
        {
            thisReport.ExportToPdf(mem);

            // Create a new attachment and put the PDF report into it.
            mem.Seek(0, System.IO.SeekOrigin.Begin);
            //Attachment att = new Attachment(mem, "MyOutputFileName.pdf", "application/pdf");
            Attachment messageAttachment = new Attachment(mem, thisReportName, "application/pdf");

            // Create a new message and attach the PDF report to it.
            MailMessage message = new MailMessage();
            message.Attachments.Add(messageAttachment);

            // Specify sender and recipient options for the e-mail message.
            message.From = new MailAddress(NOES.Properties.Settings.Default.FromEmailAddress, NOES.Properties.Settings.Default.FromEmailName);
            message.To.Add(new MailAddress(toEmailAddress, NOES.Properties.Settings.Default.ToEmailName));

            // Specify other e-mail options.
            //mail.Subject = thisReport.ExportOptions.Email.Subject;
            message.Subject = subject;
            message.Body = body;

            // Send the e-mail message via the specified SMTP server.
            SmtpClient smtp = new SmtpClient();
            smtp.SendCompleted += SmtpSendCompleted;
            smtp.SendAsync(message, message);
        }
        catch (Exception)
        {
            if (mem != null)
            {
                mem.Dispose();
                mem.Close();
            }
            throw;
        }
    }

    private void SmtpSendCompleted(object sender, AsyncCompletedEventArgs e)
    {
        var message = e.UserState as MailMessage;
        if (message != null)
        {
            foreach (var attachment in message.Attachments)
            {
                if (attachment != null)
                {
                    attachment.Dispose();
                }
            }
            message.Dispose();
        }
        if (e.Cancelled)
            EmailSendCancelled?.Invoke(this, EventArgs.Empty);
        else if (e.Error != null)
        {
            EmailSendFailure?.Invoke(this, EventArgs.Empty);
            throw e.Error;
        }
        else
            EmailSendSuccess?.Invoke(this, EventArgs.Empty);
    }