public string ConvertBase64ToFile(string localPath, POP3_BoundaryInfo bi) { if (bi.IsFileAttach == false) { return(""); } if (string.Compare(bi.ContentTransferEncoding, "base64", true) != 0) { return(""); } if (bi.Encoding != null) { return(""); } string fileName = bi.RepresentationFilename; if (fileName.Length == 0) { return(""); } if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); } string filePath = string.Format("{0}{1}", localPath, fileName); try { byte[] data = ConvertBase64ToByteArray(GetBodyLines(bi)); using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(data); bw.Flush(); } } catch (Exception ex) { AppLog.ExceptionLog(ex, "파일 생성에 실패했습니다."); filePath = ""; } return(filePath); }
private void ParseBoundaryHeaderType(POP3_BoundaryInfo bi, string headerType) { string[] splitLines = headerType.Split(';'); foreach (string line in splitLines) { Match m = Regex.Match(line, @"(?<key>[\wd\-<>@+=./?]+)[ ]*[:=][ ]*(?<value>[\w\d\-=_/.,: ()""?+]+)"); if (m.Success == true) { string key = m.Groups["key"].Value.ToUpper(); string value = DeleteSpecialCharacter(DecodingOneLine(m.Groups["value"].Value)); if (key == "CONTENT-TYPE") { bi.ContentType = value; bi.ContentTypeMain = value.Split('/')[0]; bi.ContentTypeSub = value.Split('/')[1]; } else if (key == "CHARSET") { bi.ContentTypeCharSet = value; } else if (key == "BOUNDARY") { bi.ChildBoundaryID = value; } else if (key == "NAME") { bi.ContentTypeName = value; bi.IsFileAttach = true; } else if (key == "CONTENT-TRANSFER-ENCODING") { bi.ContentTransferEncoding = value; } else if (key == "CONTENT-DISPOSITION") { bi.ContentDisposition = value; } else if (key == "FILENAME") { bi.ContentDispositionAttachFileName = value; bi.IsFileAttach = true; } } } }
public POP3_BoundaryInfo ParseBoundaryHeader(string boundaryID, int headerStartRow) { string lastLine = ""; int row = 0; POP3_BoundaryInfo bi = new POP3_BoundaryInfo(); bi.BoundaryID = boundaryID; bi.HeaderStartPosition = headerStartRow; for (row = headerStartRow; row < m_MailMessageLines.Length; row++) { string line = m_MailMessageLines[row]; if (line == "") { break; } if (line.Length > 0 && line.Substring(0, 1) != "\t" && line.Substring(0, 1) != " " && line.IndexOf(":") > 0) { lastLine = line; } else { lastLine += line; } bi.BoundaryHeaderInfo.Add(lastLine); /// 헤더 정보 추가 ParseBoundaryHeaderType(bi, lastLine); /// 헤더 정보 분석 } bi.HeaderEndPosition = row; /// 첨부파일 여부 확인 if ((string.Compare(bi.ContentTypeMain, "image", true) == 0 || string.Compare(bi.ContentTypeMain, "application", true) == 0) && (bi.ContentDispositionAttachFileName.Length > 0 || bi.ContentTypeName.Length > 0)) { bi.IsFileAttach = true; } return(bi); }
private string[] GetBodyLines(POP3_BoundaryInfo bi) { return(GetBodyLines(bi.BodyStartPosition, bi.BodyEndPosition)); }
private int ParseBodyBoundary(string startBoundaryID, int startRow, bool isAlternative = false) { string boundaryID = startBoundaryID; int row = startRow; int resultRow = 0; for (; row < m_MailMessageLines.Length; row++) { resultRow = FindStringInRow(string.Format("--{0}", boundaryID), row); if (resultRow == -1) { resultRow = FindStringInRow(string.Format("--{0}--", boundaryID), row); return(resultRow); } POP3_BoundaryInfo bi = ParseBoundaryHeader(boundaryID, resultRow); BoundaryInfoList.Add(bi); resultRow = bi.HeaderEndPosition; /// child 여부 확인 if (bi.ChildBoundaryID == "") { resultRow = FindStringInRow("", resultRow, false); bi.BodyStartPosition = resultRow; for (; resultRow < m_MailMessageLines.Length; resultRow++) { if (bi.BodyEndPosition < 0) { if (m_MailMessageLines[resultRow].IndexOf("--") == 0 || m_MailMessageLines[resultRow] == "") { bi.BodyEndPosition = resultRow - 1; } } if (m_MailMessageLines[resultRow].IndexOf("--") == 0) { row = resultRow - 1; if (isAlternative == true) { return(row); } else { break; } } } } else { int parseCount = 0; for (; resultRow < m_MailMessageLines.Length; resultRow++) { if (m_MailMessageLines[resultRow] == string.Format("--{0}", bi.ChildBoundaryID)) { resultRow = ParseBodyBoundary(bi.ChildBoundaryID, resultRow, true); if ((string.Compare(bi.ContentTypeSub, "alternative", true) == 0) && (++parseCount < 2)) { continue; } row = resultRow; break; } } } } return(row); }
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); } }