【发布时间】:2020-12-09 19:53:25
【问题描述】:
我的目标是,用户想要发送一封新邮件,在填写(.TO、.CC、.Body 等)并点击发送按钮后会显示一个客户Form,其中发送选项是.一种选择是在用户创建电子邮件时发送电子邮件(正常的send 按钮功能)。
是否知道如何将ItemSend 函数分配给自定义表单中的按钮?
ThisAddIn.cs -> 打开自定义表单
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
Application_ItemSend
private void Application_ItemSend(object Item, ref bool Cancel)
{
if (Item is Outlook.MailItem)
{
Form1 f = new Form1();
f.Show();
}
Cancel = true;
}
ChooseFormSend.cs -> 自定义发送按钮,
public void btn_standard_Click(object sender, System.EventArgs e)
{
//mail.Send() -> send email which user whant to send
}
更新(所有要求一起,使这项工作)
ThisAddIn.cs
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
private bool ProcessEmail(Outlook.MailItem mailItem, MailSendType SendType)
{
switch (SendType)
{
case MailSendType.Normal:
return false;
case MailSendType.WithAdverts:
//mailItem.BCC += "ad@server.xyz";
mailItem.HTMLBody += @"<b>Some bold text at the end :)</b>";
mailItem.HTMLBody += @"1233";
return false; // send the mail
case MailSendType.WithCoupon:
mailItem.CC += "coupon@server.xyz";
mailItem.HTMLBody += @"";
return false; // send the mail
// by default don't send the mail
default:
return true;
}
}
private void Application_ItemSend(object Item, ref bool Cancel)
{
if (Item is MailItem) // ensures Item is a mail item
{
using (Form1 form_ChooseForm = new Form1())
{
DialogResult dr = form_ChooseForm.ShowDialog();
if (dr == DialogResult.OK) // shows the form as a dialog
{
Cancel = ProcessEmail((MailItem)Item, form_ChooseForm.SendType);
// MessageBox.Show("The OK button on the form was clicked.");
}
else
{
// MessageBox.Show("Cancel process");
Cancel = true;
}
}
}
}
}
ChooseFormSend.cs
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Tools.Outlook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
namespace OutlookControll
{
public enum MailSendType
{
NoSend,
Normal,
WithAdverts,
WithCoupon
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public MailSendType SendType = MailSendType.NoSend;
private void Btn_ShowSecondForm_Click(object sender, EventArgs e)
{
AddItemsForm f2 = new AddItemsForm();
f2.ShowDialog();
this.Hide();
}
private void Btn_cancel_Click(object sender, EventArgs e)
{
Button Btn_cancel_Click = new Button();
Btn_cancel_Click.DialogResult = DialogResult.Cancel;
this.Hide();
}
public void Btn_standard_Click(object sender, EventArgs e)
{
Button Btn_standard_Click = new Button();
Btn_standard_Click.DialogResult = DialogResult.OK;
Controls.Add(Btn_standard_Click);
SendType = MailSendType.Normal;
this.Hide();
}
}
Btn_standard_Click.DialogResult = DialogResult.OK 可以设置在Properties -> Behavior -> DialogResult -> OK、Cancel、Abort 等
【问题讨论】:
-
为什么不能使用 MailItem.Send?由于 Application_ItemSend 将再次触发,您可以设置自定义属性以跳过该电子邮件。或者您可以模态显示您的格式并将所有处理限制在 Application_ItemSend 处理程序中。
-
@dmitry streblechenko 所以添加到按钮 application_itemSend ?
-
我不确定你的意思 - 按钮存在于表单中,而不是事件处理程序中。
-
好的,那么表单按钮的部分代码 application_itemsend 应该放在哪里?
-
在您的表单中。单击您的按钮将关闭您的表单并让 Application_ItemSend 继续执行。如果要取消提交,请将cancel参数设置为true。
标签: c# vsto outlook-addin outlook-2019