Move multiple mails to a folder in a different store - c#

I'm using Outlook Redemption library (http://www.dimastr.com/redemption/home.htm) for my Outlook AddIn. I want to move multiple mails from an exchange account to a PST store.
onlineAccountFolder.Items.MoveMultiple(onlineEntryIds, targetFolderInPstStore);
The source folder mails were cut from the Exchange account, but not pasted in the target folder. They are gone.
I tried the same operation on an Exchange account folder in the same store and the move operation was successful. The items were moved to the target folder.
There's no overload of the 'MoveMultiple' method where I can define a StoreID.

I had no problem with the following script executed from OutlookSpy (I am its author - click “Script Editor” button on the OutlookSpy toolbar, paste the script, click Run.
The script moves the messages selected in Outlook to a folder returned by the PickFolder method. Works as expected with both PST and Exchange target folders.
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
dim messages()
set sel = Application.ActiveExplorer.Selection
redim messages(sel.Count-1)
for i = 1 to sel.Count
messages(i-1) = sel.Item(i).EntryID
next
set targetFolder = Session.PickFolder
set sourceFolder = Session.GetFolderFromID(Application.ActiveExplorer.CurrentFolder.EntryID)
sourceFolder.Items.MoveMultiple messages, targetFolder

Use the Move method of the RDOMail class to move items between stores in Outlook.

Related

Outlook add-in error: The operation cannot be performed because the message has been changed

i am trying to create my own add-in for Outlook. My point is to extract some data from mails and then move these mails to Archive folder. When i open unread mails it works as i expect, but i got an error when i open unread mails in the moment when i am trying to move mail to Archive folder. I got an instance of mail from inspector. Here is some code.
Outlook.MailItem mail = inspector.CurrentItem as Outlook.MailItem;
var email = mail.UserProperties.Session.CurrentUser.Address;
Outlook.NameSpace ouNs = Globals.ThisAddIn.Application.GetNamespace("MAPI");
Outlook.MAPIFolder baseFolder = ouNs.Folders[email];
var archiveFolder = findFolderRecursive(baseFolder, archiveFolderName);
mail.Move(archiveFolder);
Messages returned from the inspector disallow some methods. Try to track the Inspector.Close event, store the message entry id in a variable, and enable a timer (use Timer class from the Forms namespace - it runs on the same thread). When the timer fires, disable it, open the item by its entry id using Namespace.GetItemFromID, then move it.

mailkit Add imap folder not visible

I'm trying to add folder to inbox as follows:
var _client = new ImapClient();
_client.Connect(hostName, portNumber, useSsl);
_client.Authenticate(username, password);
_client.Inbox.Open(FolderAccess.ReadWrite);
_client.Inbox.Create("Name", true);
Everything goes fine, the Create function returns created folder, the _client.Inbox.GetSubfolders() returns list that contains new created folder as well, however I can't see this folder in e-mail client application (such as Thunderbird).
What am I doing wrong?
Thunderbird may only be showing you subscribed folders. If that is the case, then you will also want to do newFolder.Subscribe ();
I should also point out that there's no reason to Open() the inbox before creating a child folder.
You only need to Open() a folder in order to read messages from it.

OpenSharedItem for opening .MSG files showing Error in Outlook C#

I am using the following code to open the signed/unsigned
Outlook messages and I display the content in WebBrowser control.
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
var item = app.Session.OpenSharedItem(msgfile) as Microsoft.Office.Interop.Outlook.MailItem;
string message = item.HTMLBody;
app.Session.Logoff();
It is working fine for the first time the file is opening, but after
closing the Outlook file trying to reopen the file it showing the
following error:
"Cannot open file: C:\tion.msg. The file may not exist, you may not
have permission to open it, or it may be open in another program.
Right-click the folder that contains the file, and then click
Properties to check your permissions for the folder."
After some time later it is opening fine. For this strange behavior
what could be the reason and how to rectify the the error message?
Outlook manages its own cache of items when you are opening and closing messages. Your best bet would be to use a randomly generated filename (i.e. Path.GetRandomFilename) when opening via OpenSharedItem so that you don't get issues. I would also use a temporary path instead of root c:\ (i.e. Path.GetTempPath).
You can try and free the MailItem reference (i.e. setting it to null), but there is no guarantee when Outlook will release the item from its cache.
Would any combination of the Quit[1], Close[2] or ReleaseComObject[3] methods work for you? My code worked better but not perfect after I used them.[4]
using Outlook = Microsoft.Office.Interop.Outlook;
.
.
.
var app = new Outlook.Application();
var item = app.Session.OpenSharedItem(msgfile) as Outlook.MailItem;
//Do stuff with the mail.
item.Close(OlInspectorClose.olDiscard);
app.Quit();
Marshal.ReleaseComObject(item);
Another solution, according to Microsoft - Help and Support[5], is to delay the opening of the file. However, it doesn't sound like a good solution to me since, like #SliverNinja also said, you'll never know when Outlook releases its lock of the file.
Notes and references
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._application.quit.aspx, read 2014-10-14, 16:19.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem.close%28v=office.15%29.aspx, read 2014-10-14, 16:19.
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx, read 2014-10-14, 16:19.
For example, if I had opened Outlook for som regular work, the Quit-method would close that window as well.
http://support2.microsoft.com/kb/2633737, read 2014-10-08, 16:19.
Hello you have two options .
set the Read-only attribute to the msg file
or
disable the following permissions for the users or usergroups to the parent folder:
Write Attributes
Write Extended Attributes
the msg file can now open multiple times but is write protect
I had this problem, in my case it was the space in the file name
import win32com.client
import os
path = 'C:/testes/mail'
files = [f for f in os.listdir(path) if '.msg' in f]
for file in files:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(os.path.join(path, file))
att=msg.Attachments
for i in att:
i.SaveAsFile(os.path.join('C:/testes/email_download', i.FileName))
I don't know if in your case the OpenSharedItem method can help ...
Firstly, try to release the message as soon as you are done with it using Marshal.ReleaseComObject(). This may or may not help since Outlook likes to cache its last opened item.
Secondly, you are logging off from Outlook while it might still be running and visible to the user - Outlook is a singleton, so you will end up with the existing instance if it was already running. Either don't call Logoff at all, or check that there are no open inspectors and explorers (Application.Explorers.Count == 0 && Application.Inspectors.Count == 0).
Thirdly, reading HTMLBody alone won't work properly if there are embedded images - they are stored as regular attachments. You can save the message as an MHTML file (which most browsers would be happy to show) using MailItem.SaveAs(..., olMHTML).
You can also use Redemption (I am its author) for that - call RDOSession.GetMessageFromMsgFile.
If you need to release the message immediately after you are done, call Marshal.ReleaseComObject()
In case of Redemption, you can also cast RDOMail object to the IDisposable interface and call IDisposable.Dispose(). In addition to the MHTML format, Redemption can also save in the HTML format with image attachments converted into embedded images- use RDOMail.SaveAs(..., olHTMLEmbeddedImages) (olHTMLEmbeddedImages == 1033).

Modify File Properties in C# Other Than Date

I'm working in .NET 3.5, Visual Studio 2010. I'm working on an Outlook Add-In that saves some email into a folder. I've gotten it to work using the Microsoft.Office.Interop.Outlook.MailItem.SaveAs function. However, the file properties have only the current time (time when the file was exported through the Add-In) as their Date Modified/Date Created etc., and other properties such as To, From, CC, BCC are not there.
If you open a folder in Windows Explorer (I'm using Windows 7), go to the top where it says Name, Date Modified, Type, etc., you can click on More, and see other various columns that might be relevant like "Album Artist", "To", "From", etc.
C# has a really easy way to modify the timings, File.SetCreationTime(filename, DateTime object);. However, there's no .SetTo or .SetAlbumArtist or anything like that. How would I go about modifying those properties?
Update 1: through research, I found this link: Read/Write 'Extended' file properties (C#), so that might contain the answer...but I have no idea how. The accepted answer mentions running a method on a shell using a .dll. The second answer contains C# code, a commenter then asked basically what I want to know (how to modify the properties of a particular file), and the next commenter responded with "you can't set these"...so I'm still at square 1.
Update 2: I also tried the following:
foreach (Object selectedObject in explorer.Selection)
{
Outlook.MailItem email = (selectedObject as Outlook.MailItem);
//Modify the information about the email
email.To = "I filled in To";
email.SaveAs(filename, OlSaveAsType.olMSG);
}
This code successfully grabs the selected email(s) and save them under filename. However, the email.To = "I filled in To" changes the information when you open the .msg, but not the file properties.
This cannot be changed, because it actually is not any file property in the terms of the filesystem (like file creation or modification datetime).
Columns in Windows Explorer you are talking about are "virtual" and they are "only" the feature of Windows Explorer. It "understand" content of some file types and it can handle showing and sorting columns like that.
If you want to change To, From etc. you have to change the content of the file you are saving, i.e. change the To or From in the message.
To do so, if You have an Microsoft.Office.Interop.Outlook.MailItem object (which you are just saving), set desired properties on that object before you save it to file, i.e.:
MailItem mail = ...;
mail.To = "some new to";
mail.Subject = "new subject";
mail.SaveAs(fileToSave, OlSaveAsType.OlMSG);
I don't know if it also changes email stored in the Outlook, if it so, create a copy of the email before changing properties
MailItem copyOfMailToSave = (MailItem)mail.Copy();

Outlook 2010 - C# - Get the account associated to a mail

I'm creating an Outlook add-in that can save selected emails to an external database.
Using the Office.IRibbonControl I can get the list of the selected email, but I need to know to which account those mails are associated.
I mean, if Outlook get messages from toto#exemple.com and from otot#exemple.com, when I want to save a message I need to know that information.
I can't use the informations like sender / receiver because it can be an outcome like an income email.
Currently, the only I have found is using the current folder path..
public void SayHello(Office.IRibbonControl control)
{
MessageBox.Show(
"Folder: " + (control.Context as Outlook.Explorer).CurrentFolder.FolderPath,
"Test",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
But the method isn't good enough. If I open a message (in a separated window) and then I change the current folder, it fails.
Also, Outlook.Explorer.CurrentAccount does not work as I expected.
So here is my question :
How can I access the related account having a Outlook.MailItem object ?
You can get the parent folder (MailItem.Parent) of an Outlook.MailItem to determine its Folder path (Folder.FolderPath).
Outlook.Folder parent = MailItem.Parent as Outlook.Folder;
string itemPath = parent.FolderPath;

Categories

Resources