文件作为电子邮件附件发送后卡在 w3wp.exe

File get stuck in w3wp.exe after sending it as email attachment

我有一个 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);
}

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