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

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

问题描述

我使用过ASP.NET MVC服务组件。 我想发送的电子邮件在一个异步的方式,让用户做其他的东西,而不必等待发送。

当我发送邮件不带附件的正常工作。 当我发送邮件至少有一个内存连接失败。

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

下面是发送方法

 
    公共静态无效的send(){

    MailMessage消息=新MailMessage(from@foo.com,too@foo.com);
    使用(MemoryStream的流=新的MemoryStream(新字节[64000])){
    附件附件=新的附件(流,我的附件);
    message.Attachments.Add(挂职);
    message.Body =这是一个异步的考验。

    SmtpClient SMTP =新SmtpClient(localhost的);
    smtp.Credentials =新的NetworkCredential(富,酒吧);
    smtp.SendAsync(消息,NULL);
    }
    }
 

下面是我的电流误差

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


解决方案

 公共静态无效发送()
    {

            MailMessage消息=新MailMessage(from@foo.com,to@foo.com);
            MemoryStream的流=新的MemoryStream(新字节[64000]);
            附件附件=新的附件(流,我的附件);
            message.Attachments.Add(挂职);
            message.Body =这是一个异步的考验。
            SmtpClient SMTP =新SmtpClient(localhost的);
            //smtp.Credentials =新的NetworkCredential(登录,密码);

            smtp.SendCompleted + =委托(对象发件人,System.ComponentModel.AsyncCompletedEventArgs E)
            {
                    如果(e.Error!= NULL)
                    {
                            System.Diagnostics.Trace.TraceError(e.Error.ToString());

                    }
                    MailMessage userMessage = e.UserState为MailMessage;
                    如果(userMessage!= NULL)
                    {
                            userMessage.Dispose();
                    }
            };

            smtp.SendAsync(邮件,消息);
    }
 

解决方案

不要使用使用在这里。您调用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天全站免登陆