-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutlook2RemedyRibbon.cs
267 lines (218 loc) · 8.88 KB
/
Outlook2RemedyRibbon.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
namespace OutlookTools
{
[ComVisible(true)]
public class Outlook2RemedyRibbon : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
public Outlook2RemedyRibbon()
{
//nothing to do here
}
#region IRibbonExtensibility Members
public string GetCustomUI(string ribbonID)
{
return GetResourceText("OutlookTools.Outlook2RemedyRibbon.xml");
}
#endregion
#region Ribbon Callbacks
//Create callback methods here. For more information about adding callback methods, visit http://go.microsoft.com/fwlink/?LinkID=271226
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
this.ribbon = ribbonUI;
}
public System.Drawing.Bitmap GetImage(Office.IRibbonControl control)
{
switch (control.Id)
{
case "convertButton":
{
//add a icon for the button here
return new System.Drawing.Bitmap(AppDomain.CurrentDomain.BaseDirectory + @"\res\convert.png");
};
case "configButton":
{
//add a icon for the button here
return new System.Drawing.Bitmap(AppDomain.CurrentDomain.BaseDirectory + @"\res\settings.png");
};
}
return null;
}
#endregion
#region "Button: Convert Settings"
public void OnConfigButton(Office.IRibbonControl control)
{
Outlook2RemedyConfig config = new Outlook2RemedyConfig();
config.ShowDialog();
}
#endregion
#region "Button: Convert Email2Ticket"
public void OnConvertButton(Office.IRibbonControl control)
{
StagingTicket ticket = new StagingTicket();
Outlook.Application application = new Outlook.Application();
Outlook.NameSpace ns = application.GetNamespace("MAPI");
//get selected outlook object / mail item
Object selectedObject = application.ActiveExplorer().Selection[1];
Outlook.MailItem selectedMail = (Outlook.MailItem)selectedObject;
//in case a email object si selected run the workflow
if (selectedObject != null)
{
//1. invoke web service
try
{
//instanciate service and invoke it create the ticket and to receive the ticket reference (ID)
Remedy2OutlookService service = new Remedy2OutlookService();
ticket = service.invoke(selectedMail);
}
catch(Exception ex)
{
string path = AppDomain.CurrentDomain.BaseDirectory + @"\temp";
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + @"\errors.log", true))
{
file.WriteLine(DateTime.Now.ToString() + " - " + ex.Message + "\n" + ex.ToString());
}
// display an error message
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//2. open submitted ticket in browser or display info/warn/error messages
if (Properties.Settings.Default.OpenInBrowser && ticket.wid != null)
{
dynamic ie = Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.Application"));
string url = Properties.Settings.Default.AppConsoleURL;
if(url.EndsWith("/")) url.Remove(url.Length - 1);
if (url.IndexOf("?", 0) > 0) url += "&eid=" + ticket.wid;
else url += "?eid=" + ticket.wid;
ie.AddressBar = false;
ie.MenuBar = false;
ie.ToolBar = false;
ie.Visible = true;
ie.Navigate2(url);
}
else
{
if (ticket.wid != null && String.Equals(ticket.sts, "done")) MessageBox.Show("Remedy workflow ticket has been created: " + ticket.wid, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
else if (ticket.rid != null && String.Equals(ticket.sts, "open")) MessageBox.Show("Selected email is transferred to Remedy into the staging record [" + ticket.rid + "] but the fulfillment ticket was not created due to an error or misconfiguration.\n\nContact your Administrator!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
else if (String.Equals(ticket.sts, "error") && ticket.log != null) MessageBox.Show("Remedy workflow error: " + ticket.log + ".\n\nContact your Administrator!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//3. mark selected email as read
if (Properties.Settings.Default.ReadMail && ticket.wid != null)
{
selectedMail.UnRead = false;
selectedMail.Save();
}
//4. insert reference ticket number in the
if (Properties.Settings.Default.InsertInSubject && ticket.wid != null)
{
selectedMail.Subject = ticket.wid + ": " + selectedMail.Subject;
selectedMail.Save();
}
//5. copy selected email item into Backup MAPI folder (if not exist will be created)
if (Properties.Settings.Default.BackupMail && ticket.wid != null)
{
Outlook.Folder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
Outlook.MAPIFolder backup = null;
foreach (Outlook.MAPIFolder subfolder in inbox.Folders)
{
if (String.Equals(subfolder.Name, "Backup"))
{
backup = subfolder;
break;
}
}
if(backup == null)
{
try
{
backup = inbox.Folders.Add("Backup", Outlook.OlDefaultFolders.olFolderInbox);
}
catch (Exception ex)
{
MessageBox.Show("Error trying to create Backup MAPI folder: " + ex.Message + ".\n\nContact your Administrator or create it manually!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if(backup != null)
{
Outlook.MailItem copyMail = selectedMail.Copy();
copyMail.Move(backup);
}
if (inbox != null) Marshal.ReleaseComObject(inbox);
if (backup != null) Marshal.ReleaseComObject(backup);
}
//6. delete selected email item
if (Properties.Settings.Default.RemoveMail && ticket.wid != null)
{
selectedMail.Delete();
}
}
}
#endregion
#region "Get SMTP account methods "
public Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
{
// Loop over the Accounts collection of the current Outlook session.
Outlook.Accounts accounts = application.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
// When the email address matches, return the account.
if (account.SmtpAddress.ToLower() == smtpAddress.ToLower())
{
return account;
}
}
// If you get here, no matching account was found.
throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
}
public Outlook.Account GetDefaultAccount(Outlook.Application application)
{
// Get the Store for CurrentFolder.
Outlook.Folder folder = application.ActiveExplorer().CurrentFolder as Outlook.Folder;
Outlook.Store store = folder.Store;
Outlook.Accounts accounts = application.Session.Accounts;
// Enumerate accounts to find
// account.DeliveryStore for store.
foreach (Outlook.Account account in accounts)
{
if (account.DeliveryStore.StoreID ==
store.StoreID)
{
return account;
}
}
// If you get here, no matching account was found.
throw new System.Exception(string.Format("No Account found!"));
}
#endregion
#region Helpers
private static string GetResourceText(string resourceName)
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] resourceNames = asm.GetManifestResourceNames();
for (int i = 0; i < resourceNames.Length; ++i)
{
if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
{
using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
{
if (resourceReader != null)
{
return resourceReader.ReadToEnd();
}
}
}
}
return null;
}
#endregion
}
}