-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
174 lines (149 loc) · 6.53 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
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Text;
namespace LightNetMailer
{
enum LogType
{
SUCCESS,
ERROR,
}
internal class Program
{
static List<cmdarg> AvaiableCmdAargs = new List<cmdarg>()
{
new cmdarg("-f","-f specify from name like \"name <[email protected]>\"",(string from) => mailer.From = from).SetRequired(),
new cmdarg("-t","-t [email protected] / [email protected];[email protected] - to mail address use \";\" for multiple recipents",(string to) => mailer.To = to).SetRequired(),
new cmdarg("-cc","-cc [email protected] / [email protected];[email protected] - carbon copy adress \";\" for multiple recipents",(string cc) => mailer.cc = cc),
new cmdarg("-bcc","-bcc [email protected] - [email protected];[email protected] - carbon copy adress \";\" for multiple recipents ",(string bcc) => mailer.bcc = bcc),
new cmdarg("-s","-s server.com:222 - server and port",(string server) =>mailer.ServerNPort = server).SetRequired(),
new cmdarg("-u","-u Message title",(string title) => mailer.Title = title),
new cmdarg("-a","-a path\\To\\Attachment - ads attachment to mail",(string Attachment) => mailer.attachmentsPaths.Add(Attachment)),
new cmdarg("-o","-o path\\to\\message_file - adds message body from file",(string messageFile) => mailer.PathToMessageFile = messageFile),
new cmdarg("-l","-l path\\to\\logfile - sets path to log file and enables logging",(string logfile) => {LogFilePath = logfile; Log = true; } ),
new cmdarg("-xu","-xu username - username to smtp server login", (string username) => mailer.username = username).SetRequired(),
new cmdarg("-xp","-xp password - password to smtp server login", (string password) => mailer.password = password).SetRequired(),
new cmdarg("-html","-html - use if mail body is html", (string x) => mailer.BodyIsHtml = true),
new cmdarg("-ssl","-ssl - use if your smtp server uses ssl", (string x) => mailer.UseSSL = true),
new cmdarg("-h","-h - displays help message",(string x) => DisplayHelpMessage())
};
static List<cmdarg> ActivatedCmdArgs = new List<cmdarg>();
static Mailer mailer = new Mailer();
static bool Log;
static string LogFilePath;
static void Main(string[] args)
{
if (args.Length > 0)
{
try
{
ParseCommandLineArgs2(args);
CheckForRequiredArgs();
InvokeParamSetters();
mailer.PrepareSmtpClient()
.PrepareFrom()
.PrepareTo()
.PrepareCC()
.PrepareBCC()
.PrepareAttachments()
.PrepareMessage()
.SendMessage();
if (Log)
{
WriteLog($"Sucessfully sent mail - FROM {mailer.From} TO {mailer.To} TITLE {mailer.Title}",LogType.SUCCESS);
}
}
catch(Exception e)
{
if (Log)
{
WriteLog($"FAIL - Failed with:{e.GetType().Name} {e.Message}", LogType.ERROR);
}
WriteErrorMessage($"FAIL - Failed with:{e.GetType().Name} {e.Message}");
}
}
else
{
DisplayHelpMessage();
}
}
static void CheckForRequiredArgs()
{
List<cmdarg> RequiredArgs = AvaiableCmdAargs.Where(x => x.IsRequired == true).ToList();
List<cmdarg> Activated = ActivatedCmdArgs;
Activated.ForEach(active =>
{
IEnumerable<cmdarg> marked = RequiredArgs.Where(x => x.ArgName == active.ArgName);
if (marked.Count() == 1)
{
RequiredArgs.Remove(marked.Single());
}
});
if(RequiredArgs.Count != 0)
{
RequiredArgs.ForEach(r => WriteErrorMessage($"[ERROR] Missing required argument - {r.ArgHelpMessage}"));
Crash("[CRASH] Mail couln't be sent. Missing required arguments above");
}
}
static void ParseCommandLineArgs2(string[] args)
{
cmdarg ToActivate = null;
bool AlreadyAssignedValue = false;
foreach(string arg in args)
{
try
{
cmdarg PickedArg = AvaiableCmdAargs.Where(x => x.ArgName == arg).Single();
ToActivate = new cmdarg(PickedArg.ArgName, PickedArg.ArgHelpMessage, null, PickedArg.ArgAction);
ActivatedCmdArgs.Add(ToActivate);
AlreadyAssignedValue = false;
}
catch (InvalidOperationException)
{
if (AlreadyAssignedValue)
{
throw;// TODO: present error to user
}
else
{
ToActivate.ArgValue = arg;
AlreadyAssignedValue = true;
}
}
}
}
static void InvokeParamSetters()
{
foreach(cmdarg p in ActivatedCmdArgs)
{
p.ArgAction.Invoke(p.ArgValue);
}
}
static void DisplayHelpMessage()
{
foreach(cmdarg c in AvaiableCmdAargs)
{
Console.WriteLine($"{c.ArgName}, {c.ArgHelpMessage}");
}
}
static void WriteErrorMessage(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
}
static void WriteLog(string Message,LogType type)
{
DateTime x = DateTime.Now;
string logMessage = $"[{x.Day}-{x.Month}-{x.Year} {x.Hour}:{x.Minute}:{x.Year}][{type.ToString()}] {Message}\n";
File.AppendAllText(LogFilePath,logMessage);
}
static void Crash(string CrashMessage)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(CrashMessage);
Console.ResetColor();
Environment.Exit(0);
}
}
}