How to check existence of a property in Outlook Interop? - com-interop

I am trying to determine sender of a email in Outlook 2007 and above. In Outlook 2010 you have a Sender property on the MailItem object while in Outlook 2007 you have to do it differently like mentioned in this question.
So now I need to know whether current version of Outlook supports the Sender property, and if it does not, use the other method. The reason for doing this is I would prefer to use the Sender property for compatibility with future versions of Outlook rather than having condition on version of Outlook.
So the question is how do I determine whether a property exists in Outlook Interop ? Obviously, this being a COM object I cannot use reflection here.

I used the MailItem.ItemProperties collection to check for the "Sender" property. Below is the code
Microsoft.Office.Interop.Outlook.MailItem myMail;
//Code to get the mail
....
Microsoft.Office.Interop.Outlook.ItemProperties mailProps = myMail.ItemProperties;
Microsoft.Office.Interop.Outlook.ItemProperty mailProp = mailProps.Item ("Sender"); //the parameter is case-sensitive
if(mailProp != null)
{
//get email address using Sender object
Microsoft.Office.Interop.Outlook.AddressEntry theSender = myMail.Sender;
}
else
{
//use alternate method for Outlook 2007
}

You can use IDispatch::GetIDsOfNames to see if the property exists

Related

MAPI showing details of contacts

We are currently using MAPI to load contact information into a form.
Within a MapiSession we are creating a RDOAddressEntry "recepient" with this bit of code
using (MapiSession session = new MapiSession())
{
//open outlook contact dialog
RDOAddressEntry recipient = session.GetAddressEntryFromID(contact.EntryId);
if (recipient.Type == null)
{
throw new ArgumentException("type not defined");
}
recipient.Details(handle.ToInt32());
}
Our problem seems to be that the dialog that opens with the last line of code creates two different dialogs. One for exchange contacts and another one for SMTP contacts.
In the last version of our application it was always opening the same dialog for both RDOAddressEntry-types and we did NOT change anything in our code...
Can you help me fix this issue so that both SMTP and exchange will bring the same dialogs again?
I am not sure why you were getting the same dialog for both before - the dialog is actually provided by the particular address book provider, so it will be different for the entries from different providers.

How to add custom property to InternetHeaders of Outlook MailItem

I need to add custom InternetHeader x-auth-guid to existing MailItem in Outlook inbox (in Exchange account) from VSTO add-in. Something like described here
https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd633654(v=exchg.80)
but without EWS.
The code using EWS work and do something like:
extendedFieldURI.propertyName = "x-auth-guid"
extendedFieldURI.distinguishedPropertySetId = "InternetHeaders"
property.extendedFieldURI = extendedFieldURI
property.value = xauthGuid
message.addExtendedProperty(property)
But in add-in both PropertyAccessor like:
mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/x-auth-guid", xauthGuid);
mailItem.Save();
and Redemption
rdoMail.Fields["http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/x-auth-guid"] = xauthnGuid;
rdoMail.Save();
seems don't work. Am I missing something?

VSTO (Outlook) is forcing MSG attachments to be of olEmbeddeditem type, but olByValue needed

I need to send email with .msg file attached to Lotus Notes (Domino) by using VSTO (from Outlook Add-In). When this file is received in Lotus Notes, the email body from msg file is appended to the end of the main mail.
From this problem I am assuming that the issue is in attachment type.
I am trying to set the attachment type to by olByValue by adding attachment :
mail.Attachments.Add(msgFilePath, OlAttachmentType.olByValue, 0, displayName);
but whatever type I specify, it is still set to olEmbeddeditem.
Is there any way, how to force msg attachment to be olByValue?
Thank you for any suggestion or advise.
Have a nice day.
Note: For reference I have created email in Lotus-Notes too with msg attached, sent to Outlook and than forwarded back to Lotus-Notes and msg is attached not appended to the end.
There is not much you can do in the Outlook Object Model - it always tries to be "helpful" and converts MSG files to embedded message attachments. If using Redemption is an option (I am its author), it will not change the type:
SafeMailItem sItem = new SafeMailItem();
sItem.Item = mail;
sItem.Attachments.Add(msgFilePath);

Outlook.Application.CreateItem with Office365 says: The operation failed

We've written an Outlook plugin using Add-in-Express. Code:
private void CreateShowMessageUsingCreateItem(Outlook._Application OutlookApp)
{
Outlook.MailItem mail = null;
try
{
mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Save();
mail.Display(false);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (mail != null) Marshal.ReleaseComObject(mail);
}
}
Works when Outlook is connected to Exchange. Fails when
Outlook is connected to Office365. Error:
Exception: System.Runtime.InteropServices.COMException (0x80004005): The operation failed.
at Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType)
at DocuSignInk.DSToolbox.ShowResponse(MailItem senderEmail) in C:\docusign_source\Ink_Outlook\DocuSignInk\DSToolbox.cs:line 540
Some research indicates that you need to release your objects
when making these calls in a loop. But I'm not in a loop. I
can't even get one call to work, so I don't get to the point
where I can release anything.
Testing with Wireshark and Charles indicates that the problem is
in the client. I was trying to see if there is a more detailed
error coming from the server, but there's no traffic to the server
at all.
A quick Python script works from the command line.
import win32com.client
outlook = win32com.client.Dispatch('Outlook.Application')
mail = outlook.CreateItem(win32com.client.constants.olMailItem)
mail.Save()
mail.Display(False)
So it must be something in the client. I'm guessing maybe thread-related?
This can happen if you haven't activated Microsoft Office (which includes Outlook).
Short, test add-ins can work if they run right away before the activation check happens.
Once the activation check happens the API calls will fail.
Firstly, the code is correct, no additional releasing is required. The most obvious reason is that your Outlook, when connected to Exchange Online, cannot create a mail item. You can check this with the following VBA macro:
Public Sub CreateEmailItem()
Dim mail As Outlook.MailItem
Set mail = Application.CreateItem(olMailItem)
mail.Save
mail.Display (False)
Set mail = Nothing
End Sub
Regards,
Dmitry Kostochko (Add-in Express Team)
I had the same exception along with HResult that was -2147467259.
The direct reason was an outlook popup window informing about its trial version or an outlook closing process which is pending after you have closed previous activities in outlook including an email that was showed up by executing the above code (this process is indicated by a respective tray icon in the taskbar until it disappears).
You need to close the window first or wait for the tray icon to disappear before you execute creating new email.

Send email attachment to a local folder

I am trying to send an email from a client PC (i.e. Windows) with an attachment and have the attachment saved to a local folder on the same client PC. I have looked at a couple of alternatives, such as MailDrop (email to dropbox) and Outlook 2003 Interop library - but want to make sure I am implementing this the best way.
Does anyone have any different ideas on a simple/elegant solution?
As long as you know Outlook will be installed on all the clients the Outlook solution works very well. You can create a file and save it, then in your outlook interop you just attach and send. You didn't specify what tools you are using but here's the basic email creation method I use for Outlook in C# (Where OutlookSetup.OutlookApp is just a static method that returns the currently open instance of the Outlook application or creates a new one if Outlook isn't open). Otherwise there are several examples here on SO of using SmtpClient to achieve similar ends.
public EmailMessage(EmailInfo emailInfo, string filenameToAttach=null)
{
Message = OutlookSetup.OutlookApp.CreateItem(OL.OlItemType.olMailItem);
Message.To = emailInfo.To;
Message.CC = emailInfo.Cc ?? "";
Message.Subject = emailInfo.Subject;
if (filenameToAttach != null)
{
Message.Attachments.Add(filenameToAttach);
}
}

Resources