c# - 作为电子邮件附件发送后,文件卡在 w3wp.exe 中

标签 c# asp.net webforms smtp w3wp

我有一个 Web 应用程序,它创建 Outlook session .ics 文件并将其作为附件发送给用户。它工作正常,但发送后我无法从服务器删除该文件,因为它正在被 w3wp.exe 使用。 这是我的功能:

public static void SendEmail(string subject, string body, List<string> to, string path)
{
    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="294746045b4c594550694a464459484750074a4644" rel="noreferrer noopener nofollow">[email protected]</a>", "Test email");
    foreach (string item in to)
    {
        mailMessage.To.Add(item);
    }
    mailMessage.Subject = subject;
    mailMessage.BodyEncoding = Encoding.UTF8;
    mailMessage.Body += body;
    mailMessage.IsBodyHtml = true;
    SmtpClient smtpClient = new SmtpClient("smtp.company.com");
    if (!string.IsNullOrEmpty(path))
    {
        Attachment attachment = new Attachment(path);
        mailMessage.Attachments.Add(attachment);
    }
    smtpClient.Send(mailMessage);
}

最佳答案

你错过了最后的mailMessage.Dispose();

关于c# - 作为电子邮件附件发送后,文件卡在 w3wp.exe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45299226/

相关文章:

c# - 将 IFormFile 转换为 Stream 将内容类型设置为 application/octet-stream

c# - Webservice复杂类型和类继承

c# - 使用 ASP.Net 的 Crystal Reports 中的 Blob 字段

javascript - HTML 表单 onSubmit()

asp.net - 上载大文件

c# - ASP.Net 以编程方式更改母版页

c# - 使用 where 子句时排除字符串的一部分

c# - 从默认库跨平台 Xamarin 选择多个图像

asp.net - 如何在回发触发器中添加进度条

c# - 如何获取请求的默认文档?