Solved

could not send attachment but only email asp.net

Posted on 2014-01-08
3
328 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

929 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

17 Experts available now in Live!

Get 1:1 Help Now