Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello, We have a very basic asp.net application for error reporting puposes.. the applcation usually used to send the emails to the respective team with some error infomation.. Now the users want us to modidy the applcation to give them the ability to attach screen shots of error message.. i am using th below code to attach the emails and running the application in winodws autentication mode but getting the acess denied error error in line 2 of the below code.

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(@"C:\Documents and Settings\u1hc95\Desktop\2013-04-05_1715.png");----Line 2
mmErrMail.Attachments.Add(attachment);

Would you please help me to get around this?
Posted

There are quite a few overloads to create an Attachment, none of them match the way you initialised the Attachment.
When creating an Attachment the second parameter needs to be either NULL or identify a valid MIME-type for your attachment.
Please refer below links

http://msdn.microsoft.com/en-gb/library/system.net.mail.attachment.aspx[^]

http://www.dreamincode.net/forums/topic/289099-systemnetmailattachments-access-to-path-is-denied/[^]

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4b9bda2e-aad1-4d36-89c3-aa32347930ed/[^]
 
Share this answer
 
Comments
Ravindranath.net 11-Apr-13 11:33am    
I even tried this but still having the issue,,


System.Web.Mail.MailAttachment attachment;
attachment = new MailAttachment(frmScrShotUpload.PostedFile.FileName, MailEncoding.Base64);
mmErrMail.Attachments.Add(attachment);


Would you please help me? i am really in big trouble
Ravindranath.net 15-Apr-13 14:35pm    
Thanks a lot... it worked for me.
snehal harawande 16-Apr-13 5:55am    
Please also rate answers.
While working on email functionality I also had same issue.
It's because we are directly using file name from upload.
To solve issue I saved the file from uploaded to temporary upload folder on server.
Then send the mail with attachment.
After that clear uploaded file from temporary upload folder.
Please check out below code.

string attach1 = string.Empty;
            string strFileName = string.Empty;
            MailMessage message = new MailMessage();
            if (attachFile1.PostedFile != null && attachFile1.PostedFile.FileName != string.Empty)
            {
                HttpPostedFile ulFile = attachFile1.PostedFile;
                Int64 nFileLen = ulFile.ContentLength;
                if (nFileLen > 0)
                {
                    strFileName = Path.GetFileName(attachFile1.PostedFile.FileName);
                    strFileName = WebConfigurationManager.AppSettings["Uploads"] + "/" + strFileName;
                    attachFile1.PostedFile.SaveAs(Server.MapPath(strFileName));
                    MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
                    message.Attachments.Add(attach);
                    attach1 = strFileName;
                }
            }
            message.From = txtFrom.Text;
            message.To = txtTo.Text;
            message.Cc = txtcc.Text;
            message.Bcc = txtbcc.Text;
            message.Subject = txtSubject.Text;
            message.Body = hdnmsg.Value;
            message.BodyFormat = MailFormat.Html;
            SmtpMail.SmtpServer = WebConfigurationManager.AppSettings["Host"];
            SmtpMail.Send(message);    
            if (attach1 != string.Empty)
                File.Delete(Server.MapPath(attach1));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900