I m sending email through window application with attachment when the mail successfully sent but after send mail i want to delete the attachment file.
But it give error
please help me
code is below
if (File.Exists(@"C:\abc.txt"))
{
//File.Delete(@"C:\abc.txt");
MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
mail.Subject = "Email using Gmail";
Attachment attfile = new Attachment(@"C:\abc.txt");
mail.Attachments.Add(attfile);
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("[email protected]", "password");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
File.Delete(@"C:\abc.txt");// when i m deleting this file it give error that it is being used by another process
MessageBox.Show("email sent successfully");
}
Loading

Vikas AhlawatPosted May 28, 2010, 9:52 PM
attfile.Dispose();
Dushan StankovicPosted May 28, 2010, 10:29 AM
I use VS2008 pro and VS2010 ultimate and code I post you work fine on both IDE.
Vikas AhlawatPosted May 28, 2010, 7:56 AM
i have tried using(MailMessage mm = new MailMessage())
but it gives error that mailmessage does not take 0 argument.
Dushan StankovicPosted May 28, 2010, 6:44 AM
using(MailMessage mm = new MailMessage())
{
//here put your code for sending mail. After this block of code ends that code will be disposed and your file will be leaved by process who use it.
}
File.Delete(@"c:\abc.txt");
Vikas AhlawatPosted May 28, 2010, 6:20 AM
So what i can do that attaching process leave this file after sent mail?
why attaching process not leave it?
Dushan StankovicPosted May 28, 2010, 5:15 AM
using (MailMessage mail = new MailMessage())
{
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
mail.Subject = "Email using Gmail";
Attachment attfile = new Attachment(@"C:\abc.txt");
mail.Attachments.Add(attfile);
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("[email protected]", "password");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
// smtp.Send(mail);
MessageBox.Show("email sent successfully");
}
File.Delete(@"C:\abc.txt");// when i m deleting this file it give error that it is being used by another process
CrishPosted May 28, 2010, 5:10 AM
try below code..
FilePath = Server.MapPath("../foldername" + filename);
public void DeleteFilePhysically(string FilePath)
{
FileInfo NewFilePath = new FileInfo(FilePath);
if (NewFilePath.Exists)
File.Delete(NewFilePath.ToString());
}
Don't forget to Mark Do you like this Answer
that solved your problem!