private static bool SendEmail(MessageMeta _meta) { try { if (_meta.To.Replace(" ", "") == string.Empty) { throw new Exception("Missing 'To Email Address'"); } if (_meta.From != string.Empty && _meta.To != string.Empty) { MailMessage message = new MailMessage(_meta.From, _meta.To, _meta.Subject, _meta.EmailText); message.IsBodyHtml = true; if (!String.IsNullOrWhiteSpace(_meta.CC)) { message.CC.Add(_meta.CC); } if (!String.IsNullOrWhiteSpace(_meta.BCC)) { message.Bcc.Add(_meta.BCC); } SmtpClient emailClient; emailClient = new SmtpClient(ConfigurationManager.AppSettings["smtpserver"], 25); emailClient.DeliveryMethod = SmtpDeliveryMethod.Network; emailClient.Send(message); int _delaymiliseconds = 60 / _EmailsPerMinuteLimit * 1000; System.Threading.Thread.Sleep(_delaymiliseconds); /*wait so we never hit the email per minute limit*/ } return(true); } catch (Exception ex) { _ErrorRaised = true; string err = "Error Caught in SendEmailsForBulletinBoard application\n" + "Error in: SendEmail()" + "\nError Message:" + ex.Message.ToString() + "\nStack Trace:" + ex.StackTrace.ToString() + "\nEmailAddressTo:" + _meta.To; EventLog.WriteEntry("NCCOBApp", err, EventLogEntryType.Error); return(false); } }
private static void PrepareSubscriberEmail() { try { string UnsubscribeURL = System.Configuration.ConfigurationManager.AppSettings["UnsubscribeURL"]; DbConnection = new SqlConnection(_dbConnString); DbConnection.Open(); DbCommand = DbConnection.CreateCommand(); DbCommand.CommandText = @"exec mlsadmin.BBRetrieveSubscriberEmailList " + _EmailsToSendForSubscribers.ToString() + ",'" + UnsubscribeURL + "';"; DbReader = DbCommand.ExecuteReader(); while (DbReader.Read()) { MessageMeta _mm = new MessageMeta(); _mm.To = DbReader["EmailAddressTo"] != DBNull.Value ? DbReader["EmailAddressTo"].ToString() : ""; _mm.From = DbReader["EmailAddressFrom"] != DBNull.Value ? DbReader["EmailAddressFrom"].ToString() : ""; //_mm.BCC = System.Configuration.ConfigurationManager.AppSettings["ccemailaddress"]; _mm.EmailText = DbReader["Body"] != DBNull.Value ? HttpUtility.HtmlDecode(DbReader["Body"].ToString()) : ""; _mm.Subject = DbReader["EmailSubject"] != DBNull.Value ? DbReader["EmailSubject"].ToString() : ""; string _sql; bool _sendEmailWorked; _sendEmailWorked = SendEmail(_mm); if (_sendEmailWorked) { _sql = "update BBEmail set SentDate = getdate(), FlagToSend = 0 where FlagToSend = 1 and id = " + DbReader["ID"].ToString(); } else { _sql = "update BBEmail set FlagToSend = 0 where FlagToSend = 1 and id = " + DbReader["ID"].ToString(); } SqlCommand DbCommand2 = null; DbCommand2 = DbConnection.CreateCommand(); DbCommand2.CommandText = _sql; DbCommand2.ExecuteNonQuery(); DbCommand2.Dispose(); } } catch (Exception ex) { _ErrorRaised = true; string err = "Error Caught in SendEmailsForBulletinBoard application\n" + "Error in: PrepareSubscriberEmail()" + "\nError Message:" + ex.Message.ToString() + "\nStack Trace:" + ex.StackTrace.ToString(); EventLog.WriteEntry("NCCOBApp", err, EventLogEntryType.Error); } finally { if (DbReader != null) { DbReader.Close(); } if (DbCommand != null) { DbCommand.Dispose(); } if (DbConnection != null) { DbConnection.Close(); } } }
private static void PrepareCompanyContactEmail() { try { DbConnection = new SqlConnection(_dbConnString); string sql = "exec mlsadmin.BBRetrieveCompanyContactEmailList " + _EmailsToSendForCompanyContacts.ToString() + ";"; DbCommand = DbConnection.CreateCommand(); DbCommand.CommandText = sql; //DbCommand.CommandType = CommandType.StoredProcedure; //DbCommand.Parameters.Add(new SqlParameter("CountToRetrieve", _EmailsToSendForCompanyContacts)); DbConnection.Open(); DbReader = DbCommand.ExecuteReader(); while (DbReader.Read()) { MessageMeta _mm = new MessageMeta(); _mm.To = DbReader["EmailAddressFrom"] != DBNull.Value ? DbReader["EmailAddressFrom"].ToString() : ""; /*Yes, the from is the to since we are using the BCC*/ _mm.From = DbReader["EmailAddressFrom"] != DBNull.Value ? DbReader["EmailAddressFrom"].ToString() : ""; _mm.BCC = DbReader["BCCList"] != DBNull.Value ? DbReader["BCCList"].ToString() : ""; _mm.CC = System.Configuration.ConfigurationManager.AppSettings["ccemailaddress"]; _mm.EmailText = DbReader["Body"] != DBNull.Value ? HttpUtility.HtmlDecode(DbReader["Body"].ToString()) : ""; _mm.Subject = DbReader["EmailSubject"] != DBNull.Value ? DbReader["EmailSubject"].ToString() : ""; bool _sendEmailWorked; _sendEmailWorked = SendEmail(_mm); if (!_sendEmailWorked) { throw new Exception("There was an error sending the Company Contact Emails: " + _mm.BCC); } } if (DbReader != null) { DbReader.Close(); } if (DbCommand != null) { DbCommand.Dispose(); } if (DbConnection != null) { DbConnection.Close(); } DbConnection.Open(); DbCommand.CommandText = @"update BBEmail set SentDate=getdate(), FlagToSend=0 where FlagToSend = 1 and ContactID is not null"; DbCommand.ExecuteNonQuery(); } catch (Exception ex) { _ErrorRaised = true; string err = "Error Caught in SendEmailsForBulletinBoard application\n" + "Error in: PrepareCompanyContactEmail()" + "\nError Message:" + ex.Message.ToString() + "\nStack Trace:" + ex.StackTrace.ToString(); EventLog.WriteEntry("NCCOBApp", err, EventLogEntryType.Error); } finally { if (DbReader != null) { DbReader.Close(); } if (DbCommand != null) { DbCommand.Dispose(); } if (DbConnection != null) { DbConnection.Close(); } } }