public static MIME_Entity CreateAttachment(Stream stream, string fileName) { if (stream == null) { throw new ArgumentNullException("stream"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } long fileSize = stream.CanSeek ? (stream.Length - stream.Position) : -1; MIME_Entity retVal = new MIME_Entity(); MIME_b_Application body = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); retVal.Body = body; body.SetData(stream, MIME_TransferEncodings.Base64); retVal.ContentType.Param_Name = Path.GetFileName(fileName); MIME_h_ContentDisposition disposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Attachment); disposition.Param_FileName = Path.GetFileName(fileName); disposition.Param_Size = fileSize; //disposition.Param_CreationDate = fileInfo.CreationTime; //disposition.Param_ModificationDate = fileInfo.LastWriteTime; //disposition.Param_ReadDate = fileInfo.LastAccessTime; retVal.ContentDisposition = disposition; return(retVal); }
/// <summary> /// Parses body from the specified stream /// </summary> /// <param name="owner">Owner MIME entity.</param> /// <param name="defaultContentType">Default content-type for this body.</param> /// <param name="stream">Stream from where to read body.</param> /// <returns>Returns parsed body.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b>, <b>defaultContentType</b> or <b>strean</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when any parsing errors.</exception> protected static new MIME_b Parse(MIME_Entity owner, MIME_h_ContentType defaultContentType, SmartStream stream) { if (owner == null) { throw new ArgumentNullException("owner"); } if (defaultContentType == null) { throw new ArgumentNullException("defaultContentType"); } if (stream == null) { throw new ArgumentNullException("stream"); } MIME_b_Application retVal = null; if (owner.ContentType != null) { retVal = new MIME_b_Application(owner.ContentType.TypeWithSubtype); } else { retVal = new MIME_b_Application(defaultContentType.TypeWithSubtype); } Net_Utils.StreamCopy(stream, retVal.EncodedStream, stream.LineBufferSize); return(retVal); }
public static MIME_Entity CreateAttachment(string file) { if (file == null) { throw new ArgumentNullException("file"); } MIME_Entity retVal = new MIME_Entity(); MIME_b_Application body = new MIME_b_Application(MIME_MediaTypes.Application.octet_stream); retVal.Body = body; body.SetDataFromFile(file, MIME_TransferEncodings.Base64); retVal.ContentType.Param_Name = Path.GetFileName(file); FileInfo fileInfo = new FileInfo(file); MIME_h_ContentDisposition disposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Attachment); disposition.Param_FileName = Path.GetFileName(file); disposition.Param_Size = fileInfo.Length; disposition.Param_CreationDate = fileInfo.CreationTime; disposition.Param_ModificationDate = fileInfo.LastWriteTime; disposition.Param_ReadDate = fileInfo.LastAccessTime; retVal.ContentDisposition = disposition; return(retVal); }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <param name="headerReencode">If true always specified encoding is used for header. If false and header field value not modified, /// original encoding is kept.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> internal protected override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset, bool headerReencode) { // We have signer certificate, sign this entity. if (this.BodyParts.Count > 0 && m_pSignerCert != null) { // Remove old signature if there is any. if (this.BodyParts.Count > 1) { this.BodyParts.Remove(1); } // Store entity to tmp stream. MemoryStream tmpDataEntityStream = new MemoryStream(); this.BodyParts[0].ToStream(tmpDataEntityStream, null, null, false); // Compute PKCS #7 message. SignedCms signedCms = new SignedCms(new ContentInfo(tmpDataEntityStream.ToArray()), true); signedCms.ComputeSignature(new CmsSigner(m_pSignerCert)); byte[] pkcs7 = signedCms.Encode(); // Create PKCS 7 entity. MIME_Entity entity_application_pkcs7 = new MIME_Entity(); MIME_b_Application application_pkcs7 = new MIME_b_Application(MIME_MediaTypes.Application.x_pkcs7_signature); entity_application_pkcs7.Body = application_pkcs7; application_pkcs7.SetData(new MemoryStream(pkcs7), MIME_TransferEncodings.Base64); entity_application_pkcs7.ContentType.Param_Name = "smime.p7s"; entity_application_pkcs7.ContentDescription = "S/MIME Cryptographic Signature"; this.BodyParts.Add(entity_application_pkcs7); signedCms.Decode(application_pkcs7.Data); signedCms.CheckSignature(true); } base.ToStream(stream, headerWordEncoder, headerParmetersCharset, headerReencode); }