MailMessage 发送后删除附件 if in Path.GetTempPath()
MailMessage Delete attachments after sending if in Path.GetTempPath()
请原谅我对 MailMessage 和 SmtpClient 的陌生 类。我已经构建了一些基本可用的东西,但在准备发送附件时,我有时会将附件复制到临时文件位置 (Path.GetTempPath() + @"\" + timestampWithFF
),因为有时必须压缩它们才能发送。发生这种情况时,我想确保在发送后删除那里的文件(特别是因为那里的任何东西都会相对较大)。
双重问题:
1. 我不应该清理文件因为 OS (win7) 会做得很好吗?
2. 如何获取client.SendCompleted
中附件的硬盘位置?
client.SendCompleted += (s, e) =>
{
client.Dispose();
foreach(Attachment a in msg.Attachments)
{
// want to actually delete the file from the HDD if it's in Path.GetTempPath();
}
msg.Dispose();
};
我知道我可以使用 a.Dispose()
,但我不知道它的作用...我怀疑它正在处理对象(无论如何 msg.Dispose
接下来都会做),但会将文件留在 HDD 上。
我必须单独发送附件的文件路径吗?
client.SendCompleted()
行位于:
sendMailAsync(SmtpClient client, MailMessage msg)
方法。我可以将其更改为:
sendMailAsync(SmtpClient client, MailMessage msg, List<string> attachments)
并将其添加到 SendCompleted()
,但感觉有点笨拙:
string tempDir = Path.GetTempPath();
foreach(string f in attachments)
{
if(f.Contains(tempDir)) // want to actually delete the file from the HDD if it's in Path.GetTempPath();
{
if (File.Exists(f)) { File.Delete(f); }
}
}
- should I not bother with cleaning up the files because the OS (win7) will do a good job of it?
如果我是你,我仍然会删除临时文件,尽管 OS 会在认为必要时清理它
- how can I get the HDD location of the Attachments in client.SendCompleted?
附件中的文件可以通过ContentStream
取回。他们的类型是 FileStream
。
client.SendCompleted += (s, e) =>
{
client.Dispose();
var fileattachments = msg.Attachments
.Select(x => x.ContentStream)
.OfType<FileStream>()
.Select(fs => fs.Name)
.ToArray();
msg.Dispose();
string tempPath = Path.GetTempPath();
foreach (var attachment in fileattachments )
{
if(attachment.Contains(tempPath)
{
File.Delete(attachment);
}
}
};
注:先处理msg
对象再做删除
请原谅我对 MailMessage 和 SmtpClient 的陌生 类。我已经构建了一些基本可用的东西,但在准备发送附件时,我有时会将附件复制到临时文件位置 (Path.GetTempPath() + @"\" + timestampWithFF
),因为有时必须压缩它们才能发送。发生这种情况时,我想确保在发送后删除那里的文件(特别是因为那里的任何东西都会相对较大)。
双重问题:
1. 我不应该清理文件因为 OS (win7) 会做得很好吗?
2. 如何获取client.SendCompleted
中附件的硬盘位置?
client.SendCompleted += (s, e) =>
{
client.Dispose();
foreach(Attachment a in msg.Attachments)
{
// want to actually delete the file from the HDD if it's in Path.GetTempPath();
}
msg.Dispose();
};
我知道我可以使用 a.Dispose()
,但我不知道它的作用...我怀疑它正在处理对象(无论如何 msg.Dispose
接下来都会做),但会将文件留在 HDD 上。
我必须单独发送附件的文件路径吗?
client.SendCompleted()
行位于:
sendMailAsync(SmtpClient client, MailMessage msg)
方法。我可以将其更改为:
sendMailAsync(SmtpClient client, MailMessage msg, List<string> attachments)
并将其添加到 SendCompleted()
,但感觉有点笨拙:
string tempDir = Path.GetTempPath();
foreach(string f in attachments)
{
if(f.Contains(tempDir)) // want to actually delete the file from the HDD if it's in Path.GetTempPath();
{
if (File.Exists(f)) { File.Delete(f); }
}
}
- should I not bother with cleaning up the files because the OS (win7) will do a good job of it?
如果我是你,我仍然会删除临时文件,尽管 OS 会在认为必要时清理它
- how can I get the HDD location of the Attachments in client.SendCompleted?
附件中的文件可以通过ContentStream
取回。他们的类型是 FileStream
。
client.SendCompleted += (s, e) =>
{
client.Dispose();
var fileattachments = msg.Attachments
.Select(x => x.ContentStream)
.OfType<FileStream>()
.Select(fs => fs.Name)
.ToArray();
msg.Dispose();
string tempPath = Path.GetTempPath();
foreach (var attachment in fileattachments )
{
if(attachment.Contains(tempPath)
{
File.Delete(attachment);
}
}
};
注:先处理msg
对象再做删除