Attachment.Add causes UnauthorizedAccessException
-
lunes, 14 de mayo de 2012 6:53I hope someone can tell me the reason for a problem which one of our customers currently had:
In our add-in for outlook 2010 we call [MailItem].Attachments.Add(...) on a specific file, which caused an UnauthorizedAccessException on the customers computer. We did some research and found out that the "temporary internet files" folder of this computer might be full. (He uses Widowsn 7 and is logged on as administrator.) The website said that Outlook has a limit of 99 files with the same name in this folder. So we told our customer to delete the files in tif and the subdirectory content.outlook, but the error kept occuring.
Today we got told from out customer that he deleted all files in \Windows\Temp directory and it works now. Though we are really happy that the symptoms are gone now, i'd like to know how to prevent and/or to solve this problem in future. So i hope you can tell us:
- Why did Attachments.Add raise an exception (Was it really because of the "100 files with same name" restriction in the tif folder)?
- Why does it work after he deleted the content in \Windows\Temp ? (Every website we found said that it is a problem with the tif folder)
I am slightly confused...
Thanks in advance
Alex
Todas las respuestas
-
martes, 15 de mayo de 2012 1:37
Hi Alex,
This exception will be thrown when the operating system denies access because of an I/O error or a specific type of security error. I think you need show some snippet to explain your issue. Are you able to reproduce your issue on your side?
Best Regards,
T.X.
-
viernes, 18 de mayo de 2012 9:03
Hi T.X.
Yes, i know that an exception can occur because of IO errors or security problems. In our customers case it wasn't a security issue as he has administrator privilegues. So ist must be some sort of an io error. Sadly i don't know what caused this error and hope you can tell me one ore more scenarioes where it can go wrong to add an attachment to a mail.
Below is the function we use to add a file to a mail. The error occurs in the mail.Attachments.Add(... line.
As far as i have read in the internet, Outlook places a copy of the file which should be attached in its own temp directory below \TemporaryInternetFiles\. Cleaning up this directory didn't work, he still got the exception.
private static bool SendHtmlMail(MailItem mail) { if (mail == null || mail.BodyFormat != OlBodyFormat.olFormatHTML) return false; try { string plainbody = GetPlainTextBody(mail); string htmlbody = mail.HTMLBody; using (TempFileHelper th = new TempFileHelper(STR_HTML_MAIL_FILE)) { Encoding mailEnc = Encoding.GetEncoding(mail.InternetCodepage); WriteFile(th.fileName, htmlbody, mailEnc); for (int i = iNumberAttachments; i > 0; i--) mail.Attachments[i].Delete(); if (!File.Exists(th.fileName)) LogString("Html file not found", LogError); // -- Exception occured here -- mail.Attachments.Add(th.fileName, OlAttachmentType.olByValue); // -- Exception occured here -- } mail.BodyFormat = OlBodyFormat.olFormatPlain; mail.Body = plainbody; } catch (System.Exception ex) { LogException(ex); return false; } return true; }

