Eml to pst Redemption C# -> Null Exception RDOMail - c#

i'm trying to create a program that conver eml file into a single pst.
i have write that command:
RDOSession session = new RDOSession();
RDOPstStore store = session.LogonPstStore(newpstpath);
RDOFolder folder = store.IPMRootFolder.Folders.Item(directoryEmlFile);
RDOMail mail = folder.Items.Add("IPM.Note");
but at the command "RDOMail mail = folder.Items.Add("IPM.Note")" the system give me the null exception. can anyone help me?

You need to differ Outlook and Redemption objects.
It looks like you need to use the GetRDOObjectFromOutlookObject method of the Session class (Redemption).
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set redItem= Session.GetRDOObjectFromOutlookObject(mail)
The Add method of the Items class creates and returns a new Outlook item in the Items collection for the folder.

Most likely the folder variable is null - if the folder does not exist, retrieving it by name (RDOFolder.Folders.Item("foldername")) will return null:
RDOSession session = new RDOSession();
RDOPstStore store = session.LogonPstStore(newpstpath);
RDOFolder folder = store.IPMRootFolder.Folders.Item(directoryEmlFile);
if (folder == null) folder = store.IPMRootFolder.Folders.Add(directoryEmlFile);
RDOMail mail = folder.Items.Add("IPM.Note");

Related

How do I check if folder and sub folder exist in Outlook using EWS Web Service & C#

I'm very new to C# development.
I'm trying to check and create a folder/ sub-folder exist in Outlook Mailbox using Exchange Web Service.
Folder Structure
MAIN_folder
Sub Folder-1
Sub Folder-2
Sub Folder-3
Implementation
public void checkFolderExistOrNot( String folder_name)
{
FolderView fv = new FolderView(100);
var findFoldersResults = exchangeService.FindFolders(
WellKnownFolderName.Inbox,
new SearchFilter.SearchFilterCollection(
LogicalOperator.Or,
new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, folder_name)),
fv);
foreach (var folder in findFoldersResults)
{
if (folder is Folder)
{
if (folder.DisplayName == folder_name)
{
archiveFolderID = folder.Id;
}
}
}
//if archive folder not found create and assign the variable to the folderID
if (archiveFolderID == null)
{
Folder folder = new Folder(exchangeService);
folder.DisplayName = folder_name;
folder.Save(WellKnownFolderName.Inbox);
archiveFolderID = folder.Id;
}
}
checkFolderExistOrNot(MAIN_folder)
checkFolderExistOrNot(MAIN_folder.Sub Folder-1)
checkFolderExistOrNot(MAIN_folder.Sub Folder-2)
checkFolderExistOrNot(MAIN_folder.Sub Folder-3)
But this is only creating the Main folder under the inbox. It would be greatly appreciated if someone could help me to identify what is missing in my implementation.
Thanks in Advance.
The only way to tell if a folder exists is to search for it with your search because you don't specify the traversal in the Folderview it will always be shallow. If you specify a deep traversal in
FolderView fv = new FolderView(100);
fv.Traversal = FolderTraversal.Deep;
You should then be able to find the parent folder you want to create a new subfolder on. Your logic should work okay as long as you don't have any name clashes a different folder levels. Otherwise what I do is this Exchange Web Service FolderId for a folder created by user or Get to an Exchange folder by path using EWS
Have you given Microsoft Graph a look?
You can basically use it for anything in Microsoft 365. With you you can also achieve your goal.
You will need to create a GraphServiceClient and with it you can do the following to check if a folder exists:
string user = "emailAddressOfTheUser";
var parentFolderRequest = graphClient.Users[user].MailFolders.Inbox.ChildFolders
.Request()
.Filter($"startsWith(displayName, 'parentFolderName')");
var parentMailFolder = await parentFolderRequest.GetAsync(cancellationToken);
Once you have the parent folder you can get it's ID and once you know that you can search it for child folders:
var parentMailFolderID = parentMailFolder.First().Id;
var childFolderRequest = graphClient.Users[user].MailFolders[parentMailFolderID].ChildFolders
.Request()
.Filter($"startsWith(displayName, 'childFolderName')");
var childMailFolder = await parentFolderRequest.GetAsync(cancellationToken);
If the childMailFolder.Count > 0 then the folder exists, if not you create the child folder:
var childFolder = new MailFolder
{
DisplayName = "childFolderName",
IsHidden = false
};
await graphClient.Users[graphUser.Id]
.MailFolders[parentMailFolderID].ChildFolders
.Request()
.AddAsync(childFolder );

EWS how do I create a Searchfolder in my own mailbox?

I use following Code to Create a SearchFolder but as it gets to the "Save" line it throws following error:
The email address associated with a folder Id does not match the
mailbox you are operating on.
private SearchFolder CreateSearchFolder( string email, SearchFilter filter)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new WebCredentials("mailboxworker", "password");
service.AutodiscoverUrl(email);
FolderId folderId = new FolderId(WellKnownFolderName.Inbox, new Mailbox(email));
FolderId searchFolderId = new FolderId(WellKnownFolderName.SearchFolders, new Mailbox(email));
// Create the folder.
SearchFolder searchFolder = new SearchFolder(service);
searchFolder.DisplayName = "Folder of " + email;
searchFolder.SearchParameters.SearchFilter = filter;
// Set the folder to search.
searchFolder.SearchParameters.RootFolderIds.Add(folderId);
// Set the search traversal. Deep will search all subfolders.
searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Deep;
// Call Save to make the EWS call to create the folder.
searchFolder.Save(searchFolderId);
return searchFolder;
}
What am I doing wrong?
associated with a folder Id does not match the mailbox
All the times I've bumped into this I've fixed it using the Microsoft.Exchange.WebServices.Data WellKnownFolderName enum instead of a string folderId
Here is a working example from MSDN: Create a search folder by using the EWS Managed API
This example assumes that the ExchangeService object has been initialized with valid values in the Credentials and Url properties.
using Microsoft.Exchange.WebServices.Data;
static void CreateSearchFolder(string email)
{
// Create the folder.
SearchFolder searchFolder = new SearchFolder(service);
searchFolder.DisplayName = "From Developer";
// Create a search filter to express the criteria for the folder.
EmailAddress developer= new EmailAddress("Jeremy#stackoverflow.com");
SearchFilter.IsEqualTo fromManagerFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.Sender, developer);
// Set the search filter.
searchFolder.SearchParameters.SearchFilter = fromManagerFilter;
// Set the folder to search.
searchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);
// Set the search traversal. Deep will search all subfolders.
searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Deep;
// Call Save to make the EWS call to create the folder.
searchFolder.Save(WellKnownFolderName.SearchFolders);
}
Here is another example on MSDN Creating search folders by using the EWS Managed API 2.0

Read Encrypted S/Mime E-mail with Outlook OOM or Redemption RDO

here's my attempt:
Outlook.Application app = new Outlook.Application();
RDOSession session = new RDOSession();
session.MAPIOBJECT = app.Session.MAPIOBJECT;
RDOFolder inbox = session.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);
RDOItems items = inbox.Items;
RDOMail mail = items.GetFirst();
if (mail.MessageClass == "IPM.Note.SMIME") {
RDOEncryptedMessage encryptedMessage = (RDOEncryptedMessage)session.GetMessageFromID(mail.EntryID)
// from here I am stuck because encryptedMessage is null
}
What am I doing wrong ?
Why do you call GetMessageFromID instead of casting mail to RDOEncryptedMessage? Are you sure you actually get an encrypted message from Items.GetFirst? Do not expect to get the very first message you see in Outlook - you do not sort the Items collection, and most likely GetFirst will return the oldest message in the folder, not the topmost message you see in Outlook's explorer.

How to retrieve the Outlook folder of a mail item (Outlook.MailItem)?

I am getting my default inbox folder via inboxFolder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox
Elsewhere in my code, I begin doing a foreach loop to extract specific information I want from these MailItems
foreach (var item in this.inboxFolder.Items)
{
Outlook.MailItem mailItem = (Outlook.MailItem)item;
//.... doing stuff here
string SenderEmail = mailItem.SenderEmailAddress;
string SenderName = mailItem.SenderName;
string FolderLocation = mailItem.???; //how to retrieve folder location?
//.... more stuff here
}
For example: A user may have created a subfolder called 'Test' shown below.
Thank you for the pointer guys. However I was having some trouble implementing the same initially. Here is how I solved it, just in case if some one faces the same issue.
Outlook.MAPIFolder parentFolder = mailItemToDelete.Parent as Outlook.MAPIFolder;
string FolderLocation = parentFolder.FolderPath;
The Parent object is dynamic and hence was causing issue.
Do you mean folder path? Use MAPIFolder.FullFolderPath. Or MAPIFoldert.Name if you only need the name.
Also keep in mind that the value will be the same for all items in the folder, so there is no reason to evaluate it on each step of the loop.

Adding a folder to a pst file from outlook in c#

I have been trying to find a way to add a folder to a pst file from c#
I have tried a whole bunch of code to try and get this to work and this is the one that seems to be most likly to be correct (as it is whats on the MSDN) but still does not work
Main {
Outlook._Application OutlookObject = new Outlook.Application();
Outlook.Store NewPst = null;
// create the pst file
string pstlocation = "C:\\Users\\Test\\Desktop\\PST\\Test.pst";
try
{
OutlookObject.Session.AddStore(pstlocation);
foreach (Outlook.Store store in OutlookObject.Session.Stores)
{
if (store.FilePath == pstlocation)
{
// now have a referance to the new pst file
NewPst = store;
Console.WriteLine("The Pst has been created");
}
}
}
catch
{ }
// create a folder or subfoler in pst
Outlook.MAPIFolder NewFolder;
NewFolder = NewPst.Session.Folders.Add("New Test Folder", Type.Missing);
}
This code creates a new PST File and then trys to add a folder to it however the last line of code:
New NewFolder = NewPst.Session.Folders.Add("New Test Folder", Type.Missing);
Gets the error "The operation failed." and "Invalid Cast Exception" can some one point out what i am doing wrong
Thanks in advance
You need to use Store.GetRootFolder() to get a handle to the root folder of that store (not Store.Session). So you would use:
// create a folder or subfolder in pst
Outlook.MAPIFolder pstRootFolder = NewPst.GetRootFolder();
Outlook.MAPIFolder NewFolder = pstRootFolder.Folders.Add("New Test Folder");
I recommend bookmarking both of the following: The PIA documentation isn't always complete, so it's worth checking out the COM documentation as well for complete class and member info.
(.NET): Outlook 2007 Primary Interop Assembly Reference
(COM): Outlook Object Model Reference

Categories

Resources