/// <summary> /// Creates attachment entity. /// </summary> /// <param name="stream">Attachment data stream. Data is read from stream current position.</param> /// <param name="fileName">File name.</param> /// <returns>Returns created attachment entity.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> or <b>fileName</b> is null reference.</exception> 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 header field from the specified value. /// </summary> /// <param name="value">Header field value. Header field name must be included. For example: 'Content-Type: text/plain'.</param> /// <returns>Returns parsed header field.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>value</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when header field parsing errors.</exception> public static MIME_h_ContentDisposition Parse(string value) { if (value == null) { throw new ArgumentNullException("value"); } // We should not have encoded words here, but some email clients do this, so encoded them if any. string valueDecoded = MIME_Encoding_EncodedWord.DecodeS(value); MIME_h_ContentDisposition retVal = new MIME_h_ContentDisposition(); string[] name_value = valueDecoded.Split(new char[] { ':' }, 2); if (name_value.Length != 2) { throw new ParseException("Invalid Content-Type: header field value '" + value + "'."); } MIME_Reader r = new MIME_Reader(name_value[1]); string type = r.Token(); if (type == null) { throw new ParseException("Invalid Content-Disposition: header field value '" + value + "'."); } retVal.m_DispositionType = type.Trim(); retVal.m_pParameters.Parse(r); retVal.m_ParseValue = value; return(retVal); }
/// <summary> /// Creates attachment entity. /// </summary> /// <param name="file">File name with optional path.</param> /// <returns>Returns created attachment entity.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null reference.</exception> 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); }