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 :)