/// <summary> /// Send an email using the configuration values. /// </summary> /// <param name="recipient">A full email address OR a valid ActiveDirectory account.</param> /// <param name="subject">The subject, might be prefixed with a configurable string.</param> /// <param name="body">The email body.</param> private void SendEmail(string recipient, string subject, string body) { subject = $"{_config.EmailPrefix}{ServiceName} - {subject} - {_config.HostAlias}"; body += $"\n\n--\n[{_versionSummary}]\n"; if (string.IsNullOrEmpty(_config.SmtpHost)) { Log.Debug("SendEmail: {0}\n{1}", subject, body); return; } if (!recipient.Contains(@"@")) { Log.Trace("Invalid recipient, trying to resolve via AD: {0}", recipient); recipient = ActiveDirectory.GetEmailAddress(recipient); } if (string.IsNullOrWhiteSpace(recipient)) { Log.Info("Invalid or empty recipient given, NOT sending email!"); Log.Debug("SendEmail: {0}\n{1}", subject, body); return; } try { var smtpClient = new SmtpClient() { Port = _config.SmtpPort, Host = _config.SmtpHost, EnableSsl = true, Timeout = 10000, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new System.Net.NetworkCredential(_config.SmtpUserCredential, _config.SmtpPasswortCredential) }; var mail = new MailMessage(_config.EmailFrom, recipient, subject, body) { BodyEncoding = Encoding.UTF8 }; smtpClient.Send(mail); } catch (Exception ex) { Log.Error("Error in SendEmail(): {0}\nInnerException: {1}\nStackTrace: {2}", ex.Message, ex.InnerException, ex.StackTrace); } }