Solved

could not send attachment but only email asp.net

Posted on 2014-01-08
3
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Easy CSR creation in Exchange 2007,2010 and 2013
The new Gmail Phishing Scam going around is surprising even the savviest of users with its sophisticated techniques.
To show how to create a transport rule 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 >> Rules tab.:  To cr…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

762 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