internal Attachment[] GetAttachments(bool includeEmbedded) { IList <Attachment> attachmentList = new List <Attachment>(); if (embeddedMessage != null) { string attachmentName = null; string charset = null; if (embeddedMessage.ContentType != null) { Parameter nameParameter = embeddedMessage.ContentType.Parameters["name"]; if (nameParameter != null && nameParameter.Value != null) { attachmentName = nameParameter.Value; } Parameter charsetParameter = embeddedMessage.ContentType.Parameters["charset"]; if (charsetParameter != null) { charset = charsetParameter.Value; } } if (attachmentName == null && embeddedMessage.ContentDisposition != null) { Parameter filenameParameter = embeddedMessage.ContentDisposition.Parameters["filename"]; if (filenameParameter != null && filenameParameter.Value != null) { attachmentName = filenameParameter.Value; } } if (attachmentName == null) { attachmentName = embeddedMessage.Subject + ".eml"; } Encoding encoding = Util.GetEncoding(charset); byte[] attachmentBuffer = encoding.GetBytes(embeddedMessage.ToString()); Attachment attachment = new Attachment(attachmentBuffer, attachmentName); attachment.ContentID = this.ContentID; attachment.ContentLocation = this.ContentLocation; attachment.ContentDisposition = this.ContentDisposition; attachment.ContentDescription = this.ContentDescription; attachmentList.Add(attachment); } else if (ContentType != null && ContentType.Type != null && ContentType.Type.ToLower() == "message" && ContentType.SubType != null && ContentType.SubType.ToLower() == "rfc822") { string attachmentName = null; string charset = null; if (this.ContentType != null) { Parameter nameParameter = this.ContentType.Parameters["name"]; if (nameParameter != null && nameParameter.Value != null) { attachmentName = nameParameter.Value; } Parameter charsetParameter = this.ContentType.Parameters["charset"]; if (charsetParameter != null) { charset = charsetParameter.Value; } } if (attachmentName == null && this.ContentDisposition != null) { Parameter filenameParameter = this.ContentDisposition.Parameters["filename"]; if (filenameParameter != null && filenameParameter.Value != null) { attachmentName = filenameParameter.Value; } } Encoding encoding = Util.GetEncoding(charset); byte[] attachmentBuffer = encoding.GetBytes(this.ToString()); Attachment attachment = new Attachment(attachmentBuffer, attachmentName); attachment.ContentID = this.ContentID; attachment.ContentLocation = this.ContentLocation; attachment.ContentDisposition = this.ContentDisposition; attachment.ContentDescription = this.ContentDescription; attachmentList.Add(attachment); } else if (body != null && ContentType != null && ContentType.Type != null && ContentType.Type.ToLower() != "text") { string attachmentName = null; if (this.ContentType != null) { Parameter nameParameter = this.ContentType.Parameters["name"]; if (nameParameter != null && nameParameter.Value != null) { attachmentName = nameParameter.Value; } } if (attachmentName == null && this.ContentDisposition != null) { Parameter filenameParameter = this.ContentDisposition.Parameters["filename"]; if (filenameParameter != null && filenameParameter.Value != null) { attachmentName = filenameParameter.Value; } } if (this.ContentTransferEncoding == ContentTransferEncoding.Base64) { byte[] attachmentBuffer = Util.DecodeBase64Attachment(body); Attachment attachment = new Attachment(attachmentBuffer, attachmentName); attachment.ContentType = new ContentType(ContentType.Type, ContentType.SubType); attachment.ContentID = this.ContentID; attachment.ContentLocation = this.ContentLocation; attachment.ContentDisposition = this.ContentDisposition; attachment.ContentDescription = this.ContentDescription; attachmentList.Add(attachment); } else { byte[] attachmentBuffer = System.Text.Encoding.Default.GetBytes(body); Attachment attachment = new Attachment(attachmentBuffer, attachmentName); attachment.ContentType = new ContentType(ContentType.Type, ContentType.SubType); attachment.ContentID = this.ContentID; attachment.ContentLocation = this.ContentLocation; attachment.ContentDisposition = this.ContentDisposition; attachment.ContentDescription = this.ContentDescription; attachmentList.Add(attachment); } } else if (body != null && ContentType != null && ContentType.Type != null && ContentType.Type.ToLower() == "text" && ContentType.Parameters["name"] != null) { string attachmentName = null; if (this.ContentType != null) { Parameter nameParameter = this.ContentType.Parameters["name"]; if (nameParameter != null && nameParameter.Value != null) { attachmentName = nameParameter.Value; } } if (attachmentName == null && this.ContentDisposition != null) { Parameter filenameParameter = this.ContentDisposition.Parameters["filename"]; if (filenameParameter != null && filenameParameter.Value != null) { attachmentName = filenameParameter.Value; } } byte[] attachmentBuffer = System.Text.Encoding.Default.GetBytes(body); Attachment attachment = new Attachment(attachmentBuffer, attachmentName); attachment.ContentType = new ContentType(ContentType.Type, ContentType.SubType); attachment.ContentID = this.ContentID; attachment.ContentLocation = this.ContentLocation; attachment.ContentDisposition = this.ContentDisposition; attachment.ContentDescription = this.ContentDescription; attachmentList.Add(attachment); } else if (body != null && ContentDisposition != null && ContentDisposition.ToString().ToLower() != "inline") { string attachmentName = null; if (this.ContentType != null) { Parameter nameParameter = this.ContentType.Parameters["name"]; if (nameParameter != null && nameParameter.Value != null) { attachmentName = nameParameter.Value; } } if (attachmentName == null && this.ContentDisposition != null) { Parameter filenameParameter = this.ContentDisposition.Parameters["filename"]; if (filenameParameter != null && filenameParameter.Value != null) { attachmentName = filenameParameter.Value; } } if (this.ContentTransferEncoding == ContentTransferEncoding.Base64) { byte[] attachmentBuffer = Util.DecodeBase64Attachment(body); Attachment attachment = new Attachment(attachmentBuffer, attachmentName); attachment.ContentID = this.ContentID; attachment.ContentLocation = this.ContentLocation; attachment.ContentDisposition = this.ContentDisposition; attachment.ContentDescription = this.ContentDescription; attachmentList.Add(attachment); } else { byte[] attachmentBuffer = System.Text.Encoding.Default.GetBytes(body); Attachment attachment = new Attachment(attachmentBuffer, attachmentName); attachment.ContentID = this.ContentID; attachment.ContentLocation = this.ContentLocation; attachment.ContentDisposition = this.ContentDisposition; attachment.ContentDescription = this.ContentDescription; attachmentList.Add(attachment); } } else if (bodyParts.Count > 0) { for (int i = 0; i < bodyParts.Count; i++) { if (bodyParts[i] != null && (includeEmbedded || bodyParts[i].ContentID == null) && bodyParts[i].ContentType != null && ContentType.Type.ToLower() != "text") { Attachment[] bodyPartAttachments = bodyParts[i].GetAttachments(includeEmbedded); for (int j = 0; j < bodyPartAttachments.Length; j++) { attachmentList.Add(bodyPartAttachments[j]); } } } } Attachment[] attachments = new Attachment[attachmentList.Count]; for (int i = 0; i < attachmentList.Count; i++) { attachments[i] = attachmentList[i]; } return(attachments); }
private void ParseHeader(byte[] headerBuffer) { string headerAsciiString = System.Text.Encoding.Default.GetString(headerBuffer, 0, headerBuffer.Length); headerAsciiString = Util.Unfolding(headerAsciiString); string[] lines = headerAsciiString.Split(Util.CrlfSeparator); string currentHeaderName = null; string currentHeaderValue = null; for (int i = 0; i < lines.Length; i++) { if (lines[i].Length > 0) { if (!lines[i].StartsWith(" ") && !lines[i].StartsWith("\t")) { if (currentHeaderName != null && currentHeaderValue != null) { currentHeaderValue = currentHeaderValue.TrimStart(); currentHeaderValue = Util.DecodeHeader(currentHeaderValue, ref headerEncoding); if (Util.IsStandardKey(currentHeaderName)) { currentHeaderName = Util.GetCorrectedStandardKey(currentHeaderName); } Header header = new Header(currentHeaderName, currentHeaderValue); headers.Add(header); currentHeaderName = null; currentHeaderValue = null; } int colonIndex = lines[i].IndexOf(":"); if (colonIndex > -1) { currentHeaderName = lines[i].Substring(0, colonIndex); if (currentHeaderName.ToLower() == "content-disposition") { currentHeaderValue = lines[i].Substring(colonIndex + 1); //parse Content-Disposition ContentDisposition cd = new ContentDisposition(currentHeaderValue); currentHeaderValue = cd.ToString(); } else { currentHeaderValue = lines[i].Substring(colonIndex + 1); } } } else { lines[i] = lines[i].Replace("\t", " "); currentHeaderValue += lines[i]; } } } if (currentHeaderName != null && currentHeaderValue != null) { currentHeaderValue = currentHeaderValue.TrimStart(); currentHeaderValue = Util.DecodeHeader(currentHeaderValue, ref headerEncoding); if (Util.IsStandardKey(currentHeaderName)) { currentHeaderName = Util.GetCorrectedStandardKey(currentHeaderName); } Header header = new Header(currentHeaderName, currentHeaderValue); headers.Add(header); } }