-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
196 lines (171 loc) · 7.07 KB
/
Program.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
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using MailMaster.structs;
using MailMaster;
namespace MailMaster
{
internal class Program
{
private static System.Net.Mail.Attachment _data;
private static bool _withAttachment = false;
private static bool _withCC = false;
private static Mail mail;
private static Logging logger;
public static void Main(string[] args)
{
logger = new Logging();
logger.LogInfo("Application started");
GlobalAppInfo appInfo = new GlobalAppInfo();
Console.WriteLine(appInfo.GetAppInfoMessage());
appInfo.ArgStructure =
"<required: host:port> <required: email_address_sender> <required: password> <required: email_address_recipient> <required: email_subject> <required: email_body> <optional: path_attachment> <optional: email_recipient_cc>";
if (args.Length < 5)
{
Console.WriteLine(appInfo.GetArgMissmatchErrorMessage(args.Length, 5));
System.Environment.Exit(2);
}
mail = new Mail();
mail.FromEmail = new MailAddress(args[1]);
string[] recipient = args[3].Split(',');
if (recipient.Length > 1)
{
var counter = 0;
List<Recipient> recipients = new List<Recipient>();
foreach (string address in recipient)
{
if (counter == 0)
{
recipients.Add(new Recipient {EmailAddress = new MailAddress(address)});
mail.RecipientEmail = recipients;
}
else
{
mail.RecipientEmail.Add(new Recipient {EmailAddress = new MailAddress(address)});
}
counter++;
}
}
else
{
List<Recipient> recipients = new List<Recipient>();
recipients.Add(new Recipient {EmailAddress = new MailAddress(recipient[0])});
mail.RecipientEmail = recipients;
}
mail.SubjectEmail = args[4];
mail.BodyEmail = args[5];
if (args.Length >= 6)
{
int indexAttachment = 0;
int indexCC = 0;
try
{
if (args[6].Contains("cc:"))
{
indexCC = 6;
_withCC = true;
}
else if (args[7].Contains("cc:"))
{
indexCC = 7;
_withCC = true;
}
}
catch (IndexOutOfRangeException e)
{
logger.LogSilent("Index out of range Command Line Interface: Command: (CC)");
}
if (indexCC > 0)
{
string[] _recipients = args[indexCC].Split(':');
string[] recipientsCc = _recipients[1].Split(',');
List<Recipient> recipientsCC = new List<Recipient>();
if (recipientsCc.Length > 0)
{
foreach (string recipientcc in recipientsCc)
{
recipientsCC.Add(new Recipient {EmailAddress = new MailAddress(recipientcc)});
}
}
else
{
recipientsCC.Add(new Recipient {EmailAddress = new MailAddress(recipientsCc[0])});
}
mail.RecipientsCC = recipientsCC;
}
try {
if (args[6].Contains("attachment:"))
{
indexAttachment = 6;
_withAttachment = true;
} else if (args[7].Contains("attachment:"))
{
indexAttachment = 7;
}
}
catch (IndexOutOfRangeException e)
{
logger.LogSilent("Index out of range Command Line Interface: Command: (Attachment)");
}
if (indexAttachment > 0)
{
string[] path = args[indexAttachment].Split(':');
_data = new System.Net.Mail.Attachment(path[1], MediaTypeNames.Application.Octet);
_withAttachment = true;
}
}
WaitHandle[] waitHandles = new WaitHandle[mail.RecipientEmail.Count];
int threadCounter = 0;
foreach (Recipient mailaddress in mail.RecipientEmail)
{
var countHandle = threadCounter;
var handle = new EventWaitHandle(false, EventResetMode.ManualReset);
logger.LogInfo("Stating new Thread for Mail sending....");
var thread = new Thread(() =>
{
SendMail(mailaddress, args);
handle.Set();
});
threadCounter++;
waitHandles[countHandle] = handle;
thread.Start();
}
WaitHandle.WaitAll(waitHandles);
}
private static void SendMail(Recipient mailaddress, string[] args)
{
SmtpClient client = new SmtpClient(args[0]);
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(args[1], args[2]);
MailMessage message = new MailMessage(mail.FromEmail, mailaddress.EmailAddress);
message.Subject = mail.SubjectEmail;
message.Body = mail.BodyEmail;
if (_withAttachment)
{
message.Attachments.Add(_data);
logger.LogInfo($"Added {_data.Name} as Attachment.");
}
if (_withCC)
{
foreach (Recipient mailCc in mail.RecipientsCC)
{
message.CC.Add(mailCc.EmailAddress);
}
}
logger.LogInfo(
$"Sending Message:\n From {mail.FromEmail}\n To: {mailaddress.EmailAddress}\n Subject: {mail.SubjectEmail}\n Body: {mail.BodyEmail}");
try
{
client.Send(message);
}
catch (Exception error)
{
logger.LogError($"Fatal Error: Could not send Message!\nCause: {error}");
}
finally
{
logger.LogInfo($"Successfully send Message {mail.SubjectEmail} to {mailaddress.EmailAddress}");
}
}
}
}