/*   private void UploadToWeb(byte[] data, string filename,string description, List<EmailList> sendToList)
         * {
         *     if (data != null)
         *     {
         *         var clientPortal = new AstrodonClientPortal(SqlDataHandler.GetClientPortalConnectionString());
         *         foreach (string customerAccount in sendToList.Select(a => a.AccNumber).Distinct())
         *         {
         *             try
         *             {
         *                 clientPortal.UploadUnitDocument(DocumentCategoryType.Letter,DateTime.Today
         *                     , building.ID, customerAccount,  filename, description, data);
         *             }catch(Exception ex)
         *             {
         *                 Controller.HandleError(ex);
         *             }
         *         }
         *     }
         * }
         */

        private void SendMail(int msgID)
        {
            var clientPortal = new AstrodonClientPortal(SqlDataHandler.GetClientPortalConnectionString());

            String mailQuery = "SELECT msg.id, msg.fromAddress, b.Code, b.DataPath, msg.incBCC, msg.bccAddy, msg.subject, msg.message, msg.billBuilding, msg.billAmount, msg.buildingID FROM tblMsg AS msg ";

            mailQuery += " INNER JOIN tblBuildings AS b ON msg.buildingID = b.id WHERE (msg.queue = 'False') AND msg.id = " + msgID.ToString();
            SqlDataHandler dh     = new SqlDataHandler();
            String         status = String.Empty;
            DataSet        ds     = dh.GetData(mailQuery, null, out status);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int buildingId = (int)dr["buildingID"];

                    String  bCode           = dr["Code"].ToString();
                    String  dataPath        = dr["DataPath"].ToString();
                    String  fromAddress     = dr["fromAddress"].ToString();
                    bool    incBCC          = bool.Parse(dr["incBCC"].ToString());
                    String  bccAddy         = dr["bccAddy"].ToString();
                    String  subject         = dr["subject"].ToString();
                    String  message         = dr["message"].ToString();
                    bool    billBuilding    = bool.Parse(dr["billBuilding"].ToString());
                    double  billAmount      = double.Parse(dr["billAmount"].ToString());
                    String  attachmentQuery = "SELECT Name, Data FROM tblMsgData WHERE msgID = " + msgID.ToString();
                    DataSet dsAttachment    = dh.GetData(attachmentQuery, null, out status);
                    Dictionary <String, byte[]> attachments = new Dictionary <string, byte[]>();
                    if (dsAttachment != null && dsAttachment.Tables.Count > 0 && dsAttachment.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow drA in dsAttachment.Tables[0].Rows)
                        {
                            if (!attachments.ContainsKey(drA["Name"].ToString()))
                            {
                                attachments.Add(drA["Name"].ToString(), (byte[])drA["Data"]);
                            }
                        }
                    }
                    String billableCustomersQuery = "SELECT distinct accNo FROM tblMsgRecipients WHERE billCustomer = 'True' and msgID = " + msgID.ToString();

                    DataSet billableCustomers = dh.GetData(billableCustomersQuery, null, out status);

                    String  allRecipientsQuery = "SELECT id, accNo, recipient FROM tblMsgRecipients WHERE msgID = " + msgID.ToString();
                    DataSet receivers          = dh.GetData(allRecipientsQuery, null, out status);

                    Dictionary <String, bool>   emails = new Dictionary <string, bool>();
                    Dictionary <string, string> bulkMailAttachments = null;


                    if (receivers != null && receivers.Tables.Count > 0 && receivers.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow rrece in receivers.Tables[0].Rows)
                        {
                            bulkMailAttachments = new Dictionary <string, string>();

                            String[] emailAddys   = rrece["recipient"].ToString().Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                            string   accNumber    = rrece["accNo"].ToString();
                            string   emailAddress = string.Empty;
                            if (emailAddys.Length > 0)
                            {
                                emailAddress = emailAddys[0];
                            }
                            foreach (string file in attachments.Keys.ToList())
                            {
                                var data = attachments[file];
                                if (data != null && data.Length > 0)
                                {
                                    string url = clientPortal.UploadUnitDocument(DocumentCategoryType.Letter, DateTime.Today, buildingId, accNumber, file, "Correspondence", data, emailAddress);
                                    bulkMailAttachments.Add(file, url);
                                }
                            }

                            bool success = Email.EmailProvider.SendBulkMail(fromAddress, emailAddys, subject, message, bulkMailAttachments);

                            String updateQuery = "UPDATE tblMsgRecipients SET sentDate = getdate() WHERE id = " + rrece["id"].ToString();
                            dh.SetData(updateQuery, null, out status);
                            if (!emails.ContainsKey(rrece["accNo"].ToString()))
                            {
                                emails.Add(rrece["accNo"].ToString(), success);
                            }
                        }
                    }
                    message += Environment.NewLine + Environment.NewLine;
                    message += "Send status:" + Environment.NewLine + Environment.NewLine;
                    var builder = new System.Text.StringBuilder();
                    builder.Append(message);
                    foreach (KeyValuePair <String, bool> statuses in emails)
                    {
                        builder.Append(statuses.Key + " = " + statuses.Value.ToString() + Environment.NewLine);
                    }
                    message = builder.ToString();
                    if (incBCC)
                    {
                        String[] bccs = bccAddy.Split(new String[] { ";" }, StringSplitOptions.None);

                        Email.EmailProvider.SendBulkMail(fromAddress, bccs, subject, message, bulkMailAttachments);
                    }
                }
            }
        }
Exemplo n.º 2
0
 private void UpdateBuildings()
 {
     new ClientPortal.AstrodonClientPortal(SqlDataHandler.GetClientPortalConnectionString()).SyncBuildings();
 }