Exemplo n.º 1
0
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(txtName.Text.Trim()))
         {
             "请输入名称!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtEmailAddress.Text.Trim()) || !MailHelper.IsEmail(txtEmailAddress.Text.Trim()))
         {
             "请输入郵箱地址(郵箱地址要正確)!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtPassword.Text.Trim()))
         {
             "请输入密碼!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtSMTP.Text.Trim()))
         {
             "请输入SMTP!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtPOP3.Text.Trim()))
         {
             "请输入POP3!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtSMTPPort.Text.Trim()))
         {
             "请输入SMTP端口!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtPOPPort.Text.Trim()))
         {
             "请输入POP3Port端口!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtMaxSendCount.Text.Trim()))
         {
             "请输最多發送數量!".ShowDialog();
             return;
         }
         if (string.IsNullOrWhiteSpace(txtInterval.Text.Trim()))
         {
             "请選擇發送間隔!".ShowDialog();
             return;
         }
         var category = this.cbCategory.SelectedItem as EmailAccountCategoryEntity;
         var entity   = EA_Repository.GetByAddress(txtEmailAddress.Text.Trim());
         if (this.EmailAccountID == -1 && entity != null)
         {
             "郵箱已存在,請不要重複添加".ShowDialog();
             return;
         }
         if (this.EmailAccountID != -1 && entity != null && this.EmailAccountID != entity.EmailAccountID)
         {
             "郵箱已存在".ShowDialog();
             return;
         }
         if (this.EmailAccountID == -1)
         {
             entity = new EmailAccountEntity
             {
                 EmailAccountAddress       = txtEmailAddress.Text.Trim(),
                 EmailAccountName          = txtName.Text.Trim(),
                 EmailAccountIsSSL         = cbSSL.SelectedIndex,
                 EmailAccountMaxEmailCount = int.Parse(txtMaxSendCount.Text.Trim()),
                 EmailAccountSpace         = int.Parse(txtInterval.Text.Trim()),
                 EmailAccountPassWord      = txtPassword.Text.Trim(),
                 EmailAccountPOP3          = txtPOP3.Text.Trim(),
                 EmailAccountPOP3Port      = int.Parse(txtPOPPort.Text.Trim()),
                 EmailAccountSMTP          = txtSMTP.Text.Trim(),
                 EmailAccountSMTPPort      = int.Parse(txtSMTPPort.Text.Trim()),
                 SendMode = (this.cbSendMode.Text == "發送" ? 0 : 1),
                 EmailAccountCategoryID   = category.ID,
                 EmailAccountCategoryName = category.Name
             };
             LogHelper.Info(string.Format("新增郵箱【{0}】", entity.EmailAccountAddress));
             EA_Repository.Insert(entity);
             LogHelper.Info(string.Format("新增郵箱【{0}】成功", entity.EmailAccountAddress));
         }
         else
         {
             entity = EA_Repository.Get(this.EmailAccountID);
             entity.EmailAccountAddress       = txtEmailAddress.Text.Trim();
             entity.EmailAccountName          = txtName.Text.Trim();
             entity.EmailAccountIsSSL         = cbSSL.SelectedIndex;
             entity.EmailAccountMaxEmailCount = int.Parse(txtMaxSendCount.Text.Trim());
             entity.EmailAccountSpace         = int.Parse(txtInterval.Text.Trim());
             entity.EmailAccountPassWord      = txtPassword.Text.Trim();
             entity.EmailAccountPOP3          = txtPOP3.Text.Trim();
             entity.EmailAccountPOP3Port      = int.Parse(txtPOPPort.Text.Trim());
             entity.EmailAccountSMTP          = txtSMTP.Text.Trim();
             entity.EmailAccountSMTPPort      = int.Parse(txtSMTPPort.Text.Trim());
             entity.SendMode = (this.cbSendMode.Text == "發送" ? 0 : 1);
             entity.EmailAccountCategoryID   = category.ID;
             entity.EmailAccountCategoryName = category.Name;
             LogHelper.Info(string.Format("修改郵箱【{0}】", entity.EmailAccountAddress));
             EA_Repository.Update(entity);
             LogHelper.Info(string.Format("修改郵箱【{0}】成功", entity.EmailAccountAddress));
         }
         if (this.EmailAccountID == -1)
         {
             "郵箱添加成功!".ShowDialog();
         }
         else
         {
             "郵箱修改成功!".ShowDialog();
         }
         this.DialogResult = true;
         this.Close();
     }
     catch (Exception ex)
     {
         if (this.EmailAccountID == -1)
         {
             LogHelper.Error(string.Format("新增郵箱【{0}】異常", txtEmailAddress.Text.Trim()), ex);
             "郵箱添加失败!".ShowDialog();
         }
         else
         {
             LogHelper.Error(string.Format("修改郵箱【{0}】為【{1}】異常", this.EmailAccountID, txtEmailAddress.Text.Trim()), ex);
             "郵箱修改失败!".ShowDialog();
         }
     }
 }
Exemplo n.º 2
0
        private (int, List <FailEmailAccountEntity>) LoadMoreData(string filePath)
        {
            List <FailEmailAccountEntity> failEntity = new List <FailEmailAccountEntity>();

            FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
            //根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档
            HSSFWorkbook workbook = new HSSFWorkbook(stream);
            //获取excel的第一个sheet
            HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);
            //最后一列的标号  即总的行数
            int rowCount = sheet.LastRowNum;
            int total    = 0;

            for (int i = (sheet.FirstRowNum + 1); i < sheet.LastRowNum + 1; i++)
            {
                total++;
                FailEmailAccountEntity ea = new FailEmailAccountEntity();
                try
                {
                    HSSFRow row = (HSSFRow)sheet.GetRow(i);
                    if (row.GetCell(0) != null)
                    {
                        ea.EmailAccountName = row.GetCell(0).ToString();
                    }
                    if (row.GetCell(1) != null)
                    {
                        ea.EmailAccountAddress = row.GetCell(1).ToString();
                    }
                    if (row.GetCell(2) != null)
                    {
                        ea.EmailAccountPassWord = row.GetCell(2).ToString();
                    }
                    if (row.GetCell(3) != null)
                    {
                        ea.EmailAccountSMTP = row.GetCell(3).ToString();
                    }
                    if (row.GetCell(4) != null)
                    {
                        ea.EmailAccountSMTPPort = int.Parse(row.GetCell(4).ToString());
                    }
                    if (row.GetCell(5) != null)
                    {
                        ea.EmailAccountPOP3 = row.GetCell(5).ToString();
                    }
                    if (row.GetCell(6) != null)
                    {
                        ea.EmailAccountPOP3Port = int.Parse(row.GetCell(6).ToString());
                    }
                    if (row.GetCell(7) != null)
                    {
                        ea.EmailAccountIsSSL = row.GetCell(7).ToString() == "是" ? 1 : 0;
                    }
                    if (row.GetCell(8) != null)
                    {
                        ea.EmailAccountMaxEmailCount = int.Parse(row.GetCell(8).ToString());
                    }
                    if (row.GetCell(9) != null)
                    {
                        ea.EmailAccountSpace = int.Parse(row.GetCell(9).ToString());
                    }
                    if (row.GetCell(10) != null)
                    {
                        ea.SendMode = row.GetCell(10).ToString() == "發送" ? 0 : 1;
                    }
                    if (row.GetCell(11) != null)
                    {
                        ea.EmailAccountCategoryName = row.GetCell(11).ToString();
                    }
                    ea.EmailAccountCategoryID = -1;
                    ea.SendMode = 1;//密送
                    ea.EmailAccountCreateTime = DateTime.Now;
                    ea.EmailAccountLastTime   = DateTime.Now;
                    LogHelper.Info(string.Format("準備導入郵箱【{0}】", ea.EmailAccountAddress));
                    if (string.IsNullOrWhiteSpace(ea.EmailAccountAddress))
                    {
                        ea.FailMessage = "郵箱地址為空";
                        failEntity.Add(ea);
                        continue;
                    }
                    if (!MailHelper.IsEmail(ea.EmailAccountAddress))
                    {
                        LogHelper.Info(string.Format("導入郵箱【{0}】地址不正確", ea.EmailAccountAddress));
                        ea.FailMessage = "郵箱地址不正確";
                        failEntity.Add(ea);
                        continue;
                    }
                    if (EA_Repository.GetByAddress(ea.EmailAccountAddress) != null)
                    {
                        LogHelper.Info(string.Format("導入郵箱【{0}】已存在", ea.EmailAccountAddress));
                        ea.FailMessage = "郵箱已存在";
                        failEntity.Add(ea);
                        continue;
                    }
                    if (!string.IsNullOrWhiteSpace(ea.EmailAccountCategoryName))
                    {
                        var entity = EAC_Repository.Get(ea.EmailAccountCategoryName);
                        if (entity != null)
                        {
                            ea.EmailAccountCategoryID = entity.ID;
                        }
                        else
                        {
                            LogHelper.Info(string.Format("導入郵箱【{0}】,創建分類【{1}】", ea.EmailAccountAddress, ea.EmailAccountCategoryName));
                            EAC_Repository.Insert(ea.EmailAccountCategoryName);
                        }
                        entity = EAC_Repository.Get(ea.EmailAccountCategoryName);
                        if (entity != null)
                        {
                            ea.EmailAccountCategoryID = entity.ID;
                        }
                    }
                    EA_Repository.Insert(ea);
                    LogHelper.Info(string.Format("導入郵箱【{0}】成功", ea.EmailAccountAddress));
                }
                catch (Exception ex)
                {
                    LogHelper.Error(string.Format("導入郵箱【{0}】異常", ea.EmailAccountAddress), ex);
                    ea.FailMessage = ex.Message;
                    failEntity.Add(ea);
                }
            }
            return(total, failEntity);
        }