简体   繁体   English

为什么客户端不处理SMTP邮件附件?

[英]Why Doesn't SMTP Mail Attachment Dispose When Client Does?

This is an issue that I recently solved, but was wondering if somebody could help clear this up for me since I don't fully understand what exactly happened. 这是我最近解决的一个问题,但我想知道是否有人可以帮助我解决此问题,因为我不完全了解发生了什么。 I created a simple smtp email client and wrapped it in a using statement to dispose of it. 我创建了一个简单的smtp电子邮件客户端,并将其包装在using语句中以进行处理。 Inside the email I added a PDF file attachment which I had generated on the fly. 在电子邮件中,我添加了即时生成的PDF文件附件。 Once the email was sent, I wanted to destroy the PDF since it was temporarily saved locally on the machine in order to send the email. 发送电子邮件后,我想销毁PDF,因为它是暂时保存在本地机器上的,以便发送电子邮件。 When the File.Delete() method would run, I would get the error that the IIS Worker Process was locking the file. 当File.Delete()方法运行时,我会收到IIS辅助进程正在锁定文件的错误。 I couldn't understand why since my client was already disposed of before I tried to delete the file. 我不明白为什么,因为在尝试删除文件之前,我的客户端已经被处理掉了。 Finally I found out that I had to dispose of the attachment as well before I could delete the file. 最终,我发现必须删除附件,然后才能删除文件。 So, my question is why doesn't disposing of the client also dispose of the attachment as well? 那么,我的问题是为什么不同时处置客户也就处置附件? Isn't the attachment part of the client so therefore it should be disposed of once the client is disposed? 客户端的附件部分不是吗,因此一旦客户端被丢弃,就应该将其丢弃? Sample code below: 下面的示例代码:

try
    {
        using (SmtpClient SmtpMail = new SmtpClient(""))
        {
            MailMessage message = new MailMessage(From, To, Subject, Body);
            message.IsBodyHtml = false;
            message.Priority = MailPriority.Normal;
            Attachment attachment = new Attachment(pdfString);
            message.Attachments.Add(attachment);
            SmtpMail.Send(message);
            attachment.Dispose(); //Why is this needed?
        }
    }

The SmtpClient's Send() method isn't (and shouldn't be) responsible for disposing of the MailMessage (and its associated assets). SmtpClient的Send()方法不(也不应该)负责处理MailMessage(及其关联的资产)。 The SmtpClient cannot make an assumption about your intended usage of the MailMessage after it has been sent. 发送邮件后,SmtpClient无法对您对MailMessage的预期用途进行假设。 You may wish to continue using the MailMessage or attachment stream to do other things. 您可能希望继续使用MailMessage或附件流来执行其他操作。

Let's consider the case where a MailMessage instance will be re-used by multiple SmtpClients. 让我们考虑一个MailMessage实例将被多个SmtpClients重新使用的情况。

var message = new MailMessage();

using(var client1 = new SmtpClient())
{
    client1.Send(message);
}

...

using(var client2 = new SmtpClient())
{
    client2.Send(message);
}

If the message 's attachments had already been disposed after the first call, the second call would fail. 如果在第一次通话后已经丢弃了message的附件,则第二次通话将失败。

It would be incorrect for the SmtpClient to just assume that you're done with the MailMessage after the first time you send it. 对于SmtpClient来说,仅仅假设您在第一次发送MailMessage之后就已经做完了,这是不正确的。 The Send() method should send an email, not send and also dispose of the message assets. Send()方法应该发送电子邮件,而不是发送电子邮件,也应该处理消息资产。 Your program may have had a need to do further work with the Attachment stream later in the program. 您的程序可能需要在程序后面的附件流中做进一步的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM