Beispiel #1
0
        internal static void Send(string subject, bool highPriority,
                                  string body, bool IsBodyHtml, string attachment)
        {
            string _toAddress, _fromAddress;

            System.Net.Mail.MailMessage msg = new MailMessage();
            msg.Subject = subject;
            if (highPriority)
            {
                msg.Priority = MailPriority.High;
            }
            try
            {
                if (!String.IsNullOrEmpty(body))
                {
                    msg.Body = body;
                }
                if (IsBodyHtml)
                {
                    msg.IsBodyHtml = true;
                }
                else
                {
                    msg.IsBodyHtml = false;
                }
                SmtpClient smtp = Smtp.GetSmtpConnection(out _toAddress, out _fromAddress);
                msg.To.Add(_toAddress);
                msg.From = new MailAddress(_fromAddress);
                if (!String.IsNullOrEmpty(attachment))
                {
                    msg.Attachments.Add(new Attachment(attachment));
                }
                smtp.Send(msg);
            }
            catch (Exception e)
            {
                throw new Exception("Cannot send Email" + e.Message);
            }
        }
        protected override ServiceOutcome DoWork()
        {
            List <AccountEntity> _Failed  = new List <AccountEntity>();
            List <AccountEntity> _Success = new List <AccountEntity>();


            using (DataManager.Current.OpenConnection())
            {
                DataManager.Current.AssociateCommands(_LogCmd);

                //Getting failure accounts
                _LogCmd.Parameters["@stat"].Value        = 0;
                _LogCmd.Parameters["@application"].Value = application;
                using (SqlDataReader _reader = _LogCmd.ExecuteReader())
                {
                    try
                    {
                        if (!_reader.IsClosed)
                        {
                            while (_reader.Read())
                            {
                                _Failed.Add(new AccountEntity(_reader));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Write("Failed to retrieve accounts from service log table [ _Failed Accounts ] ", e);
                    }
                }
                //Getting successed accounts
                _LogCmd.Parameters["@stat"].Value        = 1;
                _LogCmd.Parameters["@application"].Value = application;
                using (SqlDataReader _reader = _LogCmd.ExecuteReader())
                {
                    try
                    {
                        if (!_reader.IsClosed)
                        {
                            while (_reader.Read())
                            {
                                _Success.Add(new AccountEntity(_reader));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Write("Failed to retrieve accounts from service log table [ _Success Accounts ] ", e);
                    }
                }
            }
            #region SendingReportByEmail

            //Sending Report by Email.
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("The following accounts reported :");
            sb.AppendLine("<table border=\"1\">");
            //Table headers
            sb.AppendLine("<tr><th>DayCode</th><th>Account ID</th><th>Account_Name</th><th>Service</th><th>Application</th><th>Status</th></tr>");
            string _startTag = "<td>";
            string _endTag   = "</td>";
            if (_Failed.Count > 0)
            {
                foreach (AccountEntity _account in _Failed)
                {
                    sb.AppendLine("<tr style=\"color:red\">" + _startTag + _account.DayCode.ToString() + _endTag + _startTag + _account.Account_id.ToString() + _endTag + _startTag + _account.Account_Name + _endTag + _startTag + _account.CahnnelType + _endTag + _startTag + _account.App + _endTag + _startTag + _account.Status + _endTag + "</tr>");
                }
                foreach (AccountEntity _account in _Success)
                {
                    sb.AppendLine("<tr>" + _startTag + _account.DayCode.ToString() + _endTag + _startTag + _account.Account_id.ToString() + _endTag + _startTag + _account.Account_Name + _endTag + _startTag + _account.CahnnelType + _endTag + _startTag + _account.App + _endTag + _startTag + _account.Status + _endTag + "</tr>");
                }
                sb.AppendLine("</table>");
                try
                {
                    Smtp.Send("Accounts Validation Report [" + application + "]", true, sb.ToString(), true, null);
                }
                catch (Exception e)
                {
                    Log.Write("Error while trying to send account validation report email", e);
                }


                #endregion         //SendingReportByEmail

                #region Update_Report_Status_Table

                SqlCommand _cmd = new SqlCommand();

                using (DataManager.Current.OpenConnection())
                {
                    _cmd = _setCmd;

                    foreach (AccountEntity _account in _Failed)
                    {
                        _cmd = _setCmd;
                        DataManager.Current.AssociateCommands(_cmd);
                        _cmd.Parameters["@account_id"].Value = _account.Account_id;
                        _cmd.Parameters["@day_code"].Value   = _account.DayCode;
                        _cmd.Parameters["@service"].Value    = _account.Channel;
                        _cmd.Parameters["@app"].Value        = _account.App;
                        _cmd.Parameters["@stat"].Value       = 9;
                        _cmd.ExecuteNonQuery();
                    }
                    foreach (AccountEntity _account in _Success)
                    {
                        _cmd = _setCmd;
                        DataManager.Current.AssociateCommands(_cmd);
                        _cmd.Parameters["@account_id"].Value = _account.Account_id;
                        _cmd.Parameters["@day_code"].Value   = _account.DayCode;
                        _cmd.Parameters["@service"].Value    = _account.Channel;
                        _cmd.Parameters["@app"].Value        = _account.App;
                        _cmd.Parameters["@stat"].Value       = 10;
                        _cmd.ExecuteNonQuery();
                    }
                }
                #endregion Update_Report_Status_Table
            }
            return(ServiceOutcome.Success);
        }