2

I have a method, which saves screenshot by the definite path and then makes email message, attaching screenshot to it. As I've understood, after sending it - the special thread is being created in which attachment file is used, so I can't delete it while that thread is working. So, I need to know if when the file will be accessible for deleting.

Here's my code:

-- Configuring smtp

private SmtpClient CreateSMTP()
{
    var smtp = new SmtpClient("gate");
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("notifications@****.com", "3eCMCQxFde");
    smtp.Port = 25;
    smtp.EnableSsl = false;

    return smtp;
}

-- Making message

public MailMessage MakeMessage(bool screenshotFlag)
    {
        MailAddress from = new MailAddress("notifications@****.com", Name);
        MailAddress to = new MailAddress("****@****.com");
        MailMessage message = new MailMessage(from, to);

        message.Subject = Subject == string.Empty ? string.Empty : Subject;
        message.Body = MessageText;
        message.Body = GenerateLogAndExceptionInfo(message.Body);
        message.BodyEncoding = Encoding.Unicode;

        message.ReplyTo = new MailAddress(Mail);

        if (screenshotFlag)
        {
            CreateScreenshot();
            message.Attachments.Add(new Attachment(MailHelper.FeedBackScreenShotPath));
        }

        return message;
    }

-- Sending email

public void SendMessage()
{
    using (SmtpClient smtp = CreateSMTP())
    {
        smtp.Send(MakeMessage(SendWithScreenshot));
    }
}
  • delete it in your thread after the work was done? – Denis Schaf Mar 11 '19 at 09:25
  • Deleting is realized before closing the app. But if I send message and then close the program very quickly - the attached file is still being used by "sending thread" , so I have an exception. Before deleting I need to know exactly if file is not used anymore. @DenisSchaf – Харламов Даниил Mar 11 '19 at 09:29
  • If it is a user related problem, like the user is closing the app, then on the close button, just hide your form/interface and finish sending the email before deleting the attachments and closing the app. – meJustAndrew Mar 11 '19 at 09:31
  • Yes, I want to hide interface, or wait, or deny closing the app. But I don't know if when email is really sent. @meJustAndrew – Харламов Даниил Mar 11 '19 at 09:39
  • You could see [this thread](https://stackoverflow.com/questions/10504647/deleting-files-in-use) for waiting after the file is not in use anymore and then delete it. – meJustAndrew Mar 11 '19 at 10:10

2 Answers2

2

From the documentation:

These methods block while the message is being transmitted.

So while the message is being transmitted, the method blocks. So after the method is done and you've disposed the message instance, you could delete the file.

Of course, it could still have a lock on the file. That is why I would say you should first dispose the SmtpClient and then try to delete the file (so do that after the using block). It should be fine then.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

I've seen that the file was holding by the message object, not by smtp object, so I added using block for message, too.

Thanks to all))