public Attachment(string fileName, ContentType contentType) : base(fileName, contentType) { if ((contentType.Name == null) || (contentType.Name == string.Empty)) { this.Name = AttachmentBase.ShortNameFromFile(fileName); } else { this.Name = contentType.Name; } base.MimePart.ContentDisposition = new System.Net.Mime.ContentDisposition(); }
internal static void AddAttachment(StringBuilder builder, AttachmentBase view) { string contentType = "Content-Type: " + view.ContentType.MediaType; foreach (string key in view.ContentType.Parameters.Keys) { contentType = contentType + "; " + key + "=" + view.ContentType.Parameters[key]; } builder.AppendLine(contentType); builder.AppendLine("Content-Transfer-Encoding: base64"); if (!string.IsNullOrEmpty(view.ContentId)) { builder.AppendLine("Content-Id: <" + view.ContentId + ">"); } if (view is Attachment) { builder.AppendLine("Content-Disposition: attachment"); } builder.AppendLine(); using (MemoryStream memstream = new MemoryStream()) { int bytesRead; byte[] buffer = new byte[4096]; while ((bytesRead = view.ContentStream.Read(buffer, 0, buffer.Length)) > 0) { memstream.Write(buffer, 0, bytesRead); } string str = Convert.ToBase64String(memstream.ToArray()); StringReader reader = new StringReader(str); char[] line = new char[76]; int read; while ((read = reader.Read(line, 0, line.Length)) > 0) { builder.AppendLine(new string(line, 0, read)); } } if (view.ContentStream.CanSeek) { view.ContentStream.Seek(0, SeekOrigin.Begin); } }
private void StartSection (string section, ContentType sectionContentType, AttachmentBase att) { SendData (String.Format ("--{0}", section)); SendHeader ("content-type", sectionContentType.ToString ()); SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding)); if (!string.IsNullOrEmpty (att.ContentId)) SendHeader("content-ID", "<" + att.ContentId + ">"); SendData (string.Empty); }
static MimePart GetMimePart (AttachmentBase item) { var mimeType = item.ContentType.ToString (); var part = new MimePart (ContentType.Parse (mimeType)); var attachment = item as Attachment; if (attachment != null) { var disposition = attachment.ContentDisposition.ToString (); part.ContentDisposition = ContentDisposition.Parse (disposition); } switch (item.TransferEncoding) { case System.Net.Mime.TransferEncoding.QuotedPrintable: part.ContentTransferEncoding = ContentEncoding.QuotedPrintable; break; case System.Net.Mime.TransferEncoding.Base64: part.ContentTransferEncoding = ContentEncoding.Base64; break; case System.Net.Mime.TransferEncoding.SevenBit: part.ContentTransferEncoding = ContentEncoding.SevenBit; break; } if (item.ContentId != null) part.ContentId = item.ContentId; var stream = new MemoryBlockStream (); item.ContentStream.CopyTo (stream); stream.Position = 0; part.ContentObject = new ContentObject (stream); return part; }
protected static Encoding resolveViewEncoding(AttachmentBase view, Encoding fallbackEncoding) { String charSet = view.ContentType.CharSet; try { return Encoding.GetEncoding(charSet); } catch { return fallbackEncoding; } }
protected static string GetStringFromView(AttachmentBase view) { Encoding encoding = resolveViewEncoding(view, Encoding.ASCII); var data = new byte[view.ContentStream.Length]; view.ContentStream.Read(data, 0, data.Length); return encoding.GetString(data); }
private static string GetStringFromView(AttachmentBase view) { var data = new byte[view.ContentStream.Length]; view.ContentStream.Read(data, 0, data.Length); return Encoding.ASCII.GetString(data); }
private static byte[] getAttachmentBytes(AttachmentBase attachment) { MemoryStream actual = new MemoryStream(); Stream stream = attachment.ContentStream; while (true) { int outByte = stream.ReadByte(); if (outByte != -1) actual.Write(new[] { (byte)outByte }, 0, 1); else break; } return actual.ToArray(); }
/// <summary> /// Creates a MIME body part from an entry of the AlternateView or /// Attachments collection of a MailMessage instance and appends it /// to the specified Stringbuilder instance. /// </summary> /// <param name="builder">The Stringbuilder instance to append the /// body part to.</param> /// <param name="view">An entry from either the AlternateView or the /// Attachments collection of a MailMessage instance.</param> static void AddAttachment(StringBuilder builder, AttachmentBase view) { // Append the MIME headers for this body part string contentType = "Content-Type: " + view.ContentType.MediaType; foreach (string key in view.ContentType.Parameters.Keys) { contentType = contentType + "; " + key + "=" + view.ContentType.Parameters[key]; } builder.AppendLine(contentType); builder.AppendLine("Content-Transfer-Encoding: base64"); if (!String.IsNullOrEmpty(view.ContentId)) builder.AppendLine("Content-Id: <" + view.ContentId + ">"); if (view is Attachment) builder.AppendLine("Content-Disposition: attachment"); builder.AppendLine(); // Append the actual body part contents encoded as Base64 using (MemoryStream memstream = new MemoryStream()) { int bytesRead; byte[] buffer = new byte[4096]; while ((bytesRead = view.ContentStream.Read(buffer, 0, buffer.Length)) > 0) { memstream.Write(buffer, 0, bytesRead); } string str = Convert.ToBase64String(memstream.ToArray()); foreach (string chunk in str.ToChunks(76)) builder.AppendLine(chunk); } // Rewind the stream if it supports seeking if (view.ContentStream.CanSeek) view.ContentStream.Seek(0, SeekOrigin.Begin); }
/// <summary> /// Creates a MIME body part from an entry of the AlternateView or /// Attachments collection of a MailMessage instance and appends it /// to the specified Stringbuilder instance. /// </summary> /// <param name="builder"> /// The Stringbuilder instance to append the /// body part to. /// </param> /// <param name="view"> /// An entry from either the AlternateView or the /// Attachments collection of a MailMessage instance. /// </param> private static void AddAttachment(StringBuilder builder, AttachmentBase view) { // Append the MIME headers for this body part var contentType = "Content-Type: " + view.ContentType.MediaType; foreach (string key in view.ContentType.Parameters.Keys) { contentType = contentType + "; " + key + "=" + view.ContentType.Parameters[key]; } builder.AppendLine(contentType); builder.AppendLine("Content-Transfer-Encoding: base64"); if (!string.IsNullOrEmpty(view.ContentId)) builder.AppendLine("Content-Id: <" + view.ContentId + ">"); if (view is Attachment) builder.AppendLine($"Content-Disposition: attachment; filename=\"{((Attachment) view).Name}\""); builder.AppendLine(); // Append the actual body part contents encoded as Base64 using (var memstream = new MemoryStream()) { int bytesRead; var buffer = new byte[4096]; while ((bytesRead = view.ContentStream.Read(buffer, 0, buffer.Length)) > 0) { memstream.Write(buffer, 0, bytesRead); } var str = Convert.ToBase64String(memstream.ToArray()); var reader = new StringReader(str); var line = new char[76]; int read; while ((read = reader.Read(line, 0, line.Length)) > 0) builder.AppendLine(new string(line, 0, read)); } // Rewind the stream if it supports seeking if (view.ContentStream.CanSeek) view.ContentStream.Seek(0, SeekOrigin.Begin); }
static MimePart GetMimePart (AttachmentBase item) { var mimeType = item.ContentType.ToString (); var contentType = ContentType.Parse (mimeType); var attachment = item as Attachment; MimePart part; if (contentType.MediaType.Equals ("text", StringComparison.OrdinalIgnoreCase)) part = new TextPart (contentType); else part = new MimePart (contentType); if (attachment != null) { var disposition = attachment.ContentDisposition.ToString (); part.ContentDisposition = ContentDisposition.Parse (disposition); } switch (item.TransferEncoding) { case System.Net.Mime.TransferEncoding.QuotedPrintable: part.ContentTransferEncoding = ContentEncoding.QuotedPrintable; break; case System.Net.Mime.TransferEncoding.Base64: part.ContentTransferEncoding = ContentEncoding.Base64; break; case System.Net.Mime.TransferEncoding.SevenBit: part.ContentTransferEncoding = ContentEncoding.SevenBit; break; //case System.Net.Mime.TransferEncoding.EightBit: // part.ContentTransferEncoding = ContentEncoding.EightBit; // break; } if (item.ContentId != null) part.ContentId = item.ContentId; var stream = new MemoryBlockStream (); item.ContentStream.CopyTo (stream); stream.Position = 0; part.ContentObject = new ContentObject (stream); return part; }
public Attachment(string fileName, string mediaType) : base(fileName, mediaType) { this.Name = AttachmentBase.ShortNameFromFile(fileName); base.MimePart.ContentDisposition = new System.Net.Mime.ContentDisposition(); }
private static void DumpAttachment( string dir, AttachmentBase attachment ) { string name = null; if( !string.IsNullOrEmpty( attachment.ContentType.Name ) ) name = attachment.ContentType.Name; else { name = attachment is AlternateView ? "alternate-view" : "attachment"; string ext = MimeUtility.GetFileExtensionFromMediaType( attachment.ContentType.MediaType ); if( ext == "eml" ) name = "attached-message"; name = string.Format( name + "." + ext ); } int i = 1; string shortname = null; while( File.Exists( dir + Path.DirectorySeparatorChar + name ) ) { FileInfo fi = new FileInfo( name ); if( null == shortname ) shortname = fi.Name.Substring( 0, fi.Name.Length - fi.Extension.Length ); name = string.Format( "{0} ({1}){2}", shortname, i, fi.Extension ); i++; } using( Stream stream = File.Create( dir + Path.DirectorySeparatorChar + name ) ) { byte[] buffer = new byte[32768]; while( true ) { int read = attachment.ContentStream.Read( buffer, 0, buffer.Length ); if( read <= 0 ) break; stream.Write( buffer, 0, read ); } } }