private bool AttachmentContentDisposition(MailProperties partProps) { string contentDisposition = partProps.GetKeyPrincipal("Content-Disposition"); if (string.Compare(contentDisposition, "attachment", true) == 0) { return(true); } string transferEncoding = partProps.GetKeyPrincipal("Content-Transfer-Encoding"); if ((string.Compare(contentDisposition, "inline", true) == 0) && (string.Compare(transferEncoding, "base64", true) == 0)) { return(true); } return(false); }
private static string GetAttachmentContentId(MailProperties partProps) { string cid = partProps.GetKeyPrincipal("Content-ID"); if (!string.IsNullOrEmpty(cid) && cid.StartsWith("<") && cid.EndsWith(">")) { cid = cid.Substring(1, cid.Length - 2); } return(cid); }
private void ReadBody(MailReader reader, MailProperties partProps, String separator) { string contentType = partProps.GetKeyPrincipal("Content-Type"); bool isTextPlain = (string.Compare(contentType, "text/plain", true) == 0); bool isTextHtml = (string.Compare(contentType, "text/html", true) == 0); bool isAttachment = (!isTextPlain && !isTextHtml) || AttachmentContentDisposition(partProps); string oldSeparator = reader.GetSeparator(); reader.SetSeparator(separator); reader.SetTextPlain(isTextPlain); GXLogging.Debug(log, "isAttachment:" + isAttachment + " isTextHTML:" + isTextHtml + " istextplain:" + isTextPlain); MimeDecoder decoder = GetDecoder(partProps.GetField("Content-Transfer-Encoding")); if (isAttachment) { string outname = string.Empty; try { Stream output; if (this.DownloadAttachments) { string cid = GetAttachmentContentId(partProps); string fileName = qpDecoder.DecodeHeader(RemoveQuotes(partProps.GetKeyProperty("Content-Disposition", "filename"))); if (string.IsNullOrEmpty(fileName)) { fileName = RemoveQuotes(partProps.GetKeyProperty("Content-Type", "name")); } bool inlineAttachment = partProps.GetKeyPrincipal("Content-Disposition").StartsWith("inline"); if (inlineAttachment && !string.IsNullOrEmpty(cid)) { fileName = String.Format("{0}_{1}", cid, fileName); outname = GetFileName(attachmentsPath, fileName, contentType); this.messageHtml = this.messageHtml.Replace("cid:" + cid, outname); } else { outname = GetFileName(attachmentsPath, fileName, contentType); } attachments += outname + ";"; if (!Directory.Exists(attachmentsPath)) { Directory.CreateDirectory(attachmentsPath); } output = new FileStream(attachmentsPath + outname, FileMode.Create); } else { output = new DummyStream(); } try { decoder.DecodeFile(reader, output, runner); } catch (Exception e) { GXLogging.Error(log, "ReadBody error", e); throw e; } finally { output.Close(); } } catch (DirectoryNotFoundException dex) { GXLogging.Error(log, "Error with attachments, attachment path:" + attachmentsPath + outname, dex); throw new CouldNotSaveAttachmentException(dex); } catch (Exception ex) { if (ex.InnerException != null) { ex = ex.InnerException; } GXLogging.Error(log, "Error with attachments, attachment path:" + attachmentsPath + outname, ex); throw new InvalidAttachmentException(ex); } } else { try { TextWriter output = new StringWriter(); try { decoder.DecodeText(reader, output, runner); if (isTextPlain) { messageText.Append(((StringWriter)output).GetStringBuilder().ToString()); } else if (isTextHtml) { messageHtml.Append(((StringWriter)output).GetStringBuilder().ToString()); } } catch (Exception e) { GXLogging.Error(log, "ReadBody error", e); throw e; } finally { output.Close(); } } catch (Exception ex1) { if (ex1.InnerException != null) { ex1 = ex1.InnerException; } GXLogging.Error(log, "ReadBody error", ex1); throw new InvalidMessageException(ex1); } } reader.SetSeparator(oldSeparator); }