I'm writing piece of code which will handle extraction of information from email stored in *msg Outlook file. The idea how to do it I took from C# Outlook interop and OpenSharedItem for opening MSG files. But when calling method OpenSharedItem a get such error System.AccessViolationException. Anyone know what's the problem?
Here is code causing error
Outlook._Application app = new Outlook.Application();
Outlook.NameSpace NS = app.GetNamespace("MAPI");
Outlook.MAPIFolder inboxFld = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MailItem fld = (Outlook.MailItem) app.Session.OpenSharedItem("E://Projects//C#//message1.msg");
Ok. In case anybody will face the same problem finally I've got an answer. First of all should be
Outlook.MailItem fld = (Outlook.MailItem) app.Session.OpenSharedItem("E:\\Projects\\C#\\message1.msg");
difference is in the slashes.
Then all you need to make it work is Outlook 2007 or newer. And that's it :]
Related
I try to read some outlook msg files with Microsoft.Office.Interop.Outlook Dll. I know Outlook must be installed on machine.
I use this code to read the msg file. This works fine.
Microsoft.Office.Interop.Outlook._Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem fld = (Microsoft.Office.Interop.Outlook.MailItem)app.Session.OpenSharedItem(filename);
But if I open the same msg-file an exception is thrown, becouse the file is already opened. I think the gc is not clearing the objects.
How can I release the objects?
Probably you solved your problem nowadays, but you could try this:
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItem) != 0) { }
mailItem = null;
GC.Collect();
GC.WaitForPendingFinalizers();
I am trying to send a mail on Outlook 2013 startup programmatically that has programmatically added attachments. Then the programm takes these attachments and creates a Password-locked Zip File and also adds this File as attachment. My Problem is, that I keep getting an
"System.IO.FileNotFoundException"
when executing
mail.Attachments.Add(pathToAttachment1);
I thought the path might not exist, but I can add this Path to a new zip-directory (using the same String), so it seems to be a Problem with the
Attachments.Add(Object)-Method.
MSDN says it is totally fine to give it a Pathname as String. In VBA it even worked, but in c# not (for whatever reason). Does somebody have an idea what i am doing wrong?
The Code i want to execute:
String pathToAttachment1 = #"C:\Testfile.txt";
//create a new ZipFile
ZipFile zipAttachment = new ZipFile("EncodedAttachments.zip");
zipAttachment.Password = "1234";
//Create a new MailItem
Outlook.MailItem mail = Application.CreateItem(Outlook.OlItemType.olMailItem);
//set Receiver, Body and Subject for the MailItem
mail.To = "foo";
mail.Body = "This is the body.";
mail.Subject = "This is the Subject.";
//This is working:
zipAttachment.AddFile(pathToAttachment1);
//This is the line where the Exception is thrown:
mail.Attachments.Add(pathToAttachment1);
This code is called in the Startup-Method handling the Outlook Startup Event. Does anyone have an idea what i might do wrong or could change to make it work?
I want to open a specific email in Outlook from my C# winforms application.
At the moment I have got the following code:
//...Get Folder & Entry ID for last Email in Sent Box
Outlook.Application myApp = new Outlook.ApplicationClass();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder mySentBox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
Outlook.MailItem myMail = ((Outlook.MailItem)mySentBox.Items[1]);
string guid = myMail.EntryID;
string folderEntryID = mySentBox.EntryID;
string folderStoreID = mySentBox.StoreID;
string mailAddressee = myMail.To;
MessageBox.Show(mailAddressee);
//...Attempt to Open that Email at a later date
Outlook.MAPIFolder getFolder = (Outlook.MAPIFolder)mapiNameSpace.GetFolderFromID(folderEntryID, folderStoreID);
Outlook.MailItem getItem = (Outlook.MailItem)getFolder.Items.Find("[EntryID] = " + guid);
getItem.Display();
The first an second parts of the code will be run at different times, although they are in the same Method for testing at the moment.
The first part obtains the relevant IDs for the mst recently sent email from Outlook. This part seems to work fine as evidence my the Messagebox I've built in.
The second part however is not working and I'm struggling to find the right code to access and open that specific mailItem having obtained its ID and folder location in the first part.
Anyone able to complete this little project for me please.
Done it at last by replacing the following lines of code . . .
Outlook.MAPIFolder getFolder = (Outlook.MAPIFolder)mapiNameSpace.GetFolderFromID(folderEntryID, folderStoreID);
Outlook.MailItem getItem = (Outlook.MailItem)getFolder.Items.Find("[EntryID] = " + guid);
with this . . .
Outlook.MailItem getItem = (Outlook.MailItem)mapiNameSpace.GetItemFromID(guid, folderStoreID);
You can not use EntryId with the _Items.Find method. The MSDN reference is here.
There is also a remark which could be interesting for your project:
"The Entry ID changes when an item is moved into another store, for
example, from your Inbox to a Microsoft Exchange Server public folder,
or from one Personal Folders (.pst) file to another .pst file.
Solutions should not depend on the EntryID property to be unique
unless items will not be moved."
MailItem.EntryID Property (Outlook).
I am new to C# and trying to find a way to close windows after all new messages are saved in the draft folders.
I have the following code which does close all message windows, but at the same time, it shuts down the entire outlook.
I think there are two ways to resolve but don't know how to code...
close all active new message windows without shutting down outlook
relaunch outlook inbox after shutting down outlook.exe
// now generate emails
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem newMail = oApp.CreateItem(Outlook.OlItemType.olMailItem);
newMail.Subject = subject_header;
newMail.Recipients.Add(email_add[i]);
newMail.Recipients.ResolveAll();
newMail.Body = body;
newMail.HTMLBody += summary_html;
//Add attachment
foreach (string attachment in attachments)
{
newMail.Attachments.Add(attachment, Outlook.OlAttachmentType.olByValue);
}
newMail.Display(false);
newMail.Save();
oApp.Quit()
I want to get all events from all calendars, how do i iterate thru all calendar folders and then all events for each calendar?
If I had to guess, though I'm just getting in to Outlook myself, I would suggest the following:
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder folder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Then something along the lines of
foreach (outlook.MAPIFolder subFolder in folder.Folders)
{
// do something with subFolder
}
And you could probably create something recursive to exhaust all possibilities of the MAPIFolder.Folders property.
EDIT Ultimately, try stepping through in the debugger one you've gotten the default folder and see what you're left with. My guess is this will have the information you need.