问题
So when the message is "popped out" and I use the inspector this works fine. HOwever, with the ActiveInlineResponse the .Send() method is not available. FYI I am a bit of a novice when it comes to this but this is really important for an application we are rolling out. I have the same Add-IN in earlier version of Office that work great. I can't describe how much a little assistance would make my day here. I have spent multiple hours reading forum posts to try and work this out on my own and I am failing.
A few questions/things I have tried:
- I have tried saving the item to drafts and than sending it. I can't seem to figure out how to grab it out of drafts after saving it. I have tried using the EntryId but haven't had much luck.
- I have tried multiple different ways of modifying the Outlook.MailItem to allow the Send() Method. To no avail.
Here is the code:
public void sendSecure_Click(Office.IRibbonControl control)
{
if (control.Id == "sendSecure" || control.Id == "inSendSecure")
{
Outlook.Explorer exp = Globals.SecMailAddIn.Application.ActiveExplorer();
if(exp.ActiveInlineResponse != null)
{
if(exp.ActiveInlineResponse is Outlook.MailItem)
{
Outlook.MailItem response = null;
response = exp.GetType().InvokeMember("ActiveInlineResponse",
System.Reflection.BindingFlags.GetProperty |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public,
null, exp, null) as Outlook.MailItem;
if (response != null)
{
//Add the property to the header, save them item and captuer the EntryID and close the item.
AddUserProperty(response, "XYZ");
((Outlook._MailItem)response).Save();
var mailId = response.EntryID;
MessageBox.Show(mailId);
//Close the item
Marshal.ReleaseComObject(response);
Marshal.ReleaseComObject(exp);
//Send the Item Using the entryid.
}
}
}
else
{
Outlook.Inspector oInspOriginal = control.Context as Outlook.Inspector;
if (oInspOriginal.CurrentItem is Outlook.MailItem)
{
Outlook.MailItem oMail = oInspOriginal.CurrentItem as Outlook.MailItem;
AddUserProperty(oMail, "XYZ");
((Outlook._MailItem)oMail).Send();
}
}
}
else if (control.Id == "sendFullSecure" || control.Id == "inSendFullSecure")
{
Outlook.Inspector oInspOriginal = control.Context as Outlook.Inspector;
if (oInspOriginal.CurrentItem is Outlook.MailItem)
{
Outlook.MailItem oMail = oInspOriginal.CurrentItem as Outlook.MailItem;
AddUserProperty(oMail, "XYZZ");
((Outlook._MailItem)oMail).Send();
}
}
else
{
Outlook.Inspector oInspOriginal = control.Context as Outlook.Inspector;
if (oInspOriginal.CurrentItem is Outlook.MailItem)
{
Outlook.MailItem oMail = oInspOriginal.CurrentItem as Outlook.MailItem;
AddUserProperty(oMail, "XYZZ");
((Outlook._MailItem)oMail).Send();
}
}
}
void Explorer_InlineResponseClose()
{
}
private void AddUserProperty(Outlook.MailItem mail, string folderEmailId)
{
Outlook.PropertyAccessor mailPropertyAccessor = null;
try
{
if (string.IsNullOrEmpty(folderEmailId))
return;
mailPropertyAccessor = mail.PropertyAccessor;
mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/X-WorksiteFolderEmailId", folderEmailId);
mail.Save();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
EDIT 1:
else if (control.Id == "sendFullSecure" || control.Id == "inSendFullSecure")
{
Outlook.Explorer exp = Globals.SecMailAddIn.Application.ActiveExplorer();
if (exp.ActiveInlineResponse != null)
{
if (exp.ActiveInlineResponse is Outlook.MailItem)
{
Outlook.MailItem response = null;
response = exp.GetType().InvokeMember("ActiveInlineResponse",
System.Reflection.BindingFlags.GetProperty |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.Public,
null, exp, null) as Outlook.MailItem;
if (response != null)
{
try
{
//Add the property to the header.
AddUserProperty(response, "EBSENDSECURE");
//Get the Current Window Object
IOleWindow winh = exp as IOleWindow;
winh.GetWindow(out IntPtr win);
//Get the Handle from the window object.
AutomationElement newElement = AutomationElement.FromHandle(win);
//Find the Standard Send Button Property and invoke it to send the email.
PropertyCondition sendButtonCondition = new PropertyCondition(AutomationElement.NameProperty, "Send");
AutomationElement sendButton = newElement.FindFirst(TreeScope.Descendants, sendButtonCondition);
if (sendButton != null)
{
MessageBox.Show("The send button was found.");
var invokePattern = sendButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern.Invoke();
}
//Close the item
Marshal.ReleaseComObject(response);
Marshal.ReleaseComObject(exp);
}
catch (Exception)
{
MessageBox.Show("An error has occurred while sending this email. Please close and reopen outlook to verify that it is sent correctly.");
throw;
}
}
}
}
回答1:
Yes, OOM blocks some methods for the inline response. The best you can do is simulate a click on the Send button using the accessibility API.
If using Redemption is an option, it exposes SafeExplorer.ActiveInlineResponseSend
method:
SafeExplorer sExplorer = new Redemption.SafeExplorer();
sExplorer.Item = Application.ActiveExplorer;
sExplorer.ActiveInlineResponseSend();
来源:https://stackoverflow.com/questions/61142613/using-activeinlineresponse-in-vsto-add-in-send-mailitem-after-modifying-header