I am creating a file on the local drive with the contents using the code below.
File.WriteAllLines(path, contents);
I attach this file to the mail and send acrross to the command. After sending the mail, I need to delete the file in order to delete the file that I use below, but I get a runtime error
File.Delete(path);
Error message : the process cannot access the file because it is being used by another process
by default, the WriteAllLines () method closes the file, but still it is opened by another process. I can only delete the file by running the code after a while, but this is not a script. I need to delete it after sending mail.
Update
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); mailMessage.To.Add(new System.Net.Mail.MailAddress(recipient, ToName)); mailMessage.From = new System.Net.Mail.MailAddress(From, FromName); mailMessage.Subject = Subject; // "Outlook calendar as attachment"; // modified by Srikanth J on 28/06/2012 mailMessage.Body = "This is a test message"; System.Net.WebClient webclient = new System.Net.WebClient(); webclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; for (int i = 0; i < item.Attachments.Count; i++) { string url = item.Attachments.UrlPrefix + item.Attachments[i]; SPFile file = item.ParentList.ParentWeb.GetFile(url); mailMessage.Attachments.Add(new System.Net.Mail.Attachment(file.OpenBinaryStream(), file.Name)); } System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(path); mailMessage.Attachments.Add(mailAttachment); smtp.Send(mailMessage);
Any help is provided, thanks.
source share