using System; using System.IO; using System.Web.Mail; using System.Windows.Forms; namespace SendMail { class Class1 { [STAThread] static void Main(string[] args) { if(args.Length < 4) { Console.WriteLine("SendMail.exe. Open a text file and send it in an email."); Console.WriteLine("Syntax: SendMail ToEmail FromEmail Subject TextFilePath"); return; } else { MailMessage msg = new MailMessage(); SmtpMail.SmtpServer = "localhost"; msg.To = args[0]; msg.From = args[1]; msg.Subject = args[2]; msg.Body = ""; //"Auto email from " + SystemInformation.UserDomainName; try { using (StreamReader sr = new StreamReader(args[3])) msg.Body += sr.ReadToEnd(); } catch(Exception ex) { Console.WriteLine("Error opening file: " + ex.Message); return; } try { SmtpMail.Send(msg); Console.WriteLine("Message sent successfully"); } catch(System.Web.HttpException ex) { // get the inner inner exception from cdo.mail Console.WriteLine("Error: " + ex.InnerException.InnerException.Message); } catch(Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } } }