Exemple #1
0
        private CommunicationPoolEntity GetMailContent(Mail_Message message, int messageNumber)
        {
            CommunicationPoolEntity mail = null;

            if (message != null)
            {
                string displayName   = message.From != null ? message.From[0].DisplayName.NullSafeTrim() : "-";
                string senderAddress = message.From != null ? message.From[0].Address.NullSafeTrim() : "-";
                string senderName    = !string.IsNullOrWhiteSpace(displayName) ? displayName : ApplicationHelpers.GetSenderAddress(senderAddress);
                string recvDate      = message.Date == DateTime.MinValue ? "date not specified" : message.Date.FormatDateTime(Constants.DateTimeFormat.DefaultFullDateTime);

                Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Content").Add("Subject", message.Subject).Add("From", senderAddress)
                            .Add("SenderName", senderName).Add("MessageId", message.MessageID).Add("MessageNumber", messageNumber)
                            .Add("RecvDateTime", recvDate).ToInputLogString());

                mail = new CommunicationPoolEntity();
                mail.SenderAddress = senderAddress;
                mail.SenderName    = senderName;
                mail.Subject       = ApplicationHelpers.StringLimit(message.Subject.RemoveExtraSpaces(), Constants.MaxLength.MailSubject);
                mail.Content       = ApplicationHelpers.StripHtmlTags(GetMailMessage(message));
                mail.PlainText     = ApplicationHelpers.RemoveHtmlTags(GetMailMessage(message).ReplaceBreaks());
                mail.MessageNumber = messageNumber;
                mail.RecvDateTime  = message.Date;

                MIME_Entity[] list = message.GetAttachments(true, true);

                if (list != null && list.Length > 0)
                {
                    Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Attachments").Add("ListSize", list.Length).ToOutputLogString());

                    foreach (MIME_Entity entity in list)
                    {
                        try
                        {
                            if (entity != null)
                            {
                                string fileName;
                                string dispoType;
                                string contentId   = entity.ContentID.ExtractContentID();
                                string contentType = entity.ContentType.TypeWithSubtype.ToLowerInvariant();

                                if (entity.ContentDisposition != null)
                                {
                                    fileName  = entity.ContentDisposition.Param_FileName;
                                    dispoType = entity.ContentDisposition.DispositionType;

                                    // Retrieve file name from contentType instead.
                                    if (string.IsNullOrWhiteSpace(fileName))
                                    {
                                        fileName = entity.ContentType.Param_Name;
                                    }

                                    Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Attachment").Add("ContentDisposition", "NOT NULL").Add("ContentId", contentId)
                                                .Add("FileName", fileName).Add("ContentType", contentType).Add("DispositionType", dispoType).ToInputLogString());

                                    if (entity.Body != null && entity.Body is MIME_b_SinglepartBase)
                                    {
                                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Attachment").ToInputLogString());
                                        byte[] bytes = ((MIME_b_SinglepartBase)entity.Body).Data;

                                        if (bytes != null && bytes.Length > 0)
                                        {
                                            if ((string.IsNullOrWhiteSpace(contentId) || MIME_DispositionTypes.Attachment.Equals(dispoType)) && !string.IsNullOrWhiteSpace(fileName))
                                            {
                                                var attachment = new AttachmentEntity();
                                                attachment.ContentType = contentType;
                                                attachment.ByteArray   = bytes;
                                                attachment.Filename    = fileName;
                                                mail.Attachments.Add(attachment);
                                            }
                                            if (!string.IsNullOrWhiteSpace(contentId) && MIME_DispositionTypes.Inline.Equals(dispoType) && contentType.IsImage())
                                            {
                                                // data:image/png;base64,{0}
                                                string base64String = ApplicationHelpers.GetBase64Image(bytes);
                                                string newImg       = string.Format(CultureInfo.InvariantCulture, "data:{0};base64,{1}", contentType, base64String);
                                                string srcImg       = string.Format(CultureInfo.InvariantCulture, "cid:{0}", contentId);
                                                mail.Content = mail.Content.Replace(srcImg, newImg);
                                                Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Attachment").Add("NewImg", newImg).Add("SrcImg", srcImg).ToOutputLogString());
                                            }
                                        }

                                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Attachment").ToSuccessLogString());
                                    }
                                }
                                else
                                {
                                    Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Attachment").Add("ContentDisposition", "NULL").Add("ContentId", contentId)
                                                .Add("ContentType", contentType).ToInputLogString());

                                    if (entity.Body != null && entity.Body is MIME_b_SinglepartBase)
                                    {
                                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Attachment").ToInputLogString());
                                        byte[] bytes = ((MIME_b_SinglepartBase)entity.Body).Data;

                                        if (bytes != null && bytes.Length > 0)
                                        {
                                            if (!string.IsNullOrWhiteSpace(contentId) && contentType.IsImage())
                                            {
                                                // data:image/png;base64,{0}
                                                string base64String = ApplicationHelpers.GetBase64Image(bytes);
                                                string newImg       = string.Format(CultureInfo.InvariantCulture, "data:{0};base64,{1}", contentType, base64String);
                                                string srcImg       = string.Format(CultureInfo.InvariantCulture, "cid:{0}", contentId);
                                                mail.Content = mail.Content.Replace(srcImg, newImg);
                                                Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Attachment").Add("NewImg", newImg).Add("SrcImg", srcImg).ToOutputLogString());
                                            }
                                        }
                                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Attachment").ToSuccessLogString());
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            mail.IsDeleted = false;
                            Logger.Error("Exception occur:\n", ex);
                            Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Content").Add("Error Message", ex.Message).ToFailLogString());
                        }
                    }
                }
                else
                {
                    Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Attachments").Add("ListSize", "0").ToOutputLogString());
                }

                Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Content").Add("Content", mail.Content).ToSuccessLogString());
            }
            else
            {
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Get Mail Content").Add("Error Message", "Mail Content is null").ToFailLogString());
            }

            return(mail);
        }