static void Main(string[] args) { //grab arguments: for (int i = 0; i < args.Length; i++) { switch (args[i]) { case "-config": confDir = args[i + 1]; break; case "-html": htmlDir = args[i + 1]; break; case "-files": //grab all the files until we reach another argument: int searchIndex = i + 1; while (searchIndex < args.Length && !searchArgs(args[searchIndex])) { //Console.WriteLine("program.cs:"+args[searchIndex]); filesDir.Add(args[searchIndex]); searchIndex++; } break; } } if (confDir == "") { confDir = "./default.econf"; } if (htmlDir == "") { htmlDir = "./default.html"; } try { emailConfiguration eConfig = new emailConfiguration(load(confDir)); //Everything gets converted into a readable format for Microsoft's client //Alright, now we need the html file used for the message: StreamReader htmlIn = File.OpenText(htmlDir); message htmlMessage = new message(htmlIn.ReadLine(), htmlIn.ReadToEnd()); //SEND THE THING!!!1! email.sendMail(ref eConfig, ref htmlMessage, filesDir.ToArray()); } catch (FileNotFoundException ex) { Console.WriteLine(ex.ToString()); } catch (IndexOutOfRangeException) { Console.WriteLine("I couldn't find " + confDir + " :/"); } }
public static void sendMail(ref emailConfiguration config, ref message htmlMessage, string[] attachments) { MailMessage newMessage = new MailMessage(); newMessage.From = config.from; foreach (MailAddress element in config.to) { newMessage.To.Add(element); } newMessage.Subject = htmlMessage.subject; AlternateView alternate = AlternateView.CreateAlternateViewFromString(htmlMessage.body, null, MediaTypeNames.Text.Html); newMessage.AlternateViews.Add(alternate); SmtpClient client = new SmtpClient(config.smtpServer); client.Port = config.smtpPort; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(config.sendingEmail, config.sendingPass); client.EnableSsl = true; //adding attachments: if (attachments.Length > 0) { foreach (string attachment in attachments) { //Console.WriteLine(attachment); newMessage.Attachments.Add(new Attachment(attachment, MediaTypeNames.Application.Octet)); } } try { client.Send(newMessage); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
static void Main(string[] args) { //grab arguments: for (int i = 0; i < args.Length; i++) { int searchIndex = 0; switch (args[i]) { case "-config": confDir = args[i + 1]; break; case "-html": htmlDir = args[i + 1]; break; case "-files": //grab all the files until we reach another argument: searchIndex = i + 1; while (searchIndex < args.Length && !searchArgs(args[searchIndex])) { //Console.WriteLine("program.cs:"+args[searchIndex]); filesDir.Add(args[searchIndex]); searchIndex++; } break; case "-emails": //carbon copy of the above, just for email addresses. searchIndex = i + 1; while (searchIndex < args.Length && !searchArgs(args[searchIndex])) { //Console.WriteLine("program.cs:"+args[searchIndex]); extraEmails.Add(args[searchIndex]); searchIndex++; } break; case "-help": help = true; break; } } if (confDir == "") { confDir = "./default.econf"; } if (htmlDir == "") { htmlDir = "./default.html"; } //Let's test all file paths to see what exhists: bool fileError = false; string dirsNotFound = ""; if (!help) { List <string> dirs = new List <string>(new string[] { confDir, htmlDir }); dirs.AddRange(filesDir); foreach (string element in dirs) { if (!File.Exists(element)) { fileError = true; dirsNotFound += element + "\n"; } } } if (help) { Console.WriteLine(usage); } else if (fileError) { Console.WriteLine("Error: Could not find the following items:\n\n" + dirsNotFound + "\nType -help while launching this program for assistance."); } else { try { emailConfiguration eConfig = new emailConfiguration(load(confDir)); //Everything gets converted into a readable format for Microsoft's client //Load extra emails one might want: foreach (string email in extraEmails) { eConfig.to.Add(new System.Net.Mail.MailAddress(email)); } //Alright, now we need the html file used for the message: StreamReader htmlIn = File.OpenText(htmlDir); message htmlMessage = new message(htmlIn.ReadLine(), htmlIn.ReadToEnd()); //SEND THE THING!!!1! email.sendMail(ref eConfig, ref htmlMessage, filesDir.ToArray()); } catch (FileNotFoundException ex) { Console.WriteLine(ex.ToString()); } catch (IndexOutOfRangeException) { Console.WriteLine("There was an error while trying to read the .econf file. You may wish to create a new one with \"cliEmailConfig.exe\" if problems persist..."); } } }