Exception while reading properties of SMIME encrypted email - c#

I'm using Outlook 2019 add-in. I need to get mail item when it is moved to Sent items folder after the email is sent. I often send SMIME encrypted or signed and encrypted emails.
When I send encrypted or signed and encrypted email to most of the recipients then I can read mail item properties (using Outlook Object Model) without problem. MessageClass in such case is IPM.Note.
However I noticed that if I send encrypted or signed and encrypted email to myself (the same email in From and To) then I can't read almost all properties. I can read some like Class, DownloadState, IsConflict, MarkForDownload, Permission, Sent, Subject, Submitted, Unred or MessageClass which in this case is IPM.Note.SMIME but for others I get exception: "Your Digital ID name cannot be found by the underlying security system."
My cert is valid and seems to be ok as encrypted emails can be sent to me from another account.
Is it Outlook bug or I can do something to make Outlook read those properties using Outlook Object Model?
I also tried to use Extended MAPI and this time I could successfully read all properties. However I prefer to use OOM and I'm also curious why I can't read all properties using OOM only in specific case.

Related

Can't access the recipients of specific emails in Outlook

I have a somewhat interesting phenomenon occurring in Outlook 2019: when we are using Exchange accounts and toggle off the "Use Cached Exchange Mode to download email to an Outlook data file" option, we are no longer able to get the e-mail addresses for recipients via the Microsoft.Office.Interop.Outlook.PropertyAccessor interface (using the MAPI property PR_SMTP_ADDRESS) from an e-mail, in which we entered the recipient e-mails using the suggested Outlook contact. (eg. when the address is entered it looks like this: 'John Smith'). When we observed the item using OutlookSpy, we noticed that the specific property's value says MAPI_E_NOT_FOUND or is not present at all. I also noticed that in these cases, Outlook puts the corresponding e-mail addresses in the BCC field, which we obviously can't access when viewing the e-mail in the recipient's inbox. Is there a way for us to get the addresses, or is there some kind of workaround for this problem?
No property is guaranteed to be present. If the property is missing, try to use Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress (error and exceptions checks omitted).

Opening encrypted emails with c#

I'm working in Visual Studio 2012 to build a relatively simple add-in for Outlook 2010. Machines are Win 7 Enterprise 32 and 64. The purpose of the add-in is to decrypt email messages directly in the user's inbox so that they can be easily searched in .pst archives or saved to network folders as .msg files unencrypted. The emails are encrypted using the x.509 PKI with the private keys stored on a smart card which prompts the user for a PIN each time an encrypted email is opened. This is what I have so far:
private void buttonDecrypt_Click(object sender, RibbonControlEventArgs e) {
currentExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
if (currentExplorer.Selection.Count > 0) {
for (int i = 0; i < currentExplorer.Selection.Count; i++) {
Object selObject = currentExplorer.Selection[i+1];
if (selObject is Microsoft.Office.Interop.Outlook.MailItem) {
Microsoft.Office.Interop.Outlook.MailItem mailItem =
(selObject as Microsoft.Office.Interop.Outlook.MailItem);
mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x6E010003", 0);
mailItem.Save();
}
}
}
}
If I manually open an encrypted email in Outlook, I get prompted for my PIN which allows the email to open fine. Then if I close the email and hit my new button on the ribbon it does exactly what I want by re-saving the email unencrypted directly in my inbox. However, if I select more than one email at once, or don't open the message manually first, my code errors on mailItem.PropertyAccessor.Setproperty() with message COMException was unhandled by user code: Your Digital ID name cannot be found by the underlying security system. This tells me I need a way to prompt the user for their PIN prior to attempting to open the email.
I don't think there is much you can do using the Outlook Object Model alone. OOM tries very hard to represent signed or encrypted messages as regular IPM.Note items. It goes as far as exposing a fake IMessage MAPI object from the MailItem.MAPIOBJECT property.
When you set the PR_SECURITY_FLAGS property, Outlook knows to decrypt the message. There is no way to specify how the decryption will be done or provide any parameters. That property is nothing but a hack to encrypt / decrypt Outlook messages; it only works when the property is set using MailItem.PropertyAccessor.SetProperty.
Ultimately what you need is the data from the P7M attachment stored by the encrypted/signed message. You can see this in OutlookSpy (I am its author): if you click the IMessage button on the OutlookSpy toolbar (it uses MailItem.MAPIOBJECT), you will see the regular decrypted message. But if you click the IMAPISecureMesage in the IMessage window or open the message from the IMAPIFolder | GetContentsTable tab, you will see the raw encrypted message and its P7M attachment.
To extract that attachment, you will need either Extended MAPI (C++ or Delphi only) or a wrapper like Redemption (I am also its author - use RDOSession.GetMessageFromID). Redemption also exposes the RDOEncryptedMessage object that can be used for decryption .

How can you tell if invitations have been sent for a meeting in Outlook?

I am writing a COM add-in for Outlook using C#. If a meeting was saved and invitations were not sent, Outlook puts a message at the top of the form saying the invitations have not been sent. How do I determine programmatically that invitations have not been sent for a meeting?
I tried examining each Recipient and checking the MeetingResponseStatus. I would expect it to be OlResponseNone if the invitation has not been sent but OlResponseNotResponded if the invitations have been sent but no responses have been received. However, I always get OlResponseNone for recipients that either haven't been sent an invitation or that have but have not yet reponded. I therefore can't tell apart a meeting where invitations haven't been sent from one where they have been sent but no one has responded.
I've done most of my testing in Outlook 2007, but I believe the same holds true for 2003 and 2010, all of which I need to support.
I found a partial answer in this post. I need to read the DASL property "http://schemas.microsoft.com/mapi/id/{00020329-0000-0000-C000-000000000046}/80BE0102". In Outlook 2007 and leter, you can do this with a PropertyAccessor as follows:
appointment.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/id/{00020329-0000-0000-C000-000000000046}/80BE0102");
However, I need to also support Outlook 2003, and the PropertyAccessor property was added in Outlook 2007. I was therefore able to use Redemption's RDOMail object with the following code (error handling omitted).
var _session = New RDOSession();
_session.Logon();
var _message = _session.GetMessageFromID(appointment.EntryID, ((Outlook.MAPIFolder)appointment.Parent).StoreID);
return (bool)_message.Fields["http://schemas.microsoft.com/mapi/id/{00062002-0000-0000-C000-000000000046}/8229000B"];
Marshal.ReleaseComObject(_message);
_session.Logoff();
Marshal.ReleaseComObject(_session);

How to include the user signature in the email body when sending a customized email through Thunderbird, using C# 3.5?

We need to send email from our desktop application using C# 3.5. The requirement is to send the new email using the Thunderbird, not directly from our application. So we are setting certain properties for the new email like emialfrom, emailto, subject, body & attachments through code, we save it as an .eml file & then we open that .eml file in thunderbird using code:
The logic we are following is:
MyEmailClass eml = new MyEmailClass();
eml.Subject = "subject";
eml.SetHtmlBody(" email body");
eml.From = "from";
email.AddTo = "toemail#domain.com";
email.AttachmentPath = "attachmentpath";
email.SaveEml("myEmail.eml");
So now we have the the .eml file & we need to open it in the Thunderbird, we are using System.Diagnostics.Process.Start to open the eml file in Thunderbird:
System.Diagnostics.Process.Start(thunderbird exe path,myEmail.eml path);
The above works fine, however we have just one issue, the sender signature is not shown when the email opens in the Thunderbird.
Facts:
1- The user has valid signature associated with his account.
2- We are using Thunderbird 8.0
3- We are using C# 3.5
4- Thunderbird is the default email client on users systems.
Signatures are added by the email client when it creates the email body. Since you are creating the email body through code, you would have to programmatically insert the signature. Just to be clear, you are not necessarily launching thunderbird on the user's machine, you are launching whatever process is associated with the .eml file extension. If you want to include the singature from Thunderbird, you could look to see if any of their APIs help, but they look like they haven't been updated in years. You could also give your users the option of setting up their signature within your application.

Generate an e-mail to be downloaded by client and sent from their outlook account

One of the requirements for the application that I'm working on is to enable users to submit a debugging report to our helpdesk for fatal errors (much like windows error reporting).
I've been told that e-mails must come from a client's mail account to prevent the helpdesk getting spammed and loads of duplicate calls raised.
In order to achieve this, I'm trying to compose a mail message on the server, complete with a nice message in the body for the helpdesk and the error report as an attachment, then add it to the Response so that the user can download, open and send it.
I've tried, without success, to make use of the Outlook Interoperability Component which is a moot point because I've discovered in the last 6 hours of googling that creating more than a few Application instances is very resource intensive.
If you want the user to send an email client side, I don't see how System.Net.Mail will help you.
You have two options:
mailto:support#domain.com?subject=Error&body=Error message here...
get user to download email in some format, open it in their client and send it
Option 1 will probably break down with complex bodies. With Option 2, you need to find a format that is supported by all mail clients (that your users use).
With option 1, you could store the email details locally on your server against some Error ID and just send the email with an Error ID in the subject:
mailto:support#domain.com?subject=Error 987771 encountered
In one of our applications the user hits the generate button and it creates and opens the email in outlook. All they have to do is hit the send button. The functions is below.
public static void generateEmail(string emailTo, string ccTo, string subject, string body, bool bcc)
{
Outlook.Application objOutlook = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)(objOutlook.CreateItem(OlItemType.olMailItem));
/* Sets the recipient e-mails to be either sent by 'To:' or 'BCC:'
* depending on the boolean called 'bcc' passed. */
if (!(bcc))
{
mailItem.To = emailTo;
}
else
{
mailItem.BCC = emailTo;
}
mailItem.CC = ccTo;
mailItem.Subject = subject;
mailItem.Body = body;
mailItem.BodyFormat = OlBodyFormat.olFormatPlain;
mailItem.Display(mailItem);
}
As you can see it is outputting the email in plaintext at the moment because it was required to be blackberry friendly. You can easily change the format to HTML or richtext if you want some formatting options. For HTML use mailItem.HTMLBody
Hope this helps.
EDIT:
I should note that this is used in a C# Application and that it is referencing Microsoft.Office.Core and using Outlook in the Email class the function is located in.
The simple answer is that what you are trying to achieve isn't realistically achievable across all platforms and mail clients. When asked to do the improbable it is wise to come up with an alternative and suggest that.
Assuming that your fault report is only accessible from an error page then you've already got a barrier to spam - unless the spammers can force an exception.
I've always handled this by logging the fault and text into the database and integrating that with a ticketing system. Maybe also have a mailto: as Bruce suggest with subject=ID&body=text to allow the user to send something by email.
I don't think an .eml format file will help either - because they'll need to forward it, and most users would probably get confused.
A .eml is effectively plain text of the message including headers as per RFC-5322.

Categories