My goal, the user wants to send a new email, after filling in (.TO
, .CC
, .Body
etc) and clicking on the send button will display a custome Form
where the sending options are. One option is to send the email as the user created it (normal send
button function).
Is there any idea how to assign ItemSend
function to button in custom form ?
ThisAddIn.cs
-> open custom Form
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
-> custom sending button,
public void btn_standard_Click(object sender, System.EventArgs e)
{
//mail.Send() -> send email which user whant to send
}
UPDATE (all aswers to gether, to make this work)
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
can be setup in Properties
-> Behavior
-> DialogResult
-> OK, Cancel, Abort, etc.