コード例 #1
0
        // 重組郵件內容及其附件(原始郵件)
        private bool BuildUpEmailBodyAndAttachmentsFromRawHeader(EEmailRecord erecord, ref string strBody, ref List<SelectListItem> attachments, ref string attachlist, ref string errMsg)
        {
            bool bRet = true;
            try
            {
                EEmailRawHeader erawheader = erecord.EmailRawHeader;
                if (erawheader == null)
                {
                    // 不正常
                    errMsg = String.Format("RawHeader Not Found [ERID={0}]", erecord.Id);
                    return false;
                }

                if (erawheader.EmailRawBody.Count() < 1)
                {
                    // 不正常
                    errMsg = String.Format("RawBody Not Found [RawID={0}]", erawheader.Id);
                    return false;
                }

                string curpath = Server.MapPath("~");
                if (curpath[curpath.Length - 1] == '\\')
                    curpath = curpath.Substring(0, curpath.Length - 1);

                string mailbox = String.Format("{0}\\inbox\\{1}", curpath, erecord.RID);

                List<SelectListItem> cids = new List<SelectListItem>();
                attachments.Clear();
                attachlist = "";
                string strCnt = "", strCntTmp = "";

                foreach (var body in erawheader.EmailRawBody.OrderBy(rb => rb.PlaceHolder))
                {
                    // 郵件內容區塊
                    if (body.FileName.Length < 1)
                    {
                        // 內容區塊為純文字編碼
                        if (body.CntMedia.IndexOf("plain") >= 0)
                        {
                            // 取不到HTML編碼方式的內容區塊時, 則取出純文字內容
                            if (strCntTmp.Length > 0)
                                strCnt += strCntTmp;

                            // 取出內容 (字串形式)
                            strCntTmp = System.Text.Encoding.GetEncoding(body.CodePage).GetString(body.BinaryCnt);
                            strCntTmp = strCntTmp.Replace("\r\n", "<br />").Replace("\n", "<br />");
                            continue;
                        }

                        // 取出內容 (HTML形式)
                        strCnt += System.Text.Encoding.GetEncoding(body.CodePage).GetString(body.BinaryCnt);
                        strCntTmp = "";
                    }

                    // 郵件檔案區塊
                    else
                    {
                        // 郵件附檔
                        if (!body.FileName.Contains('|'))
                        {
                            attachlist += "|" + body.Id.ToString() + ":" + body.FileName;
                            attachments.Add(new SelectListItem() { Text = body.FileName, Value = body.Id.ToString() });
                        }
                        // 郵件中內嵌資訊(圖片或檔案)
                        else
                        {
                            string[] cid_filename = body.FileName.Split('|');

                            // 內嵌資訊不是圖片
                            if (!body.CntMedia.Contains("image"))
                            {
                                attachlist += "|" + body.Id.ToString() + ":" + cid_filename[1];
                                attachments.Add(new SelectListItem() { Text = cid_filename[1], Value = body.Id.ToString() });
                                continue;
                            }

                            // 先預備存放郵件圖片的資料夾
                            if (!Directory.Exists(mailbox))
                            {
                                Directory.CreateDirectory(mailbox);
                            }

                            // 原始內嵌圖片沒有檔案名稱, 系統取亂數作為檔案名稱
                            if (cid_filename[1].Contains("(no name)"))
                            {
                                Random RGen = new Random();
                                string fname = RGen.Next(100000, 9999999).ToString();
                                cid_filename[1] = fname + "." + body.CntMedia.Substring(body.CntMedia.IndexOf("/") + 1);
                            }
                            if (!cid_filename[1].Contains("."))
                            {
                                cid_filename[1] += "." + body.CntMedia.Substring(body.CntMedia.IndexOf("/") + 1);
                            }

                            // 圖檔名稱包含路徑
                            if (cid_filename[1].IndexOf('\\') != -1)
                            {
                                cid_filename[1] = cid_filename[1].Substring(cid_filename[1].LastIndexOf('\\') + 1);
                            }

                            // 圖檔名稱包含非法字元
                            foreach (char c in System.IO.Path.GetInvalidFileNameChars())
                            {
                                cid_filename[1] = cid_filename[1].Replace(c, '_');
                            }

                            // 暫存圖片檔案名稱格式: RawHeaderID_CID_FileName
                            string attname = String.Format("{0}_{1}_{2}", body.RawHeaderId, cid_filename[0].Replace(':', '-'), cid_filename[1]);

                            if (!System.IO.File.Exists(String.Format("{0}\\{1}", mailbox, attname)))
                            {
                                BinaryWriter writer = new BinaryWriter(new FileStream(String.Format("{0}\\{1}", mailbox, attname), FileMode.CreateNew, FileAccess.Write, FileShare.None));
                                writer.Write(body.BinaryCnt);
                                writer.Close();
                            }
                            cids.Add(new SelectListItem() { Text = cid_filename[0], Value = Url.Content(String.Format("~/inbox/{0}/{1}", erecord.RID, attname.Replace("\\", "/"))) });
                        }
                    }
                }
                if (strCntTmp.Length > 0)
                    strCnt += strCntTmp;

                strBody = strCnt;

                // 將郵件內容中的圖片, 取代成系統暫存位置
                foreach (SelectListItem cidatt in cids)
                {
                    strBody = strBody.Replace("cid:" + cidatt.Text, cidatt.Value);
                }

            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                attachments.Clear();
                bRet = false;
            }
            return bRet;
        }
コード例 #2
0
        // 重組郵件內容及其附件(草稿)
        private bool BuildUpEmailBodyAndAttachmentsFromDraft(EEmailRecord erecord, ref string strBody, ref List<SelectListItem> attachments, ref string attachlist, ref string errMsg)
        {
            bool bRet = true;
            try
            {
                EEmailSavedBody esavedbody = erecord.EmailSavedBody;
                if (esavedbody == null)
                {
                    // 不正常
                    errMsg = String.Format("SavedBody Not Found [ERID={0}]", erecord.Id);
                    return false;
                }

                List<SelectListItem> cids = new List<SelectListItem>();
                attachments.Clear();
                attachlist = "";

                if (esavedbody.BodyCnt != null)
                    strBody = System.Text.Encoding.GetEncoding(esavedbody.CodePage.Value).GetString(esavedbody.BodyCnt);

                var esavedattach = db.EmailSavedAttachment.Where(a => a.EmailRecordId == erecord.Id);
                foreach (var attach in esavedattach)
                {
                    attachlist += "|" + attach.Id.ToString() + ":" + attach.FileName;
                    attachments.Add(new SelectListItem() { Text = attach.FileName, Value = attach.Id.ToString() });
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                attachments.Clear();
                bRet = false;
            }
            return bRet;
        }