Solved

could not send attachment but only email asp.net

Posted on 2014-01-08
3
327 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
Comment Utility
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
Comment Utility
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
Comment Utility
discover out on own afterwardss
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
In this video we show how to create a User Mailbox 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 Recipients >> Mailb…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now