我有以下代码,基本上将文件附加到电子邮件中,然后在附加所有附件并发送电子邮件后,我尝试删除所有文件,但是我得到一个文件正在使用异常。我相信错误出现在这一行
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
我尝试使用此代码,但收到无法发送的电子邮件
using Attachment data = new Attachment(file, MediaTypeNames.Application.Octet)){
//and the rest of the code in here.
}
foreach (KeyValuePair<string, string> kvp in reports) {
browser.GoTo(kvp.Value);
Thread.Sleep(1000);
System.IO.File.Move(@"C:\Reports\bidata.csv", @"C:\Reports\"+kvp.Key.ToString()+".csv");
string file = @"C:\Reports\" + kvp.Key.ToString() + ".csv";
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
mail.Attachments.Add(data);
}
smtpserver.Send(mail);
string[] files = Directory.GetFiles(@"C:\Reports");
foreach (string files1 in files)
{
File.Delete(files1);
}