Beispiel #1
0
        public POP3_Parsing GetMessage(int messageNumber)
        {
            POP3_Parsing parse = new POP3_Parsing();

            try
            {
                parse.MailMessage(RetrieveMessage(messageNumber));

                parse.ParseHeader();
                parse.ParseBody();
                parse.ConvertStringBoundaryBody();

                parse.IsParseSuccess = true;

                AppLog.Write(LOG_LEVEL.MSG, string.Format("[ Subject: {0} ] 메일을 수신 완료하였습니다.", parse.Subject));

                #region For Debug
                //AppLog.Write(LOG_LEVEL.TRC, "----------------------------------------------------------------------------------------------------");
                //AppLog.Write(LOG_LEVEL.TRC, parse.Subject);

                //foreach (POP3_BoundaryInfo info in parse.BoundaryInfoList)
                //{
                //    AppLog.IsDisplay = false;
                //    AppLog.Write(LOG_LEVEL.TRC, "----------------------------------------------------------------------------------------------------");
                //    AppLog.Write(LOG_LEVEL.TRC, "Convert Body                     : " + info.ContentType);
                //    AppLog.Write(LOG_LEVEL.TRC, "CharSet                          : " + info.ContentTypeCharSet);
                //    AppLog.Write(LOG_LEVEL.TRC, "ContentTransferEncoding          : " + info.ContentTransferEncoding);
                //    AppLog.Write(LOG_LEVEL.TRC, "ConvertBody                      : " + info.ConvertBody);
                //    AppLog.Write(LOG_LEVEL.TRC, "ContentTypeName                  : " + info.ContentTypeName);
                //    AppLog.Write(LOG_LEVEL.TRC, "ContentDispositionAttachFileName : " + info.ContentDispositionAttachFileName);
                //    AppLog.IsDisplay = true;
                //}
                //AppLog.Write(LOG_LEVEL.TRC, " ");
                //AppLog.Write(LOG_LEVEL.TRC, " ");
                //AppLog.Write(LOG_LEVEL.TRC, " ");
                //AppLog.Write(LOG_LEVEL.TRC, " ");
                //AppLog.Write(LOG_LEVEL.TRC, " ");
                //AppLog.Write(LOG_LEVEL.TRC, " ");
                #endregion
            }
            catch (Exception ex)
            {
                AppLog.ExceptionLog(ex, string.Format("[ Subject:{0} ] 메일을 수신 실패하였습니다.", parse.Subject));
            }
            return(parse);
        }
        /// <summary>
        /// Name       : PollingLoop_
        /// Parameters : Empty
        /// Content    : 폴링 루프
        /// Return     : Empty
        /// Writer     : 장동훈
        /// Date       : 2012.09.03
        /// </summary>
        private void PollingLoop_()
        {
            POP3_Client mail = new POP3_Client();

            DbModule.SendRequestMasterInfo     reqInfo_mstr     = null;
            DbModule.SendRequestDetailInfo     reqInfo_dtl      = null;
            DbModule.SEND_REQ_SITE             reqInfo_site     = null;
            List <DbModule.SendRequestDocInfo> reqInfo_doc_List = null;
            P_TYPE destProcessType   = P_TYPE.PSC;
            int    retryConnectCount = 0;   // POP3 접속 실패시 3회 재시도
            int    retryLoginCount   = 0;   // POP3 로그인 실패시 3회 재시도

            /// 마지막 실행시간 (Sleep 실행의 기준시간)
            /// 첫 Sleep 실행시 바로 실행 되도록 시간 수정
            DateTime lastExecuteTime = DateTime.Now.AddMilliseconds(Config.EMAIL_POLLING_TIME * -1);

            try
            {
                while (!BtfaxThread.Stop)
                {
                    if (DateTime.Now.Subtract(lastExecuteTime).TotalMilliseconds < Config.EMAIL_POLLING_TIME)
                    {
                        Thread.Sleep(1000);
                        continue;
                    }
                    lastExecuteTime = DateTime.Now;

                    try
                    {
                        if (mail.IsConnected == false &&
                            !mail.Connect(Config.EMAIL_POP_SERVER, Config.EMAIL_POP3_SERVER_PORT))
                        {
                            if (retryConnectCount++ < 3)
                            {
                                AppLog.Write(LOG_LEVEL.ERR, string.Format("IP: {0}, PORT: {1} 로 접속중 오류가 발생하였습니다. ({2} 회 접속 시도)", Config.EMAIL_POP_SERVER, Config.EMAIL_POP3_SERVER_PORT, retryConnectCount));

                                /// 0.5 초 대기
                                for (int i = 0; i < 5; i++)
                                {
                                    Thread.Sleep(100);
                                }

                                mail = new POP3_Client();
                                continue;
                            }
                            else
                            {
                                AppLog.Write(LOG_LEVEL.ERR, "해당 서버로 접속 연결이 되지 않습니다.");
                                throw new Exception("접속 에러");
                            }
                        }
                        if (mail.IsLogin == false &&
                            !mail.Login(Config.EMAIL_ID, Config.EMAIL_PW))
                        {
                            if (retryLoginCount++ < 3)
                            {
                                AppLog.Write(LOG_LEVEL.ERR, string.Format("ID: {0}, PASSWORD: {1} 로 로그인중 오류가 발생하였습니다. ({2} 회 로그인 시도)", Config.EMAIL_ID, string.Empty.PadRight(Config.EMAIL_PW.Length, '*'), retryLoginCount));

                                /// 0.5 초 대기
                                for (int i = 0; i < 5; i++)
                                {
                                    Thread.Sleep(100);
                                }

                                continue;
                            }
                            else
                            {
                                AppLog.Write(LOG_LEVEL.ERR, "해당 서버로 로그인이 되지 않습니다.");
                                throw new Exception("로그인 에러");
                            }
                        }

                        /// 초기화
                        retryConnectCount = 0;
                        retryLoginCount   = 0;

                        int messageCount = mail.GetMailCount();
                        for (int i = 1; i <= messageCount; i++)
                        {
                            POP3_Parsing parseMail = mail.GetMessage(i);

                            if (parseMail.IsParseSuccess == false)
                            {
                                continue;
                            }

                            if (ParseEmailDocument(parseMail,
                                                   ref reqInfo_mstr,
                                                   ref reqInfo_dtl,
                                                   ref reqInfo_site,
                                                   ref reqInfo_doc_List,
                                                   ref destProcessType) != RESULT.SUCCESS)
                            {
                                continue;
                            }

                            /// Master, Detail DB Insert
                            if (InsertToDb(reqInfo_mstr, reqInfo_dtl, reqInfo_site) != RESULT.SUCCESS)
                            {
                                continue;
                            }

                            /// Doc DB Insert
                            if (InsertToDbDoc(reqInfo_mstr.OUT_FAX_ID, reqInfo_doc_List) != RESULT.SUCCESS)
                            {
                                continue;
                            }

                            // FAXBOX Insert - 20130124
                            if (InsertToDbFaxbox(reqInfo_mstr) != RESULT.SUCCESS)
                            {
                                continue;
                            }

                            /// Pass Over
                            if (PassOverSendRequest(destProcessType, reqInfo_mstr, reqInfo_dtl) != RESULT.SUCCESS)
                            {
                                continue;
                            }

                            break;
                        }

                        /// 메일함 Update
                        mail.Disconnect();

                        lastExecuteTime = DateTime.Now;
                    }
                    catch (Exception ex)
                    {
                        lastExecuteTime = DateTime.Now;

                        AppLog.ExceptionLog(ex, string.Format("{0} 쓰레드에서 다음과 같은 오류가 발생하였습니다.-1", m_ThreadName));
                        if (mail.IsConnected == true)
                        {
                            mail.ResetDeleteMessage();
                        }
                        AppLog.ExceptionLog(ex, string.Format("{0} 쓰레드에서 다음과 같은 오류가 발생하였습니다.-2", m_ThreadName));
                        //throw ex;
                    }
                }   // while
            }
            catch (Exception ex)
            {
                AppLog.ExceptionLog(ex, string.Format("{0} 쓰레드에서 다음과 같은 오류가 발생하였습니다. 쓰레드를 종료합니다.-1", m_ThreadName));
                if (mail.IsConnected == true)
                {
                    mail.ResetDeleteMessage();
                }
                AppLog.ExceptionLog(ex, string.Format("{0} 쓰레드에서 다음과 같은 오류가 발생하였습니다. 쓰레드를 종료합니다.-2", m_ThreadName));
                throw ex;
            }
        }
        private RESULT ParseEmailDocument(POP3_Parsing p_parseMail,
                                          ref DbModule.SendRequestMasterInfo p_reqInfo_mstr,
                                          ref DbModule.SendRequestDetailInfo p_reqInfo_dtl,
                                          ref DbModule.SEND_REQ_SITE p_reqInfo_site,
                                          ref List <DbModule.SendRequestDocInfo> p_reqInfo_doc_List,
                                          ref P_TYPE p_destProcessType)
        {
            p_reqInfo_mstr     = new DbModule.SendRequestMasterInfo(m_ThreadName);
            p_reqInfo_dtl      = new DbModule.SendRequestDetailInfo();
            p_reqInfo_site     = new DbModule.SEND_REQ_SITE();
            p_reqInfo_doc_List = new List <DbModule.SendRequestDocInfo>();

            try
            {
                /// 첨부파일 Boundary 추출
                List <POP3_BoundaryInfo> attachFileList = GetAttachFileList(p_parseMail.BoundaryInfoList);

                if (attachFileList.Count == 0)
                {
                    return(RESULT.F_FILE_CNT_ZERO);
                }

                /// 메일 제목 분석
                if (ParseEmailTitle(p_parseMail.Subject, ref p_reqInfo_mstr, ref p_reqInfo_dtl) == false)
                {
                    AppLog.Write(LOG_LEVEL.ERR, string.Format("Email 제목이 형식에 맞지 않습니다. ({0} / {1})", Config.EMAIL_TITLE_FORMAT, p_parseMail.Subject));
                    //continue;
                    return(RESULT.F_PARSE_ERROR);
                }

                /// REQ_TYPE, Dest Process Type 결정
                SetReqType(attachFileList, ref p_reqInfo_mstr, ref p_reqInfo_dtl, ref p_destProcessType);

                /// PSC 는 tif 1개로 DCV, TPC 필요없음.
                if (p_destProcessType == P_TYPE.PSC)
                {
                    POP3_BoundaryInfo bi = attachFileList[0];

                    /// Email AttachFile -> Local File
                    string filePath = p_parseMail.ConvertBase64ToFile(m_DownloadEmailAttachFileLocalPath, bi);
                    if (filePath.Length == 0)
                    {
                        return(RESULT.F_FILE_NOT_EXIST);
                    }

                    /// File name is must unique. (File Rename)
                    FileInfo fi = new FileInfo(filePath);
                    do
                    {
                        fi.MoveTo(string.Format(@"{0}\{1}_{2}.{3}", fi.Directory, bi.RepresentationFilenameOnly, DateTime.Now.ToString("yyyyMMddHHmmss.fffffff"), bi.RepresentationFilenameExtension));
                        //} while (File.Exists(Path.Combine(m_AoxInputDocPath, fi.Name)));
                    } while (File.Exists(Path.Combine(Config.FINISHED_TIF_PATH, m_RelativePath, fi.Name)));
                    /// Local -> Destination
                    //fi.MoveTo(Path.Combine(m_AoxInputDocPath, fi.Name));
                    fi.MoveTo(Path.Combine(Config.FINISHED_TIF_PATH, m_RelativePath, fi.Name));

                    //p_reqInfo_dtl.TIF_FILE = Path.Combine(m_RelativePath, attachFileList[0].RepresentationFilename);
                    p_reqInfo_dtl.TIF_FILE = Path.Combine(m_RelativePath, fi.Name);
                }
                else
                {
                    /// 첨부파일 AOX_INPUT_DOC_PATH 로 이동
                    foreach (POP3_BoundaryInfo bi in attachFileList)
                    {
                        /// Email AttachFile -> Local File
                        string filePath = p_parseMail.ConvertBase64ToFile(m_DownloadEmailAttachFileLocalPath, bi);
                        if (filePath.Length == 0)
                        {
                            continue;
                        }

                        /// File name is must unique.
                        FileInfo fi = new FileInfo(filePath);
                        do
                        {
                            fi.MoveTo(string.Format(@"{0}\{1}_{2}.{3}", fi.Directory, bi.RepresentationFilenameOnly, DateTime.Now.ToString("yyyyMMddHHmmss.fffffff"), bi.RepresentationFilenameExtension));
                        } while (File.Exists(Path.Combine(m_AoxInputDocPath, fi.Name)));
                        /// Local -> Destination
                        fi.MoveTo(Path.Combine(m_AoxInputDocPath, fi.Name));

                        /// DOC info
                        DbModule.SendRequestDocInfo doc = new DbModule.SendRequestDocInfo();
                        doc.DOC_PATH   = Config.AOX_INPUT_DOCS_PATH;
                        doc.strDocFile = fi.Name.Substring(0, fi.Name.LastIndexOf("."));
                        doc.strDocExt  = fi.Name.Substring(fi.Name.LastIndexOf(".") + 1);

                        p_reqInfo_doc_List.Add(doc);
                    }
                }

                AppLog.Write(LOG_LEVEL.MSG, string.Format("[ Subject: {0} ] 메일을 분석 완료하였습니다.", p_parseMail.Subject));
                return(RESULT.SUCCESS);
            }
            catch (Exception ex)
            {
                AppLog.ExceptionLog(ex, "");

                return(RESULT.F_PARSE_ERROR);
            }
        }