7
Reply

sending email with win application?

Vikas Ahlawat

Vikas Ahlawat

May 28 2010 2:45 AM
2.5k
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");
                    
                }

Answers (7)