I have trouble with sending an email with attached file in C#. First time everything is ok: a pdf file is generated and attached to a email and the email can be send and receive. But if I try this twice, I get an IO-Exception when generating the file. If I try to rename the file manually I get an error message, which show me that the IIS Worker Process keep on using the file.
If I commented the part for email sending out, the file can generated and saved more times. So it's sure that the error is in this code part.
Here is my code for sending the email:
MailMessage eMail = new MailMessage();
eMail.To.Add(sEmailAddressReceiver); //filled before
eMail.From = new MailAddress(sEmailAddressSender); //filled before
eMail.Subject = "Title";
eMail.Priority = MailPriority.Normal;
eMail.Body = "File is attached.";
Attachment aAttachment = new Attachment(sFilename);
eMail.Attachments.Add(aAttachment);
SmtpClient smtpClient = new SmtpClient("xxx", 25);
smtpClient.Send(eMail);
Have anyone an idea what is missing?
Thank you!