File name
        Commit message
        Commit date
    File name
        Commit message
        Commit date
    File name
        Commit message
        Commit date
    File name
        Commit message
        Commit date
    using AegisImplicitMail;
using System.Net.Mail;
using ClientLib;
using ClientLib.CommonService;
using DevExpress.XtraEditors;
using Popbill.Fax;
using PublicLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace HANMI_SALE
{
    partial class CustMailPopup : XtraForm
    {
        private string m_EMAIL, m_USER_ID, m_USER_PWD, m_SERVER_ADDR, m_BizNO;
        private int m_SERVER_PORT;
        private FaxService m_FaxService = null;
        public string m_CUST_CD;
        public string m_CUST_NN;
        public string m_TEL_NO;
        public string m_OWNER_TEL_NO;
        public string m_MGR_TEL_NO;
        public CustMailPopup(string cust_nm)
        {
            InitializeComponent();
            m_BizNO = UtilClass.toStr(ConstClass._GET_USER_INFO("BIZ_NO")).Replace("-", "");
            m_EMAIL = "";
            m_USER_ID = "";
            m_USER_PWD = "";
            m_SERVER_ADDR = "";
            m_SERVER_PORT = 0;
            textEdit_Search.ImeMode = System.Windows.Forms.ImeMode.Hangul;
            textEdit_Search.Text = cust_nm;
            this.Shown += (sender, e) =>
            {
                progressBarControl_Send.Properties.DisplayFormat.FormatString = "{0:#,##0} / " + 0.ToString("#,##0");
                progressBarControl_Send.Position = 0;
                progressBarControl_Send.Properties.Minimum = 0;
                progressBarControl_Send.Properties.Maximum = 0;
                progressBarControl_Send.Visible = true;
                Application.DoEvents();
                DataView s06 = new DataView(ConstClass._DATA);
                s06.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '06'";
                DataView s07 = new DataView(ConstClass._DATA);
                s07.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '07'";
                DataView s11 = new DataView(ConstClass._DATA);
                s11.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '11'";
                if (s06.Count > 0 || s07.Count > 0 || s11.Count > 0)
                {
                    string linkID = UtilClass.toStr(s06[0]["NO_CD_NM"]);
                    string key = UtilClass.toStr(s07[0]["NO_CD_NM"]);
                    if (!UtilClass.isNull(linkID) && !UtilClass.isNull(key))
                    {
                        m_FaxService = new FaxService(linkID, key);
                        m_FaxService.IsTest = s11.Count > 0 && UtilClass.isEqual(s11[0]["NO_CD_NM"], "Y");
                    }
                }
                if (m_FaxService != null)
                {
                    double remainPoint = m_FaxService.GetBalance(m_BizNO);
                    //labelControl_Point.Text = remainPoint.ToString("#,##0") + " P";
                }
                DataView s01 = new DataView(ConstClass._DATA);
                s01.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '01'";
                if (s01.Count > 0)
                {
                    m_EMAIL = UtilClass.toStr(s01[0]["NO_CD_NM"]);
                }
                DataView view = new DataView(ConstClass._DATA);
                view.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '01'";
                m_USER_ID = UtilClass.toStr(view[0]["NO_CD_NM"]);
                view.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '02'";
                m_USER_PWD = UtilClass.toStr(view[0]["NO_CD_NM"]);
                view.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '03'";
                m_SERVER_ADDR = UtilClass.toStr(view[0]["NO_CD_NM"]);
                view.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '04'";
                m_SERVER_PORT = UtilClass.toInt(view[0]["NO_CD_NM"]);
                searchProc();
            };
            textEdit_Search.KeyPress += (sender, e) =>
            {
                if (e.KeyChar == '\r')
                {
                    searchProc();
                }
            };
            simpleButton_Text_Clear.Click += (sender, e) =>
            {
                textEdit_Search.Text = "";
            };
            simpleButton_Search.Click += (sender, e) =>
            {
                searchProc();
            };
            radioGroup_CUST_KIND.EditValueChanged += (sender, e) =>
            {
                searchProc();
            };
            radioGroup_ORDER_BY.EditValueChanged += (sender, e) =>
            {
                searchProc();
            };
            radioGroup_CUST_NAME_GBN.EditValueChanged += (sender, e) =>
            {
                searchProc();
            };
            gridView_Main.KeyPress += (sender, e) =>
            {
                if (e.KeyChar == '\r')
                {
                    DataRow row = gridView_Main.GetFocusedDataRow();
                    if (row != null)
                    {
                        m_CUST_CD = UtilClass.toStr(row["CUST_CD"]);
                        m_CUST_NN = UtilClass.toStr(row["CUST_NM"]);
                        m_TEL_NO = UtilClass.toStr(row["TEL_NO"]);
                        m_OWNER_TEL_NO = UtilClass.toStr(row["OWNER_TEL_NO"]);
                        m_MGR_TEL_NO = UtilClass.toStr(row["MGR_TEL_NO"]);
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            };
            gridView_Main.DoubleClick += (sender, e) =>
            {
                DataRow row = gridView_Main.GetFocusedDataRow();
                if (row != null)
                {
                    m_CUST_CD = UtilClass.toStr(row["CUST_CD"]);
                    m_CUST_NN = UtilClass.toStr(row["CUST_NM"]);
                    m_TEL_NO = UtilClass.toStr(row["TEL_NO"]);
                    m_OWNER_TEL_NO = UtilClass.toStr(row["OWNER_TEL_NO"]);
                    m_MGR_TEL_NO = UtilClass.toStr(row["MGR_TEL_NO"]);
                    this.DialogResult = DialogResult.Yes;
                    this.Close();
                }
            };
            simpleButton_Cancel.Click += (sender, e) =>
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            };
        }
        public void searchProc()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                gridControl_Main.DataSource = null;
                SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                    ClientClass.CreateSqlParameter("COMP_CD", SqlDbType.NVarChar, ConstClass._COMP_CD),
                    ClientClass.CreateSqlParameter("CUST_KIND", SqlDbType.NVarChar, radioGroup_CUST_KIND.EditValue),
                    ClientClass.CreateSqlParameter("ORDER_BY", SqlDbType.Int, radioGroup_ORDER_BY.EditValue),
                    ClientClass.CreateSqlParameter("DEL_YN", SqlDbType.NVarChar, checkBox_DEL_YN.Checked ? 'N':'Y'),
                    ClientClass.CreateSqlParameter("CUST_NAME_GBN", SqlDbType.Int, radioGroup_CUST_NAME_GBN.EditValue),
                    ClientClass.CreateSqlParameter("SEARCH", SqlDbType.NVarChar, textEdit_Search.Text)
                };
                ResultData resultData = ClientClass.GetData("GetCustSelectPopup_M", aParam);
                if (resultData.isError)
                {
                    throw new Exception(resultData.ResultValue);
                }
                gridControl_Main.DataSource = resultData.TableData;
                gridView_Main.Focus();
                this.Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                XtraMessageBox.Show(ex.Message);
            }
        }
        private void simpleButton_SelectAll_Click(object sender, EventArgs e)
        {
            DataTable aData = gridControl_Main.DataSource as DataTable;
            foreach (DataRow r in aData.Rows)
            {
                r["SEL_YN"] = "Y";
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                int count = 0;
                DataTable aData = gridControl_Main.DataSource as DataTable;
                foreach (DataRow r in aData.Rows)
                {
                    if (UtilClass.toStr(r["SEL_YN"]) == "Y")
                    {
                        count++;
                    }
                }
                progressBarControl_Send.Properties.DisplayFormat.FormatString = "{0:#,##0} / " + count.ToString("#,##0");
                progressBarControl_Send.Position = 0;
                progressBarControl_Send.Properties.Minimum = 0;
                progressBarControl_Send.Properties.Maximum = count;
                progressBarControl_Send.Visible = true;
                Application.DoEvents();
                if (XtraMessageBox.Show(count + " 건의 메일을 발송하시겠습니까? (담당자 이메일 주소가 없는 경우 전송되지 않습니다)", "메일 전송", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }
                List<string> lstAddr = new List<string>();
                int nSucc = 0;
                int nFail = 0;
                int nSkip = 0;
                int nJung = 0;
                foreach (DataRow r in aData.Rows)
                {
                    if (UtilClass.toStr(r["SEL_YN"]) == "Y")
                    {
                        string strEMail = UtilClass.toStr(r["MGR_EMAIL"]).Trim();
                        //strEMail = "kang@signus.kr";
                        if (strEMail.Length > 0)
                        {
                            if (lstAddr.FirstOrDefault(x => x == strEMail) == null)
                            {
                                lstAddr.Add(strEMail);
                                if (sendMail(strEMail))
                                    nSucc++;
                                else
                                    nFail++;
                            }
                            else
                            {
                                nJung++;
                            }
                        }
                        else
                        {
                            nSkip++;
                        }
                    }
                    progressBarControl_Send.Position++;
                    Application.DoEvents();
                }
                string strM = "메일 발송이 완료되었습니다.";
                strM += "\n\n전송 완료: " + nSucc.ToString();
                strM += "\n\n전송 실패: " + nFail.ToString();
                strM += "\n\n메일주소 중복: " + nJung.ToString();
                strM += "\n\n메일주소 없음: " + nSkip.ToString();
                XtraMessageBox.Show(strM);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
        private void simpleButton_SEND_FAX_Click(object sender, EventArgs e)
        {
            try
            {
                DataView s08 = new DataView(ConstClass._DATA);
                s08.RowFilter = "CATE_CD = 'S' and GRP_CD = 'S01' and NO_CD = '08'";
                if (s08.Count < 1)
                {
                    throw new Exception("팩스 전송을 위한 환경정보가 없습니다.");
                }
                string userID = UtilClass.toStr(s08[0]["NO_CD_NM"]);
                if (UtilClass.isNull(userID))
                {
                    throw new Exception("팩스 전송을 위한 팝빌 등록정보가 없습니다.");
                }
                object faxObj = ConstClass._GET_USER_INFO("FAX_NO");
                string myFaxNo = UtilClass.toStr(faxObj);
                if (UtilClass.isNull(myFaxNo) || UtilClass.isNull(m_BizNO))
                {
                    throw new Exception("회사정보관리에서 팩스번호 및 사업자번호를 입력하신 후, 시스템을 재시작하시길 바랍니다.");
                }
                int count = 0;
                DataTable aData = gridControl_Main.DataSource as DataTable;
                foreach (DataRow r in aData.Rows)
                {
                    if (UtilClass.toStr(r["SEL_YN"]) == "Y")
                    {
                        count++;
                    }
                }
                progressBarControl_Send.Properties.DisplayFormat.FormatString = "{0:#,##0} / " + count.ToString("#,##0");
                progressBarControl_Send.Position = 0;
                progressBarControl_Send.Properties.Minimum = 0;
                progressBarControl_Send.Properties.Maximum = count;
                progressBarControl_Send.Visible = true;
                Application.DoEvents();
                if (XtraMessageBox.Show("팩스는 제목과 내용은 무시되고, 첨부 파일만 발송됩니다. 계속 하시겠습니까?", "팩스 발송", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }
                if (XtraMessageBox.Show(count + " 건의 팩스를 발송하시겠습니까? (팩스 번호가 없는 경우 전송되지 않습니다)", "팩스 발송", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    return;
                }
                List<string> lstAddr = new List<string>();
                int nSucc = 0;
                int nFail = 0;
                int nSkip = 0;
                int nJung = 0;
                foreach (DataRow r in aData.Rows)
                {
                    if (UtilClass.toStr(r["SEL_YN"]) == "Y")
                    {
                        string strEMail = UtilClass.toStr(r["FAX_NO"]).Trim();
                        //strEMail = "0504-080-9514";
                        if (strEMail.Length > 0)
                        {
                            if (lstAddr.FirstOrDefault(x => x == strEMail) == null)
                            {
                                lstAddr.Add(strEMail);
                                if (sendFax(userID, m_BizNO, myFaxNo, strEMail, UtilClass.toStr(r["CUST_NM"]), this.textEdit_Subject.Text))
                                    nSucc++;
                                else
                                    nFail++;
                            }
                            else
                            {
                                nJung++;
                            }
                        }
                        else
                        {
                            nSkip++;
                        }
                    }
                    progressBarControl_Send.Position++;
                    Application.DoEvents();
                }
                string strM = "팩스 발송이 완료되었습니다.";
                strM += "\n\n전송 완료: " + nSucc.ToString();
                strM += "\n\n전송 실패: " + nFail.ToString();
                strM += "\n\n팩스번호 중복: " + nJung.ToString();
                strM += "\n\n팩스번호 없음: " + nSkip.ToString();
                XtraMessageBox.Show(strM);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }
        private void simpleButton_SelectNon_Click(object sender, EventArgs e)
        {
            DataTable aData = gridControl_Main.DataSource as DataTable;
            foreach (DataRow r in aData.Rows)
            {
                r["SEL_YN"] = "N";
            }
        }
        private void simpleButton_FileFind_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "파일열기";
            if (openFileDialog.ShowDialog() != DialogResult.OK)
                return;
            this.textEdit_File.Text = openFileDialog.FileName;
        }
        private bool sendMail(string email)
        {
            ResultClass result = new ResultClass();
            try
            {
                MimeMailMessage mail = new MimeMailMessage();
                mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString("EMAIL", Encoding.UTF8, "text/html"));
                mail.IsBodyHtml = false;
                mail.Body = this.textEdit_Content.Text;
                mail.BodyEncoding = Encoding.UTF8;
                mail.Subject = this.textEdit_Subject.Text;
                mail.SubjectEncoding = Encoding.UTF8;
                mail.Sender = new MimeMailAddress(m_EMAIL, getEncodeDisplayName(ConstClass._COMP_NM));
                mail.From = new MimeMailAddress(m_EMAIL, getEncodeDisplayName(ConstClass._COMP_NM));
                mail.To.Add(email);
                string fileName = this.textEdit_File.Text;
                if (System.IO.File.Exists(fileName))
                {
                    MimeAttachment attch = new MimeAttachment(fileName);
                    Application.DoEvents();
                    mail.Attachments.Add(attch);
                    Application.DoEvents();
                }
                SmtpSocketClient smtp = new SmtpSocketClient();
                smtp.Host = m_SERVER_ADDR;
                smtp.Port = m_SERVER_PORT;
                smtp.SslType = SslMode.Ssl;
                smtp.User = m_USER_ID;
                smtp.Password = m_USER_PWD;
                smtp.AuthenticationMode = AuthenticationType.Base64;
                smtp.SendCompleted += (sendere, ee) =>
                {
                    if (ee.Error != null)
                    {
                        throw new Exception(ee.Error.Message);
                    }
                    else
                    {
                    }
                };
                smtp.SendMail(mail);
                return true;
            }
            catch (Exception ex)
            {
                //result.isError = true;
                //result.ResultStr = ex.Message;
                XtraMessageBox.Show(ex.Message);
            }
            return false;
        }
        private bool sendFax(string userID, string bizNo, string fromFax, string toFax, string toName, string strTitle)
        {
            ResultClass result = new ResultClass();
            try
            {
                string requestNum = "HANMI" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string fileName = this.textEdit_File.Text;
                if (System.IO.File.Exists(fileName))
                {
                    DateTime? reserveDT = null;
                    toName = toName + " 귀하";
                    m_FaxService.SendFAX(bizNo.Replace("-", "").Replace(" ", ""), fromFax, toFax, toName,
                            fileName, reserveDT, userID, false, strTitle, requestNum);
                    return true;
                }
            }
            catch (Exception ex)
            {
                //result.isError = true;
                //result.ResultStr = ex.Message;
                XtraMessageBox.Show(ex.Message);
            }
            return false;
        }
        private string getEncodeDisplayName(string str)
        {
            var fromEncoding = Encoding.UTF8;
            var encodingName = fromEncoding.BodyName.ToLower();
            return "=?" + encodingName + "?B?" + TransferEncoder.ToBase64(fromEncoding.GetBytes(str)) + "?=";
        }
    }
}