Reputation: 74
I know this question has been asked a few times now, but I checked all the answers out and none seems to solve my problem.
I'm trying to send photos as Attachments in an email. So, first, when the person chooses the photos, I copy them into a specific folder, then I get them back from this folder as I send the mail. In the end, I want to delete the folder, but it throw the exception you can see in the title. I already checked, I don't see any stream I didn't close (since I don't use streams for this), I'm lost.
Method I use to copy the file :
private void Send_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dialogresult = ofd.ShowDialog();
///Si l'utilisateur a bien rentré un fichier
if (dialogresult == DialogResult.OK || dialogresult == DialogResult.Yes)
{
string folder = Data.HelperDirectory.getFolderDocumentAndCreate("Devis");
Dialogs.PleazeWait pw = new Dialogs.PleazeWait();
pw.Show();
///Récupère le nom du fichier que l'utilisateur a envoyé précédemment
string file = folder + System.IO.Path.GetFileName(ofd.FileName);
///Ajoute "(1)" au nom du fichier pour éviter les doublons
while (File.Exists(file))
{
file = file + " (1)";
}
///Copie le fichier dans le répertoire de sauvegardes
File.Copy(ofd.FileName, file);
pw.Close();
}
}
Code part used to get the files and attach them to the mail :
string folder = Data.HelperDirectory.getFolderDocumentAndCreate("Devis");
string[] files = Directory.GetFiles(folder);
foreach(string file in files)
{
if(!string.IsNullOrEmpty(file))
{
mail.Attachments.Add(new Attachment(file));
}
}
Finally, when I try to delete the folder (after sending the mail of course) :
Directory.Delete(folder, true);
If it can help to solve the problem, I can say this code manages to send the mail (so it passes the second part of code) but throws an exception when trying to delete the folder. And nothing about files or folders is called between those parts of code.
Thanks in advance for your time and help !
Upvotes: 1
Views: 85
Reputation: 2626
Depends on your type of mail but after sending the mail you can try to dispose that object.
client.Send(mail);
mail.Dispose();
Upvotes: 2