Ken

Ken

  • NA
  • 1
  • 0

Managing E-maill attachments question

Mar 31 2008 12:09 PM
I am creating a form where a user will fill out the form submit and will be sent as an e-mail. By using the asp:FileUpload will be able to send an attachment. I am using the MailMessage class to construct the e-mail and have it sent to a designated e-mail address. Where I am having troubles getting my head around is how to manage the attachments. The site itself is hosted off-site and I do not have a lot of space so the attachment needs to come directly to the building, or be held in a temp folder and be deleted after sending. I tried the following.

if (this.fiupDesign.HasFile)
{
filePath = Server.MapPath("~/App_Support/TempStorage");
filePath = Path.Combine(filePath, Path.GetFileName(this.fiupDesign.FileName));
this.fiupDesign.SaveAs(filePath);
fiupDesign.PostedFile.InputStream.Close();
} //end if

//Construct E-mail

mm = new MailMessage (this.txtEmail.Text, "[email protected]");
mm.IsBodyHtml = false;
mm.Subject = string.Format("Design - {0}", this.txtPatternName.Text);
mm.Body = sbTemplate.ToString();
mm.Attachments.Add(new Attachment(filePath));

sc = new SmtpClient("localhost");
sc.UseDefaultCredentials = true;
sc.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
sc.Send(mm);

/* Clean up temp file */

if (File.Exists(filePath))
File.Delete(filePath);

The problem here is that my cleanup of the temp file tries to delete the attachments to quickly and before they are sent so it throws an error saying "file is in use by another process." How would you of gone about doing this? Any help here would be fantastic. Even if it is an entirely different direction because this above isn't working.