I automate lots of jobs to run daily. Mostly just getting Task Scheduler to run a bat file or python script and I can just check from my desktop if something failed and re-run it. However, if I am in .NET and something is going to run from another server that I won’t necessarily be able to access whenever I want, receiving an email that a job succeeded or failed would be nice. I am hoping for a more permanent solution, but in pinch, a gmail account will work.
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 |
using System; using System.Net; using System.Net.Mail; namespace SmtpWithGmail { class Program { static void Main(string[] args) { // This may be deprecated but it works for now // docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient MailMessage msg = new MailMessage(); msg.Body = "Sleep tight. All went well."; msg.Subject = "Scheduled job succeeded"; msg.From = new MailAddress("[mySender]@gmail.com"); msg.To.Add(new MailAddress("[myReceiver]@gmail.com")); SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.Credentials = new NetworkCredential("[mySender]@gmail.com" , "[myPass]"); smtp.EnableSsl = true; smtp.Send(msg); } } } |