using System; using System.Net.Mail; using System.Text.RegularExpressions; using EmailDispatcher; namespace EmailDispatcher { class Program { private static SmtpClient smtp = new SmtpClient(Constants.gmailConn, Constants.gmailPort); static void Main(string[] args) { try { #region Sender Name bool userNameBool = true; while(userNameBool) { Console.Write("Enter your name: "); User.displayNameStr = Console.ReadLine(); if(Constants.regUserName.IsMatch(User.displayNameStr)) userNameBool = false; else { Console.WriteLine("Name entered was not in proper format. Please retry"); userNameBool = true; } } #endregion #region From User bool fromStrBool = true; while (fromStrBool) { Console.Write("Enter your email address: "); User.fromStr = Console.ReadLine(); if (Constants.regEmail.IsMatch(User.fromStr)) fromStrBool = false; else { Console.WriteLine("Email address entered was not in proper format. Please retry"); fromStrBool = true; } } #endregion #region User Password Console.Write("Enter your password: "); ConsoleKeyInfo info = Console.ReadKey(true); while(info.Key != ConsoleKey.Enter) { if (info.Key != ConsoleKey.Backspace) { User.passwordStr += info.KeyChar; info = Console.ReadKey(true); } else { User.passwordStr.Remove(User.passwordStr.Length - 1); info = Console.ReadKey(true); } } for (int i = 0; i < User.passwordStr.Length; i++) Console.Write("*"); #endregion Console.WriteLine(); #region To User bool toStrBool = true; while (toStrBool) { Console.Write("Enter target email address: "); User.toStr = Console.ReadLine(); if (Constants.regEmail.IsMatch(User.toStr)) toStrBool = false; else { Console.WriteLine("Email address entered was not in proper format. Please retry"); toStrBool = false; } } #endregion #region Message Subject bool messageSubjectBool = true; while (messageSubjectBool) { Console.Write("Enter message subject: "); User.subjectStr = Console.ReadLine(); if (Constants.regString.IsMatch(User.subjectStr)) messageSubjectBool = false; else { Console.WriteLine("Subject entered was not in proper format. Please retry"); messageSubjectBool = true; } } #endregion #region Message Body bool messageBodyBool = true; while (messageBodyBool) { Console.Write("Enter message body: "); User.bodyStr = Console.ReadLine(); if (Constants.regString.IsMatch(User.bodyStr)) messageBodyBool = false; else { Console.WriteLine("Body entered was not in proper format. Please retry"); messageBodyBool = true; } } #endregion var msg = new MailMessage(); msg.From = new MailAddress(User.fromStr, User.displayNameStr); msg.To.Add(new MailAddress(User.toStr)); msg.Subject=User.subjectStr; msg.Body=User.bodyStr; smtp.EnableSsl = true; smtp.Credentials = new System.Net.NetworkCredential(User.fromStr, User.passwordStr); msg.IsBodyHtml = false; msg.ReplyToList.Add(new MailAddress(User.fromStr)); smtp.Send(msg); Console.WriteLine("Email despatched!"); } catch (Exception e) { Console.WriteLine(String.Format("{0}\n{1}\n{2}\n{3}\n{4}\n", e, e.Source, e.Message, e.StackTrace, e.InnerException)); } finally { } } } }