Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 800535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:18:16+00:00 2026-05-14T23:18:16+00:00

i am using System.Net.Mail for sending mail in asp.net.. how to delete attachment file

  • 0

i am using System.Net.Mail for sending mail in asp.net..
how to delete attachment file after it is send as attachment mail..
i tried to use File.Delete method.. but i am getting this error..
the process cannot access the file path\fun.jpg’ because it is being used by another process.
thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:18:16+00:00Added an answer on May 14, 2026 at 11:18 pm

    Dispose of the MailMessage when you’re done with it.
    It still has a lock on the file you’ve added as an attachment until you’ve done so.

    var filePath = "C:\\path\\to\\file.txt";
    var smtpClient = new SmtpClient("mailhost");
    using (var message = new MailMessage())
    {
        message.To.Add("to@domain.com");
        message.From = new MailAddress("from@domain.com");
        message.Subject = "Test";
        message.SubjectEncoding = Encoding.UTF8;
        message.Body = "Test " + DateTime.Now;
        message.Attachments.Add(new Attachment(filePath));
    }
    if (File.Exists(filePath)) File.Delete(filePath);
    Console.WriteLine(File.Exists(filePath));
    

    Output: False

    I would imagine that if you still have something locking the file after disposing the message, that you likely have another lock on the file, but without code, we can’t help you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send an email in ASP.NET using system.net.mail the problem is
I'm sending attachments using the System.Net.Mail.SmtpClient in C#. The attachment names are the same
am using system.net.mail to send email as shown below, but its too slow. it
I am creating and sending emails using the System.Net.Mail namespace. The smtp.Send(mailMessage) call is
Hi I'm using System.Net.Mail to send some HTML formatted emails. What is the correct
How can I send an email using SYstem.Net.Mail to an email address that is
I have an App that will send authenticated emails using System.Net.Mail and System.Net.NetworkCredential my
I am wondering what the best way to send bulk emails is using System.Net.Mail
I am using asp.net 3.5 and C#. I want to send mail from asp.net,
I'm using System.Net.Mail to send an email message and want to make sure it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.