【问题标题】:File get stuck in w3wp.exe after sending it as email attachment文件作为电子邮件附件发送后卡在 w3wp.exe 中
【发布时间】:2017-07-25 09:29:58
【问题描述】:

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

public static void SendEmail(string subject, string body, List<string> to, string path)
{
    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress("no-reply@company.com", "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);
}

【问题讨论】:

    标签: c# asp.net webforms smtp w3wp


    【解决方案1】:

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

    【讨论】:

    • 在这种情况下,最好使用 USING 关键字,这样可以确保垃圾收集器会清理内存。示例:using(MailMessage mailMessage = new MailMessage()) { ... } 使用此代码将起作用,因为最后 using 将为您执行 .Dispose()
    • 很高兴能帮到你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2013-03-19
    • 2015-09-09
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多