所以这是代码:
string filename = @"c:\test.xlsx";
using (XLWorkbook wb = CreateWorkbookInformation())
{
wb.SaveAs(filename);
Email.EmailAsAttachment(filename);
}
File.Delete(filename);
它可以很好地创建工作簿信息,它可以很好地保存文件,它可以将文件作为附件发送电子邮件...但是,当我尝试删除文件时(在using
语句之后),它会声明“过程正在使用中”。应该没有任何保持文件打开的东西?!?我错过了什么过程,我应该关闭以删除文件?
答案 0 :(得分:0)
啊,没关系,与ClosedXML无关,只是将文件附加到电子邮件而没有使用正确的using
语句来附件。因此,附件过程使文件保持打开状态。
答案 1 :(得分:0)
非常感谢您发布此答案!!!
我发疯了,试图了解我的问题!
我按照您的建议做了,并使用了正确的using
语句:
using (MailMessage mail = new MailMessage(senderAdress, emailAdress))
{
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = ConfigurationManager.AppSettings["SMTPHostName"];
if (string.IsNullOrWhiteSpace(client.Host))
{
errorMsg = "ConfigurationManager.AppSettings[\"SMTPHostName\"] not found";
return false;
}
mail.Subject = subject;
mail.IsBodyHtml = isBodyHtml;
mail.Body = body;
if (!string.IsNullOrWhiteSpace(attachmentFullPath))
{
if (File.Exists(attachmentFullPath))
{
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
contentType.Name = Path.GetFileName(attachmentFullPath);
mail.Attachments.Add(new Attachment(attachmentFullPath, contentType));
}
else
{
errorMsg = "Attachment File Doesn't Exist: " + attachmentFullPath;
return false;
}
}
client.Send(mail);
}