Outlook Redemption DLLs - Unable to delete emails in PST - outlook

I am using the below command to delete emails from PST.
foreach (Redemption.RDOMail oitem in filteredItems)
{
try
{
oitem.Delete();
}
catch (Exception ex)
{
PSTLog.Log("Exception in DeleteEmails: " + ex.Message);
}
}
The Redemption DLLs indicate the emails have been deleted successfully. If I try to read PST again using Redemption DLLs, I get lower email count which makes sense. However, I am still able to see the deleted emails in Outlook. Tried options like closing/reopening Outlook & detaching/re-attaching PST in Outlook, but it has not helped.
Is it possible Outlook is caching the results elsewhere and causing this discrepancy? Outlook version is 2016.
Any help would be appreciated!!

Do not use foreach loops if you are modifying the collection. Use a down "for" loop:
foreach ( int i = filteredItems.Count; i > 0; i--)
{
Redemption.RDOMail oitem = filteredItems[i];
try
{
oitem.Delete();
}
catch (Exception ex)
{
PSTLog.Log("Exception in DeleteEmails: " + ex.Message);
}
Marshal.ReleaseComObject(oitem);
}

Related

Integration of Outlook calendar, GSUIT calendar And iCal programatically C#

I have a specific requirement, I have a .Net Application where I have some dates, so I want to create appointments for these dates in Outlook, GSuit and iCal calendar.
Can someone help me to give steps to create these appointments programmatically C#.
I tried to achieve by adding reference Microsoft.Office.interop.outlook and below code. i got this code from Microsoft site but I have difficulty to replace "this". what code should replace "this" in below code.
{
try
{
Outlook.AppointmentItem newAppointment =
(Outlook.AppointmentItem)
this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
newAppointment.Start = DateTime.Now.AddHours(2);
newAppointment.End = DateTime.Now.AddHours(3);
newAppointment.Location = "ConferenceRoom #2345";
newAppointment.Body =
"We will discuss progress on the group project.";
newAppointment.AllDayEvent = false;
newAppointment.Subject = "Group Project";
newAppointment.Recipients.Add("Roger Harui");
Outlook.Recipients sentTo = newAppointment.Recipients;
Outlook.Recipient sentInvite = null;
sentInvite = sentTo.Add("Holly Holt");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType
.olRequired;
sentInvite = sentTo.Add("David Junca ");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType
.olOptional;
sentTo.ResolveAll();
newAppointment.Save();
newAppointment.Display(true);
}
catch (Exception ex)
{
MessageBox.Show("The following error occurred: " + ex.Message);
}```

Office.Interop how to find email using PR_SEARCH_KEY in sent Folder

I am using Outlook Object model (Interop) for my softawre.
Before I send email, I get and keep the PR_SEARCH_KEY of the email that we created.
If I want to find the email in sent folder using PR_SEARCH_KEY, how can I do that in c# using Office.Interop (not EWS or not redemption)?
I tried to find it from SentFolder.Items.Find(filter). But it does not work as the PR_SEARCH_KEY is binary.
Thanks !
public Outlook.MailItem FindEmailFromSentFolder(string emailId)
{
try
{
if (_sentFolderItems == null)
return null;
// find the sent mail from sent folder based on PR_Serach_Key
var filter = string.Format("#SQL=\"http://schemas.microsoft.com/mapi/proptag/0x300B0102\" = '{0}'",
emailId);
var item = _sentFolderItems.Find(filter);
if (item != null && item is Outlook.MailItem)
return item as Outlook.MailItem;
}
catch (Exception ex)
{
return null;
}
return null;
}
As you already noticed, OOM won't let you search for any binary property - you will need Extended MAPI (C++ or Delphi) or Redemption (any language).
Your best bet is to set some string property on the outgoing message and then look for it in the Sent Items folder.

How to get contact's photo from exchange server

Based on this tutorial, I'm trying to get contacts photos
private String createPhoto() {
try {
AttachmentCollection attachments = contact.getAttachments();
for (Attachment attachment : attachments.getItems()) {
if (attachment instanceof FileAttachment) {
boolean isPhoto = ((FileAttachment) attachment).isContactPhoto();
if (isPhoto) {
attachment.load();
FileAttachment photo = contact.getContactPictureAttachment();
String filename = photo.getName() + ".jpg";
photo.load(new FileOutputStream(filename, true));
return filename;
}
}
}
} catch (Exception ex) {
LOGGER.info("" + ex);
}
return null;
}
However, attachments.getItems() is always an empty array.
On my mailbox, I have few contacts with photos, and I can receive them by calling URL https://companyname.exchange.com/EWS/Exchange.asmx/s/GetUserPhoto?email=name#company.exchange.com&size=HR360x360
Why I can't get a photo from the code?
On my mailbox, I have few contacts with photos, and I can receive them by calling URL https://companyname.exchange.com/EWS/Exchange.asmx/s/GetUserPhoto?email=name#company.exchange.com&size=HR360x360
That request gets the Userphoto which is stored in the (Source)Users Mailbox (or low res in ActiveDirectory) and made available by that operation.
Your code is trying to retrieve the ContactPhoto which can be stored as an attachment on Contacts in a UserMailbox.
So these are two separate things so which one are you dealing with ?, As you haven't shown it you need to make sure you ExchangeServerRequest Version is set to 2010 or greater as Contact photos aren't returned in 2007. You might also want to test quickly the contacts in Question with the EWS Editor https://ewseditor.codeplex.com/ that will allow you to get the objects and see if there is a ContactPhoto Attachments using EWS.

Custom outlook plug in not downloading as expected

I have written a custom code such that when ever an email arrives which has an attachment,it must get downloaded into a shared location, and email arrives daily.
When I open my laptop daily it's working fine if i don't open and if there are continuous mails (with attachments) and it is not getting downloaded for example, when i open my laptop on monday I have 3 mails with attachment (from satu, sunday, monday).
It is not downloading the latest report from monday it is still showing me same report on saturday.
Here is my code..
private void ThisAddIn_Startup(object sender,System.EventArgs e) {
outlookNameSpace=this.Application.GetNamespace("MAPI");
inbox=outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
items=inbox.Items;
items.ItemAdd +=new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}
private void items_ItemAdd(object Item) {
Outlook.Items inboxitems;
const string destinationDirectory=#"\\Service Now\";
Outlook.MailItem newEmail=null;
inboxitems=inbox.Items.Restrict("[Unread] = true");
try {
foreach (object collectionItem in inboxitems) {
newEmail=collectionItem as Outlook.MailItem;
if (newEmail !=null) {
if (newEmail.Attachments.Count > 0) {
for (int i=1; i <= newEmail.Attachments.Count; i++) {
if (newEmail.Attachments[i].FileName.Contains("Logic")) {
// String Des= destinationDirectory.Remove(0, 1);
newEmail.Attachments[i].SaveAsFile(destinationDirectory + newEmail.Attachments[i].FileName);
// MessageBox.Show("Hurry");
}
}
}
}
}
}catch (System.Exception ex) {
MessageBox.Show(""+ex);
}
}
Did you try to debug the code?
I see the following conditions in the code:
inboxitems= inbox.Items.Restrict("[Unread] = true");
and
if (newEmail.Attachments[i].FileName.Contains( "Logic"))
Make sure that emails corresponds to the conditions shown above.
Be aware, the ItemAdd event of the Items class is not fired when a large number of items are added to the folder at once (more than 16).
P.S. Try to check out the MarkForDownload property of Outlook items - an OlRemoteStatus value that determines the status of an item once it is received by a remote user.

How can I access to outlook folders which have MS Exchange Account

I have written the following code to access outlook folders.
MSOutlook.Application app = new MSOutlook.Application();
MSOutlook.NameSpace ns = app.GetNamespace("MAPI");
try
{
foreach (MSOutlook.Folder folder in ns.Folders)
{
...
}
}
catch (Exception ex)
{
...
}
but this code throw exception at ns.Folders and this exception means
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
However, I can get folders using the same code in the environment which has no exchange account.
How can I get the folders in the environment which has MS Exchange account?
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Outlook._NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder taskFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderTasks);
foreach (Microsoft.Office.Interop.Outlook.Folder folder in ns.Folders)
{
System.Diagnostics.Debug.WriteLine("Folder ----"+folder.Name.ToString());
}
This works for me fine..

Resources