2

I have problem after sending an email with an attachment. After such a send I would like to delete this file and the process is denied because another process is using the file I want to delete.

This is message I am getting: "The process cannot access the file 'C:\Users\x\AppData\Roaming\ipconfig.txt' because it is being used by another process."

There is some code from me:

    public static void SendAnEmail(string host, string username, string password, string mailTo, string subject, string body, List<string> attachmentsFileNames)
    {
        SmtpClient client = new SmtpClient();
        client.Port = 587;
        client.Host = host;
        client.EnableSsl = true;
        client.Timeout = 60000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(username, password);

        MailMessage mailMessage = new MailMessage(username, mailTo, subject, body);
        foreach(string singleAttachmentFileName in attachmentsFileNames)
        {
            Attachment attachment = new Attachment(singleAttachmentFileName, "text/plain");
            mailMessage.Attachments.Add(attachment);
        }
        mailMessage.BodyEncoding = UTF8Encoding.UTF8;
        mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    }

And here is the call of the SendAnEmail function and try of deleting those files I sent as an attachments.

            Transport.SendAnEmail(Settings.mailHost, Settings.mailUsername, Settings.mailPassword, Settings.mailTargetAddress, Settings.ftpDirectory, " ", Settings.attachmentsListFileNames);

            foreach(string singlePath in Settings.attachmentsListFileNames)
                File.Delete(singlePath);

I would like an advise how I may resolve this issue and after send an email with attachment - delete those attachment.

Please ask me if something is not clear.

edit: Sure, I am using C#, framework 4.5, WPF. Platform targeted is Windows and the environment used to programming is Visual Studio 2013 :)

tripleee
  • 175,061
  • 34
  • 275
  • 318
jan kowalski
  • 189
  • 3
  • 21
  • Please [edit] your question to clarify the language and platform. – tripleee Sep 28 '15 at 03:07
  • Anyone see the solution ? Please take a look because it is unusual case. Maybe I didn't point the situation in most clear way, but I think you can understand what is the case ? Acces to the file is denied and I can't delete it, and this is only if I am sending an email with such a file as an attachment line before I would like to send. – jan kowalski Sep 28 '15 at 13:41
  • I added the [tag:c#] and [tag:.net] tags to your question. If these are not correct, please edit. With no useful tags, you are virtually guaranteed to not get any useful answers. Adding comments here will only reach, by rough approximation, me; and I know nothing about Windows. – tripleee Sep 28 '15 at 13:44
  • I could not found the private message button to you, so I would like to thank you for your support here - thanks :) – jan kowalski Sep 28 '15 at 13:52
  • There are no private messages, but thanks. If you would like to increase my reputation here, visiting my profile and finding something you'd like to upvote would be much appreciated. (-: – tripleee Sep 28 '15 at 14:07
  • Possible duplicate of [delete attachment file](https://stackoverflow.com/questions/2857107/delete-attachment-file) – Cee McSharpface Dec 15 '17 at 10:23

1 Answers1

2

Before deleting, you have to do this :

mailMessage.Attachments.Dispose();
client.Dispose();

It worked for me.

A.Pissicat
  • 3,023
  • 4
  • 38
  • 93