-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailHelper.cs
100 lines (78 loc) · 3.51 KB
/
MailHelper.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
using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookAddIn_Report_Spam
{
public class MailHelper
{
static public Outlook.Explorer currentExplorer = null;
static private Outlook.Application currentApplication = Globals.ThisAddIn.Application;
static public Dictionary<string, object> customizedRegistryValue = Globals.ThisAddIn.customizedRegistryValue;
// Parent Function that invokes the helper / handlers
public static void CreateMail_SelectedMails_Attached()
{
IEnumerable<MailItem> mailitems = MailHelper.GetSelectedEmails_Handler();
if (MailItemsStatusCheck_Helper(mailitems) == false) return;
Outlook.MailItem newMail = currentApplication.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
Outlook.MailItem newMailWithAttachments = AddMailItemsAsAttachment_Handler(mailitems, newMail);
// Test to display the email window, restricted to Development purpose
// newMail.Display(false);
Boolean sendMailConfirmation = SendMailConfirmation_Helper();
if (sendMailConfirmation) newMail.Send();
}
/**
* Handler for composing a new mail with mailitems as attachments
*
* \mailitems IEnumerable<MailItem> instance contains iterative mail items.
* \newMail Empty new mail item to be attached with mailitems.
*/
public static MailItem AddMailItemsAsAttachment_Handler(IEnumerable<MailItem> mailitems, MailItem newMail)
{
foreach (MailItem mailitem in mailitems)
{
newMail.Attachments.Add(mailitem, Outlook.OlAttachmentType.olEmbeddeditem, 1, mailitem.Subject);
newMail.To = (string)customizedRegistryValue["SOCEmail"];
newMail.Subject = (string)customizedRegistryValue["EmailSubject"];
newMail.HTMLBody = (string)customizedRegistryValue["EmailBody"];
}
return newMail;
}
// Simple handler to retrieve mail selected by user
public static IEnumerable<MailItem> GetSelectedEmails_Handler()
{
foreach (Object mail_obj in currentApplication.ActiveExplorer().Selection)
{
if (mail_obj is MailItem)
{
yield return (MailItem)mail_obj;
}
}
}
/**
* Helper function for asserting if the MailItems has passed a conditional check
*
* \return a simple Boolean which true indicates pass and false equals block
*/
static public Boolean MailItemsStatusCheck_Helper(IEnumerable<MailItem> mailitems)
{
// Selected Mail is empty
if (mailitems.Count() == 0) return false;
return true;
}
// Confirmation dialog to determine whether the user agrees to send
static public Boolean SendMailConfirmation_Helper()
{
DialogResult dialogResult = MessageBox.Show("Confirm Report Spam Email?", "Report Spam", MessageBoxButtons.OKCancel);
if (dialogResult == DialogResult.OK) return true;
return false;
}
}
}