2

我有一个关于 MVC 4 C# 互联网应用程序的项目,我按照一些指南使用 ajax 函数将 Google 可视化柱形图保存到我的驱动器中。此外,我正在使用 razor PDF nugget 包制作 PDF 格式的报告。路径是: “C\temp\”和两个文件都在那里。我需要将两个文件附加到电子邮件中并发送它们,然后删除创建的文件。

我用 ajax 调用发送电子邮件功能:

$('#btnSend').on('click', function () {
            if (from) {
                fromDate = from.toJSON();
            }

            if (to) {
                toDate = to.toJSON();
            }
            // from and to are dates taken from two datepickers

            // saves the pdf to the server
            if (from && to) {
                $.ajax({
                    url: "/Home/SavePdf/",
                    data: { fromDate: fromDate, toDate: toDate },
                    type: 'GET',
                    async: false,
                    datatype: 'json',
                    success: function (data) {
                        alert('got here with data');
                    },
                    error: function () { alert('something bad happened'); }
                });
            }


            // saves the column chart to the server from canvas item with id reports

            var imgData = getImgData(document.getElementById("reports"));
            var imageData = imgData.replace('data:image/png;base64,', '');

            $.ajax({
                url: "/Home/SaveImage/",
                type: 'POST',
                data: '{ "imageData" : "' + imageData + '" }',
                datatype: 'json',
                async: false,
                contentType: 'application/json; charset=utf-8',
                success: function () {
                    alert('Image saved successfully !');
                },
                error: function () {
                    alert('something bad happened');
                }
            });

            $.ajax({
                type: "POST",
                url: "/Home/SendMail/",
                async: false,
                success: function() {
                    alert('Message sent successfully');
                }
            });
        });

这是我在后面代码中的功能

public void SendMail()
        {
            var path = @"C:\temp\";
            string pngFilePath = path + DateTime.Now.ToShortDateString() + ".png";
            string pdfFilePath = path + DateTime.Now.ToShortDateString() + ".pdf";

            MailMessage message = new MailMessage("message sender", "message reciever")
            {
                Subject = "Test",
                Body = @"Test"
            };
            Attachment data = new Attachment(pdfFilePath , 

 MediaTypeNames.Application.Pdf);
        message.Attachments.Add(data);

        Attachment data2 = new Attachment(pngFilePath , GetMimeType(pngFilePath); 
 // GetMimeType function gets the mimeType of the unknown attachment
        message.Attachments.Add(data2);

        SmtpClient client = new SmtpClient();
        client.Host = "smtp.googlemail.com";
        client.Port = 587;
        client.UseDefaultCredentials = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential("mail sender", "sender password");
        client.Send(message);

        // delete the png after message is sent
        if ((System.IO.File.GetAttributes(pngFilePath ) & FileAttributes.Hidden) == FileAttributes.ReadOnly) 
        {

            System.IO.File.SetAttributes(pngFilePath , FileAttributes.Normal);            

            if (System.IO.File.Exists(pngFilePath ))  
            {     
                System.IO.File.Delete(pngFilePath ); 
            }  
        }

        // delete the pdf after message is sent
        if ((System.IO.File.GetAttributes(pdfFilePath ) & FileAttributes.Hidden) == FileAttributes.ReadOnly)
        {

            System.IO.File.SetAttributes(pdfFilePath , FileAttributes.Normal);

            if (System.IO.File.Exists(pdfFilePath ))
            {
                System.IO.File.Delete(pdfFilePath );
            }
        }
    }

我想删除这些文件,但 IIS 继续使用它们并且永远无法执行删除操作。有没有办法从 iis 中分离这些文件并删除它们?

4

2 回答 2

3

SmtpClient是锁定文件。为每个文件创建一个流并将其用作附件。像这样的东西:

using(Stream fs1 = File.OpenRead(pdfFilePath))
using(Stream fs2 = File.OpenRead(pngFilePath))
{
   Attachment data = new Attachment(fs1 , GetMimeType(pdfFilePath));
   Attachment data2 = new Attachment(fs2 , GetMimeType(pngFilePath));
   message.Attachments.Add(data);
   message.Attachments.Add(data2);
   ...
   client.Send(message);
}

编辑:我最初的建议,client.Dispose()在发送邮件后使用,似乎不起作用

于 2013-11-07T16:18:03.727 回答
0

我添加了同样的问题,我看到了以前的答案。

可以使用client.Dispose(),但您也必须这样做message.Attachments.Dispose()

之后,您可以删除您的文件。

于 2018-04-12T11:08:23.097 回答