문제

I have created an application that generates excel files from information in a database. These files are saved on my HDD in a folder.

After that I attach the files and send them via mail. When I generate another batch of files I delete the old files and then create the new ones.

My problem is when I have generated one batch of files and then send them, and I want to generate another batch I cannot delete the one of the old files, because the mailing method is still holding on to one of the excel files.

Here is my code:

public void SendMailedFilesDKLol() {
    string[] sentFiles=Directory.GetFiles(some_Folder);

    if(sentFiles.Count()>0) {
        System.Net.Mail.SmtpClient client=new System.Net.Mail.SmtpClient("ares");
        System.Net.Mail.MailMessage msg=new System.Net.Mail.MailMessage();

        msg.From=new MailAddress("system@lol.dk");
        msg.To.Add(new MailAddress("lmy@lol.dk"));
        msg.Subject="IBM PUDO";

        msg.Body=
            sentFiles.Count()+" attached file(s) has been sent to the customer(s) in question ";

        msg.IsBodyHtml=true;

        foreach(string file in sentFiles) {
            Attachment attachment=new Attachment(file);
            msg.Attachments.Add(attachment);
        }

        client.Send(msg);
    }
}

I have tried to dispose the client element but that didn't help.

Can anyone help me with this?

도움이 되었습니까?

해결책

Both System.Net.Mail.MailMessage & System.Net.Mail.SmtpClient are IDisposable classes. You can try the following,

    using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("ares"))
    {
       using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
       {
          msg.From = new MailAddress("system@lol.dk");
          msg.To.Add(new MailAddress("lmy@lol.dk"));
          msg.Subject = "IBM PUDO";
          msg.Body = sentFiles.Count() + " attached file(s) has been sent to the customer(s) in question ";
          msg.IsBodyHtml = true;
          foreach (string file in sentFiles)
          {
              Attachment attachment = new Attachment(file);
              msg.Attachments.Add(attachment);
          }

          client.Send(msg);
        }
     }

다른 팁

It sounds like you may not be closing your file stream when generating the excel files or you have them open in excel when trying to email them.

Please can you show your code for generating the excel files.

You need to dispose the Attachment objects. Example using LINQ:

public void SendMailedFilesDKLol() {
    string[] sentFiles=Directory.GetFiles(some_Folder);

    if(sentFiles.Count()>0) {
        System.Net.Mail.SmtpClient client=new System.Net.Mail.SmtpClient("ares");
        System.Net.Mail.MailMessage msg=new System.Net.Mail.MailMessage();

        msg.From=new MailAddress("system@lol.dk");
        msg.To.Add(new MailAddress("lmy@lol.dk"));
        msg.Subject="IBM PUDO";

        msg.Body=
            sentFiles.Count()+" attached file(s) has been sent to the customer(s) in question ";

        msg.IsBodyHtml=true;

        var attachments = sentFiles.Select(f => new Attachment(f)).ToList();

        attachments.ForEach(a => msg.Attachments.Add(a));

        client.Send(msg);

        attachments.ForEach(a => a.Dispose());
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top