gpt4 book ai didi

c# - 如何在 MVC 应用程序中使用 SMTP 客户端发送和发送电子邮件后删除保存在服务器上的文件

转载 作者:行者123 更新时间:2023-11-30 22:12:14 24 4
gpt4 key购买 nike

我有一个关于 M.V.C 4 C# 互联网应用程序的项目,我已经按照一些指南使用 ajax 功能将 Google 可视化柱形图保存到我的驱动器中的图像。此外,我正在使用 Razor P.D.F 金 block 包制作 P.D.F 格式报告。路径是:“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 中分离并删除这些文件吗?

最佳答案

锁定文件的是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(),但似乎行不通

关于c# - 如何在 MVC 应用程序中使用 SMTP 客户端发送和发送电子邮件后删除保存在服务器上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19840782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com