-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThisAddIn.cs
140 lines (119 loc) · 4.97 KB
/
ThisAddIn.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
namespace WarnOnCrossSendOutlookAddIn
{
public partial class ThisAddIn
{
private string[] accountDomains;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
identifyMultipleEmailDomains();
this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
if (isCrossSendEmail(Item))
{
if (MessageBox.Show("Your attemping to mail from one Domain to users in another.\n" +
"Do you want to send any way?",
"Cross Send Warning!", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
{
Cancel = true;
}
}
}
private bool isCrossSendEmail(object Item)
{
if (accountDomains != null)
{
var mailItem = Item as Outlook.MailItem;
if (mailItem != null &&
mailItem.SendUsingAccount != null &&
mailItem.SendUsingAccount.SmtpAddress != null)
{
//Identify the account that user is sending from
string senderDomain = getDomainStringFromAddress(mailItem.SendUsingAccount.SmtpAddress);
var otherDomains = (from i in accountDomains
where !i.Equals(senderDomain)
select i).ToList();
if (otherDomains.Count > 0)
foreach(Outlook.Recipient recipient in mailItem.Recipients)
{
string recipientDomain = getDomainStringFromAddress(recipient.Address);
if (string.IsNullOrWhiteSpace(recipientDomain))
{
var exchangeUser = recipient.AddressEntry.GetExchangeUser();
if (exchangeUser != null)
{
recipientDomain = getDomainStringFromAddress(
exchangeUser.PrimarySmtpAddress);
}
else
{
var exchangeDL = recipient.AddressEntry.GetExchangeDistributionList();
if (exchangeDL != null)
{
recipientDomain = getDomainStringFromAddress(
exchangeDL.PrimarySmtpAddress);
}
}
}
if (!string.IsNullOrEmpty(recipientDomain))
{
if (otherDomains.Exists(j => j == recipientDomain))
{
return true;
}
}
} //end foreach recipient
}
}
return false;
}
private void identifyMultipleEmailDomains()
{
List<string> accountDomainList = new List<string>();
foreach (Outlook.Account account in this.Application.Session.Accounts)
{
if (account != null && !string.IsNullOrWhiteSpace(account.SmtpAddress))
{
string accountDomain = getDomainStringFromAddress(account.SmtpAddress);
if (!string.IsNullOrEmpty(accountDomain))
accountDomainList.Add(accountDomain);
}
}
if (accountDomainList.Count > 1)
{
accountDomains = accountDomainList.ToArray();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private static string getDomainStringFromAddress(string address)
{
if (string.IsNullOrWhiteSpace(address) ||
!address.Contains("@"))
return string.Empty;
return address.Substring(address.IndexOf('@') + 1);
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}