C# Attach to open outlook email, edit and send - c#

I am trying get some C# to attach to an open reply-email (triggered manually by user), on the already running instance of Outlook (opened manually by user). The code should identify the open reply email, edit the subject line and body of the email and send the email.
The problem is that I get as far as identifying the running instance of Outlook and assigning it to an object using one of the Marshal methodsoutApp = Marshal.GetActiveObject("Outlook.Application") as Application, but then I cannot cast it to a MailItem type in order to manipulate its elements e.g. the subject line, body, etc...something like MailItem mailItem = (MailItem)outApp.CreateItem((OlItemType.olMailItem)); throws an invalid cast exception at runtime.
Apologies if I am wrong, but could not find a single example close to this exact sequence of events, one of the closer ones is this post c# outlook open existing instance and reply to email
but then it goes a whole different way. There are tons of posts on how to use the Microsoft.Office.Interop.Outlook to OPEN and then use an instance of Outlook, but hardly anything (that I could find) on how to use an open instance. Any help is appreciated, thank you.
EDIT 08102019:
The code is used from an RPA platform, so there is no risk of it being picked up as malware. The "user" is just a virtual user on an account with purpose-made permissions and a controlled environment...sorry, nothing dark here :-). Anyway, here is the code I am using at the moment which creates a new instance and saves it to drafts in Outlook. It is not what I set out to do, as I explained above, this is just a temporary fix:
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.To = "test#test.com";
mailItem.Subject = "Test Email Generation";
mailItem.HTMLBody = "<html><body>This is the body of the email.</strong>.<br/> This is another line in the body of the email.</body></html>";
mailItem.Display(false);
System.Threading.Thread.Sleep(3000);
mailItem.Close(OlInspectorClose.olSave);
Marshal.ReleaseComObject(outlookApp);

To get the opened mail item in the inspector window you need:
Use the ActiveInspector method to get an instance of the Inspector class.
The Inspector.CurrentItem property returns an Object representing the current item being displayed in the inspector.
Set any properties like Subject, Body, Recipients and etc.
To get the inline response in the Explorer window you need to use the Explorer.ActiveInlineResponse property which returns an item object representing the active inline response item in the explorer reading pane.

Related

Redemption GetRDOObjectFromOutlookObject creates conflicting issues on save

I am currently using GetRDOObjectFromOutlookObject to get the RDOAttachment object from outlook attachment. Although I could successfully use the object for the functionality I needed to achieve. Once the mail item is sent , outlook triggers a save and there I have conflicting issue that causes the outlook repair tool error. I realized that this happens when two copies of the same message is opened, Outlook fails to save the mail and throws the error. How do I overcome this issue?
foreach (Outlook.Attachment at in mail.MailItem.Attachments)
{
Redemption.IRDOAttachment rDOAttachment;
rDOAttachment = Globals.ThisAddIn.session.GetRDOObjectFromOutlookObject(at);
rDOAttachment.DisplayName = at.DisplayName;
Is this a bug in redemption or am I missing something here?
I used the below code by getting RDOAttachments from RDOMail rather than GetRDOObjectFromOutlookObject.
RDOMail msg = Globals.ThisAddIn.session.GetMessageFromID(redemptionMailItem.Item.EntryID);
RDOAttachments redemptionAttachments = msg.Attachments;
This solved the issue, But having said that there is a bug GetRDOObjectFromOutlookObject, that makes it unreliable to use it.

Outlook.MailItem custom properties issue

i have the following situation for my Add-In (Office >= 2010):
I want to add some custom properties to an Outlook.MailItem (property must be mail associated) while the mailtext is written.
If this mail is sent i want to grap the send event and get the previously set properties again, doing something and removing the properties and continue sending.
Problem if i use PropertyAccessor:
I use it as follows to save the property while writing the mail:
string propTag = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/test_property"
mailItem.PropertyAccessor.SetProperty(propTag, value);
And to read the property again on sending the mail:
string propTag = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/test_property"
string readProperty = mailItem.PropertyAccessor.GetProperty(propTag);
works if cached mode is enabled on exchange
works NOT if cached mode isn't enabled... i can't find the previously setted properties anymore (Exception with unknown property is thrown)
OutlookSpy (http://www.dimastr.com/outspy/home.htm) can find the property on sending so does anyone knows how to read the properties in a different way?
I would thank you very much for every help.
You need to call Save if you want your changes to be persisted.

Outlook does not recognize one or more names. From 1 computer

I am trying to send an email with several recipients. On most machines, the program works as expected, but on one I get the following error. System.Runtime.InteropServices.COMException (0x80004005): Outlook does not recognize one or more names.
I have narrowed down what address the machine thinks is the issue and there is nothing wrong with it.
I have tried using using both the recipients and To fields as follows:
Outlook.MailItem outLookMsg = outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Recipients recips = outLookMsg.Recipients;
Outlook.Recipient recip = recips.Add("'doesntWork#test.com'");
recip.Type = OlMailRecipientType.olTo;
outLookMsg.Send()
As well as
Outlook.MailItem outLookMsg = outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
outLookMsg.To = "'doesntWork#test.com'";
outLookMsg.Send()
I have tried forcing the program to use the literal email address without resolving by using 'doesntwork#test.com' with and without the single quotes, but that does not work. I really don't want outlook to try and resolve the recipient, I just want it to send the email. Anyone run into this before?

c# VSTO Outlook email body from opened window

I've a problem with my VSTO application for Outlook. I want to process the email body from a selected e-mail.
For selected e-mails out of the "default" list this code works fine:
Object selItem = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
Outlook.MailItem mailItem = (Outlook.MailItem)selItem;
return mailItem.Body;
But if a user opens an email from the list with a double click, the email is displayed in a new window. If the addin is executed in this window (over the ribbon), the email from the list is still used (which is now in the background).
Is there a way to find out if the plugin was started in a separate window and then get the email body from it?
Regards,
Florian
Coincidentally, I just dealt with something similar to this. My situation isn't identical, but since I could easily piece together what it seems like you're looking for see below. I haven't tested this, and obviously you'll have to handle passing the correct reference to your Outlook Application, but since i had this immediately available I figured it would pass it along with the hope that you'll find it helpful.
private static void ribbonButton_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Application application = new Outlook.Application();
Outlook.Inspector inspector = application.ActiveInspector();
if (application.ActiveExplorer().Selection[1] is Outlook.MailItem explorerMailItem)
{
// Write code to handle message if sourced from explorer (i.e., Reading Pane)
}
else if (inspector.CurrentItem is Outlook.MailItem inspectorMailItem)
{
// Write code to hanlde message if sourced from inspector
// (i.e., openened (double-clicked) message
}
}
When you double click on email item you open an inspector window and you can access it by using Application.ActiveInspector() method. The Inspector object has CurrentItem property which represents the opened item.
Also, you should avoid using multiple dots in expressions and properly release COM objects.

Determine if MailItem is open in Compose or Read mode

Is there some way that I can determine if a MailItem is being opened in a ReadMail view (not sure about the terminology here) or in a Compose view. That is, am I opening a mail that has been sent to me, or am I opening a mail to send to someone.
I've checked the MailItem and the Inspector objects but can't find anything of relevance. Unfortunately I don't know how to access specific Properties (I've seen some GetProperty() method somewhere) so I don't know if I could access it via that..
I was hoping that there would be something I could do like:
// where OutlookApp is my Outlok Application
Outlook.Inspector inspector = OutlookApp.ActiveInspector();
Outlook.MailItem item = inspector.CurrentItem as Outlook.MailItem;
if (item.IsOpenInComposeView)
{
// ...
}
_MailItem.Sent will be true when the message is in Read mode, and false when the message is in Compose mode.

Categories

Resources