• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

could not send attachment but only email asp.net

tried sending email with attachment,

just that it could not send the attachment but only the email

error :
System.IO.IOException: The process cannot access the file 'C:..\Websites\SendEmailwithAttachment\timesheet.xlsx' because it is being used by another process.
i guess this works:

eventhough it is checked from the task manager it is not running there,

            var smtp = new SmtpClient
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("asdasd@gmail.com", "pwd"),
            using (var message = new MailMessage("sadada@gmail.com", "sadsads@gmail.com")
                Subject = "sadsasdaad",
                Body = "assadsadsada",




             //   string strFileName = null;

                if (inpAttachment2.PostedFile != null)
                    HttpPostedFile attFile = inpAttachment2.PostedFile;
                    int attachFileLength = attFile.ContentLength;
                    if (attachFileLength > 0)
                        strFileName = Path.GetFileName(inpAttachment2.PostedFile.FileName);

                        Attachment attach = new Attachment(Server.MapPath(strFileName));

                        //MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
                        //    attach2 = strFileName;


Open in new window

  • 2
1 Solution
Kishan ZunjareSr. Software EngineerCommented:
Hi doramail05,

Your problem seems to be caused by not released memory stream;

using (Attachment data = new Attachment("FileName",  MediaTypeNames.Application.Octet))
    // 1. Adding attachment to the e-mail message
    // 2. Sending out the e-mail message

The using statement will ensure that the Dispose method is called when the object gets out of the scope.

Also calling Dispose on the SmtpClient, doesn't call it on the Attachments.

Hope this will help you.

doramail05Author Commented:
hi there

the problem solved,

it is being called the attachment at the above code and it is being called again afterwards

which rectify the problem
doramail05Author Commented:
discover out on own afterwardss
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now