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

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.

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 switch Dialogs in BotFramework SDK3 C#

I'm trying to add a timeout Dialog using proactiveMessages. If user doesn't reply to [A dialog], [timeout dialog] comes out. So I think timeout dialog should be the current dialog. But do I to close other dialog [A dialog]?
According this, it seems context.EndConversation was not working in MS Teams. Of course I have tried again. It is still not working.
I also tried the way below. But it seems not working either.
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, context.Activity.AsMessageActivity()))
{
var botData = scope.Resolve<IBotData>();
await botData.LoadAsync(default(CancellationToken));
var stack = scope.Resolve<IDialogStack>();
stack.Reset();
await botData.FlushAsync(default(CancellationToken));
}
Any suggestions about changing the dialog?
There are two methods of redirecting dialog flow within a C# bot.
you can use context.Forward() to send a user to a new dialog starting with a message that you are currently processing:
await context.Forward(new NewOrderDialog(), this.ResumeAfterNewOrderDialog, message, CancellationToken.None);
or you can use context.call() to send a user to a new dialog and start from scratch there:
context.Call(new AgeDialog(this.name), this.AgeDialogResumeAfter);
The "ResumeAfter" functions can be defined anywhere (including a function within the new dialog itself) and setting these to where you would like to redirect the user after they have finished with your timeout dialog will allow you to determine the flow.

Outlook VSTO form does not display in release version (installed)

Thanks for looking.
I am working on an Outlook plugin that includes a pop-up Form that loads a browser inside of it to allow the user to log in via a 3rd party auth service.
This works great when running from a debug session: I see the custom tab in the ribbon, click the "login" button, and the form pops up as a modal using .ShowDialog().
I am using Outlook 2016.
Problem
When I publish this VSTO and then install it on my machine, the plugin loads and I can see the "login" button in the custom ribbon tab, but clicking it does nothing. I have checked to be sure that the dialog isn't simply popping under the main form. If it's there--I can't find it.
Back to debug session--everything works great. I suspect a permissions issue, but I don't get any prompts or errors from Outlook.
Last, I don't know if it's related, but I sent the VSTO installer to a colleague and they get the following error when attempting to install:
System.Security.SecurityException: Customized functionality in this
application will not work because the certificate used to sign the
deployment manifest for {APP NAME REMOVED} or its location is not
trusted. Contact your administrator for further assistance.
Any help is greatly appreciated.
Most probably your form is shown behind the Outlook window. You need to specify the parent window handle if you want to see the form all the time on top of Outlook windows. The Show and ShowDialog methods of the System.Windows.Forms.Form class allows to specify the parent window handle by passing an instance of the IWin32Window interface as a parameter.
First, you need a class which implements that interface:
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get
{
return _hwnd;
}
}
private IntPtr _hwnd;
}
In Outlook you can cast an instance of the Explorer or Inspector class to the IOleWindow interface and get the window handle which can be used for the IWin32Window implementation.

Outlook.Application.Quit does not work

I'm trying to quit outlook.application after I'm done with the object like the following
//variables intialisation
var outlookApp = new Microsoft.Office.Interop.Outlook.Application();
RDOFolder store;
RDOStore mailbox;
RDOSession session;
session = Redemption.RedemptionLoader.new_RDOSession();
session.MAPIOBJECT = outlookApp.Session.MAPIOBJECT;
mailbox = session.GetDefaultFolder(rdoDefaultFolder.olFolderInbox).Store;
store = session.GetDefaultFolder(rdoDefaultFolder.olFolderInbox).Parent;
//...code goes on
//Quitting
session.LogOff();
outlookApp.Quit();
Marshal.ReleaseComObject(store);
Marshal.ReleaseComObject(mailbox);
Marshal.ReleaseComObject(session);
Marshal.ReleaseComObject(outlookApp);
What could be the problem? I trying to wait for a couple minutes to make sure they were nothing going on in the outlook process bloking it to quit but it never close itself. When I click on the outlook icon to close it manually I get the following error:
Outlook cannot display this view
But If I break before session.LogOff(), open outlook in full mode and restart the code then it will close without problem.
Thanks!
You are killing the Outlook MAPI session by calling RDOSession.Logoff. Don't do that - the session belongs to Outlook, you just borrowed it by reading the Namespace.MAPIOBJECT property.

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