Alex Rivera | Logout

SMTP Send is locking up my files - c#

Asked 2009-08-18T20:42:17.297
27

I have a function thats sending messages ( a lot of them) and their attachments.

It basically loops through a directory structure and creates emails from a file structure for example

 c:\emails\message01
                \attachments
 c:\emails\message02
                \attachments

The creation of the messages takes place using .net c#, standard stuff.

After all messages are created... I have another function that runs directly afterwards that copies the message folder to another location.

Problem is - files are locked...

Note: I'm not moving the files, just copying them....

Any suggestions on how to copy locked files, using c#?

Update

I have this add attachments method

    private void AddAttachments(MailMessage mail)
    {
        string attachmentDirectoryPath = "c:\messages\message1";
        DirectoryInfo attachmentDirectory = new DirectoryInfo(attachmentDirectoryPath);
        FileInfo[] attachments = attachmentDirectory.GetFiles();
        foreach (FileInfo attachment in attachments)
        {
            mail.Attachments.Add(new Attachment(attachment.FullName));
        }
    }
Edit
Report

1 Answer

2
    MailMessage email = new MailMessage();

    email.From = txtFrom.Text;
    email.To = txtToEmail.Text;
    email.Subject = txtMSubject.Text; 
    email.Body = txtBody.Text;

    SmtpClient mailClient = new SmtpClient();
    mailClient.Host = "smtp.emailAddress";
    mailClient.Port = 2525;
    mailClient.Send(email );
    email.Dispose();

    // After Disposing the email object you can call file delete

    if (filePath != "")
    {
      if (System.IO.File.Exists(filePath))
      {
        System.IO.File.Delete(filePath); 
      }
    }
answered 2012-06-26T00:00:26.787

Your Answer