当用户在 Outlook 中按下发送按钮时,如何将电子邮件保存到文件夹而不是发送
How to save an email to a folder instead of sending it when the user presses the send button in Outlook
我正在尝试使用 VSTO 编写 Outlook 插件(2007 或更高版本)。当用户按下新电子邮件上的发送按钮时,我不想发送电子邮件,而是想将整封电子邮件保存到一个文件夹(插件首先创建 运行 )。
在未来某个事件发生的不确定时间后,我想以编程方式从我保存的文件夹中发送该电子邮件。
这甚至可行吗?首先,我可以将邮件保存到一个特殊的文件夹而不是发送它吗?我知道我可以取消发送事件,但如何保存整个电子邮件
设置事件以捕获电子邮件的发送事件并取消电子邮件发送,然后执行 MailItem 的必要操作。
下面的示例代码 -
public partial class ThisAddIn
{
private Outlook.Inspectors _inspectors;
public static Outlook.MailItem theCurrentMailItem;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
_inspectors = Application.Inspectors;
_inspectors.NewInspector += _inspectors_NewInspector;
Application.ItemSend += Application_ItemSend;
}
void Application_ItemSend(object Item, ref bool Cancel)
{
object item = (Outlook.MailItem)Item;
if(item != null)
{
//theCurrentMailItem.Move(..SomeFolder) // Move or save, saveas as needed
Cancel = true;
}
}
void _inspectors_NewInspector(Outlook.Inspector inspector)
{
if (inspector == null) throw new ArgumentNullException("inspector");
theCurrentMailItem = null;
object item = inspector.CurrentItem;
if (item == null) return;
if (!(item is Outlook.MailItem)) return;
theCurrentMailItem = inspector.CurrentItem as Outlook.MailItem;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
应用程序 class 的 ItemSend 事件会在用户通过检查器发送 Microsoft Outlook 项目时触发(在检查器关闭之前,但在用户单击发送按钮)或在程序中使用 Outlook 项目(如 MailItem)的发送方法时。传递的布尔参数允许取消进一步处理,因此您将能够做任何您需要的事情。
您还可以考虑重新调整功能区 UI 控件(“发送”按钮)的用途,有关详细信息,请参阅 Temporarily Repurpose Commands on the Office Fluent Ribbon。在这种情况下,您将能够取消默认操作并改为执行您自己的操作。此外,您可以区分是否要发送新的 MailItem。请注意,所有发出的消息(回复、转发等)都会触发 ItemSend 事件。
不清楚您说的是什么文件夹:它是磁盘上的文件夹还是 Outlook 中的文件夹?
要将 Outlook 项目放入文件夹,您可以使用 Move 方法。
要将项目保存到磁盘上的文件夹,您可以使用 SaveAs method which saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used. Later you can use the CreateItemFromTemplate 方法根据保存的项目(.msg 文件)重新构造 MailItem。
我正在尝试使用 VSTO 编写 Outlook 插件(2007 或更高版本)。当用户按下新电子邮件上的发送按钮时,我不想发送电子邮件,而是想将整封电子邮件保存到一个文件夹(插件首先创建 运行 )。
在未来某个事件发生的不确定时间后,我想以编程方式从我保存的文件夹中发送该电子邮件。
这甚至可行吗?首先,我可以将邮件保存到一个特殊的文件夹而不是发送它吗?我知道我可以取消发送事件,但如何保存整个电子邮件
设置事件以捕获电子邮件的发送事件并取消电子邮件发送,然后执行 MailItem 的必要操作。
下面的示例代码 -
public partial class ThisAddIn
{
private Outlook.Inspectors _inspectors;
public static Outlook.MailItem theCurrentMailItem;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
_inspectors = Application.Inspectors;
_inspectors.NewInspector += _inspectors_NewInspector;
Application.ItemSend += Application_ItemSend;
}
void Application_ItemSend(object Item, ref bool Cancel)
{
object item = (Outlook.MailItem)Item;
if(item != null)
{
//theCurrentMailItem.Move(..SomeFolder) // Move or save, saveas as needed
Cancel = true;
}
}
void _inspectors_NewInspector(Outlook.Inspector inspector)
{
if (inspector == null) throw new ArgumentNullException("inspector");
theCurrentMailItem = null;
object item = inspector.CurrentItem;
if (item == null) return;
if (!(item is Outlook.MailItem)) return;
theCurrentMailItem = inspector.CurrentItem as Outlook.MailItem;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
应用程序 class 的 ItemSend 事件会在用户通过检查器发送 Microsoft Outlook 项目时触发(在检查器关闭之前,但在用户单击发送按钮)或在程序中使用 Outlook 项目(如 MailItem)的发送方法时。传递的布尔参数允许取消进一步处理,因此您将能够做任何您需要的事情。
您还可以考虑重新调整功能区 UI 控件(“发送”按钮)的用途,有关详细信息,请参阅 Temporarily Repurpose Commands on the Office Fluent Ribbon。在这种情况下,您将能够取消默认操作并改为执行您自己的操作。此外,您可以区分是否要发送新的 MailItem。请注意,所有发出的消息(回复、转发等)都会触发 ItemSend 事件。
不清楚您说的是什么文件夹:它是磁盘上的文件夹还是 Outlook 中的文件夹?
要将 Outlook 项目放入文件夹,您可以使用 Move 方法。
要将项目保存到磁盘上的文件夹,您可以使用 SaveAs method which saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used. Later you can use the CreateItemFromTemplate 方法根据保存的项目(.msg 文件)重新构造 MailItem。