我的目标是,用户想要发送一封新电子邮件,在填写(.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 等中设置。