protected void SendMailRun() { // Create an extra reference to insure GC doesn't collect // the reference from the caller SmtpClientNative Email = this; Email.SendMail(); }
/// <summary> /// Sends an Admin email with the Admin email name and email address from Web Store Config /// </summary> /// <param name="Subject"></param> /// <param name="Message"></param> /// <param name="Recipient"></param> /// <returns></returns> public static bool SendAdminEmail(string Subject, string Message, string ccList, string emailAddress = null) { if (string.IsNullOrEmpty(emailAddress)) emailAddress = App.Configuration.AdminEmailAddress; try { SmtpClientNative Email = new SmtpClientNative(); Email.MailServer = App.Configuration.MailServer; Email.Recipient = emailAddress; Email.SenderEmail = App.Configuration.AdminEmailAddress; Email.SenderName = App.Configuration.AdminEmailName; Email.ContentType = "text/plain"; Email.Subject = Subject; Email.Message = Message; // *** Capture errors so we can log them Email.SendError += OnSmtpError; Email.SendMailAsync(); } catch (Exception) { return false; } return true; }
public static void OnSmtpError(SmtpClientNative Smtp) { string ErrorMessage = "Error sending Email: " + Smtp.ErrorMessage + "\r\n" + Smtp.Subject + "\r\nTo: " + Smtp.Recipient + "\r\nServer: " + Smtp.MailServer; // *** Log into SQL Error Log LogManager.Current.WriteEntry( new WebLogEntry { Message = "Email Sending Error", Details = ErrorMessage, ErrorLevel = ErrorLevels.Error } ); }
/// <summary> /// Sends email using the WebStoreConfig Email Configuration defaults. /// /// <seealso>Class WebUtils</seealso> /// </summary> /// <param name="Subject"> /// The subject of the message. /// </param> /// <param name="Message"> /// The body of the message. /// </param> /// <param name="Recipient"> /// The recipient as an email address. /// </param> /// <param name="SendName"> /// Name descriptive of the sender. /// </param> /// <param name="SendEmail"> /// The email address of the sender. /// </param> /// <param name="NoAsync"> /// Whether this message is send asynchronously or not. /// </param> /// <param name="Boolean SendAsText"> /// Determines whether the message is sent as Text or HTML. /// </param> /// <returns>Boolean</returns> public static bool SendEmail(string Subject, string Message, string Recipient, string SendName, string SendEmail, bool NoAsync, bool SendAsText, delSmtpNativeEvent OnErrorHandler) { try { SmtpClientNative Email = new SmtpClientNative(); Email.MailServer = App.Configuration.MailServer; if (!string.IsNullOrEmpty(App.Configuration.MailServerUsername)) { Email.Username = App.Configuration.MailServerUsername; Email.Password = App.Configuration.MailServerPassword; } Email.Recipient = Recipient; if (SendAsText) Email.ContentType = "text/plain"; else { Email.ContentType = "text/html; charset=utf-8"; Email.Encoding = Encoding.UTF8; } if (SendName == null || SendName == "") { Email.SenderEmail = App.Configuration.SenderEmailAddress; Email.SenderName = App.Configuration.SenderEmailName; } else { Email.SenderEmail = SendEmail; Email.SenderName = SendName; } Email.Subject = Subject; Email.Message = Message; // *** Capture any error messages and log them if (OnErrorHandler == null) Email.SendError += OnSmtpError; else Email.SendError += OnErrorHandler; if (NoAsync) return Email.SendMail(); else Email.SendMailAsync(); } catch (Exception) { return false; } return true; }