Opening encrypted emails with c# - 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 .

Related

Exception while reading properties of SMIME encrypted email

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.

How to open default email client with attachment

I'm working on an issue to my project that when I click the button, a default email client should pop out and if there's an attachment, it should be automatically attach to the default email client like this.
I already tried a lot of methods how to do this. First I used MAPI, but the MAPI cannot detect my Default Email Client even though I already set it in Control Panel, It shows this two message box
I already searched the internet about those error but there's no definite or clear answer to me. HERE'S the code I used in MAPI.
I used also the mail:to protocol to call the default email client who's handling to the aforementioned protocol with using this line of codes.
Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
Dim filename = Convert.toChar(34) & "C:\USERS\JOSHUA~1.HER\DOWNLO~1\ASDPOR~1.PDF" & Convert.toChar(34)
Debug.Writeline(filename)
Dim asd As String = String.Format("mailto:someone#somewhere.com?subject=hello&body=love my body&Attach={0}", filename)
proc.StartInfo.FileName = asd
proc.Start()
But still, no luck. I read a thread that the mail:to don't handle attachment anymore, but this line of code opened my default email client with body and subject, but there's no attachment. In terms of the filename variable, I already tried every path format, I read that I should use 8.3 path format. But still doesn't work.
The last method I used is extending the System.Net.MailMessage.MailMessage() following THIS answer. This works in terms of opening the default email client and attaching an attachment to a mail, but this is not editable and there's no send button on the default email client because this line of code just generating an .eml file and opening it. I'm thinking of parsing the eml file but still I don't know how to open the default email client progmatically in a new message form. Here's the photo
You guys have any idea how to make this possible? Thanks!
I am afraid that this will not be possible to do using some generic method for any mail client. But you can easily create your own solution using System.Net.Mail.SmtpClient and some simple custom UI.

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.

windows live mail mapi support

I am writing a c# .net application that needs to send email messages using MAPI. I am using the following library to achieve this http://www.codeproject.com/KB/IP/SendFileToNET.aspx
Here is an example how I use it.
MAPI mapi = new MAPI();
mapi.AddRecipientTo("test#test.com");
int returncode = mapi.SendMailPopup("subject", "message");
if(returncode==0)
{
MessageBox.Show("User sent message");
}
else if (returncode==1)
{
MessageBox.Show("User abort");
}
This piece of code opens a new message dialog on default mail program and should display if user decided to send or abort sending the email.
This code works fine when using Outlook meaning that it popups a new email dialog and returns the correct error code if user for example decides not to send the message by closing the message dialog.
The problem is that when using windows mail this does not work correctly. The send mail dialog opens correctly and the application pauses its execution on mapi.SendMailPopup until the mail dialog is closed. However no matter what the user does the mapi.SendMailPopup always return zero. If user would for example decide to close the message dialog without sending the message the mapi.SendMailPopup returns 0 when the correct response would be 1(user abort).
My question is should windows live mail have required MAPI-support for this, and if no can someone tell me some other free windows mail client that would offer the required MAPI-support
This is confusing-- that .NET solution is SMAPI, not MAPI. I'm not sure how reliable the return values from SMAPI are, especially since the return value is returned directly from your default MUA vendor's implementation of SMAPI, and there are at least a dozen of them out there. Since the browser doesn't use the return value, and they're the principal clients of SMAPI, I wouldn't rely on it.

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