Solved

could not send attachment but only email asp.net

Posted on 2014-01-08
3
330 Views
Last Modified: 2014-01-14
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);
                        inpAttachment2.PostedFile.SaveAs(Server.MapPath(strFileName));

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

                        //MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
                        message.Attachments.Add(attach);
                        attach.Dispose();
                        //    attach2 = strFileName;
                    }
                }

                smtp.Send(message);
                message.Dispose();
            }

Open in new window

0
Comment
Question by:doramail05
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 39764708
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.

-Kishan
0
 
LVL 1

Accepted Solution

by:
doramail05 earned 0 total points
ID: 39770162
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
0
 
LVL 1

Author Closing Comment

by:doramail05
ID: 39778798
discover out on own afterwardss
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question