Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi,

I am working on a solution to to extract emails from exchange using EWS APIs and then upload to SharePoint online . Below is the sample code for file creation, and before upload to sharepoint.

string mSubject = mItem.Subject;
string targetFileURL = targetFolderUrl + "/" + Regex.Replace(mSubject, @"[^\w\.@-]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5));

//limit URL to 150 characters
if (targetFileURL.Length > 150)
targetFileURL = targetFileURL.Substring(0, 150) +".eml";
else
targetFileURL = targetFileURL + ".eml";

mItem.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mc = mItem.MimeContent;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = mc.Content;
newFile.Url = targetFileURL;
newFile.Overwrite = true;


It works perfectly well for .EML format but when I try to save the file in .MSG format it throws error.

Any suggestions on how to save the file in proper MSG format? Will greatly appreciate quick responses.

Thanks
Posted
Updated 14-Apr-15 6:23am
v2
Comments
Sergey Alexandrovich Kryukov 14-Apr-15 15:38pm    
Of course, because MSG format is different. "it throws error" is not informative (and not correct: it may throw exception :-).
Do you mean Outlook MSG or something else?
—SA
Member 11606873 14-Apr-15 15:44pm    
Exact exception thrown is

"Cannot open "Filename.MSG". Its possible the file is already open or you don't have permissions to open it.
To check your permissions, right click the file folder and then click properties."

and yes I mean outlook MSG only.

I understand that the format is different, and so please suggest how i should save the MSG format?

1 solution

It's possible that Outlook is running and the file is opened for exclusive use by Outlook (this is the default way of opening files).

If this is that problem (no matter if your access is blocked by outlook or any other process), you can use the Sysinternals utility "handle.exe". For further detail and the explanation on how you investigate such case, please see my past answer:
how to compress the error 'it is already used by another process' in vb.net[^].

If the problem is just permission, just check up permission. Then you may need to just run your application as administrator. (I doubt this is the case though.)

If you really need to work with MSG, you need to understand that the format is proprietary, so many relevant products, even 3rd-party ones, will be commercial. But you don't have to use such products, as the format is described in publicly available publications. Please see: https://msdn.microsoft.com/en-us/library/cc463912.aspx[^].

See also this very popular CodeProject article: Reading an Outlook MSG File in C#[^].

Sorry that his article is only for reading, but it still can be helpful.

See also: http://blogs.msdn.com/b/openspecification/archive/2009/11/06/msg-file-format-part-1.aspx[^].

—SA
 
Share this answer
 
Comments
Member 11606873 15-Apr-15 10:50am    
Hi Sergey,
Thanks much for your response.

although the exception indicates that the mail item may be opened by another process, its not the case. also, I am running the application as an administrator.

I gone through the links shared by you. However nothing helped in this case.

One thing to note here is that the outlook mail items have some additional Extended Properties created. As expected, when I save these files in .eml format it doesnot brings the extended properties. So my question is, am I using the right way to extract the content as in the below lines?

mItem.Load(new PropertySet(ItemSchema.MimeContent));
MimeContent mc = mItem.MimeContent;
Sergey Alexandrovich Kryukov 15-Apr-15 11:00am    
No, you did not get the point. As administrator is one thing, locked by another process is totally unrelated.

1) How exactly did you run as administrator, and on what system?
2) Did you use handle.exe as I suggested?

—SA
Member 11606873 16-Apr-15 17:29pm    
Hi Sergey,
I was getting this error while opening the saved MSG files on directory.
anyways i found out that the main reason why this error was coming was because the MSG file was not created properly.
So I am exploring options on how to create the correct MSG file. In my case its a bit tricky as I have a lot of customization done on outlook.

Thanks
Sergey Alexandrovich Kryukov 16-Apr-15 18:11pm    
Well, maybe. I was just answering on what you could do with these problem if accessibility; and here it does not matter if the file is valid or not.
—SA

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