Esempio n. 1
0
        /// <summary>
        /// Decodes the attachment data from a mime encoded type.
        /// </summary>
        /// <param name="writeAttachmentToFile">Write the attachment indicator.</param>
        private void DecodeData(bool writeAttachmentToFile)
        {
            // If the data is an attachemnt.
            if (_contentDisposition != null)
            {
                // If the attachment is encoded as BASE-64
                // then decode the data from base 64.
                if ((_contentDisposition.Equals("attachment;")) &&
                    (_contentTransferEncoding.ToUpper().Equals("BASE64")))
                {
                    // Convert the attachment data to a byte array
                    // from the base 64 encoding. Assign the decoded
                    // data to the byte array of the raw file.
                    _binaryData = Convert.FromBase64String(_data.Replace("\n", ""));
                    _file       = _binaryData;
                    _decoded    = true;

                    // If write the attachment set then
                    // directly write the attachment
                    // to the specified file.
                    if (writeAttachmentToFile)
                    {
                        EmailMessageParse.WriteAttachment(_binaryData,
                                                          _connection.AttachmentDirectory, _filename);
                    }
                }
                // If the attachment is encoded as QUOTED-PRINTABLE
                // then decode the data from quoted printable.
                else if ((_contentDisposition.Equals("attachment;")) &&
                         (_contentTransferEncoding.ToUpper().Equals("QUOTED-PRINTABLE")))
                {
                    // Convert the attachment data to a byte array
                    // from the quoted printable encoding. Assign the decoded
                    // data to the byte array of the raw file.
                    string output = EmailMessageParse.FromQuotedPrintable(_data);
                    _file    = Encoding.ASCII.GetBytes(output);
                    _decoded = true;

                    // If write the attachment set then
                    // directly write the attachment
                    // to the specified file.
                    if (writeAttachmentToFile)
                    {
                        EmailMessageParse.WriteQuotedPrintable(output,
                                                               _connection.AttachmentDirectory, _filename);
                    }
                }
                // If the attachment has been encoded with
                // some other transfer encoding then assign
                // the raw encoded data.
                else if (_contentDisposition.Equals("attachment;"))
                {
                    _file    = Encoding.ASCII.GetBytes(_data);
                    _decoded = false;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Write the attachment to the file path.
        /// </summary>
        /// <param name="directoryPath">The directory path to write the file.</param>
        /// <param name="fileName">The name of the file to write.</param>
        /// <returns>True if the attachment was written else false.</returns>
        public bool WriteAttachment(string directoryPath, string fileName)
        {
            try
            {
                // Write the current attachment
                // to the specified file.
                EmailMessageParse.WriteAttachment(_file,
                                                  directoryPath, fileName);

                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }