/// <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>mediaTypedefaultContentTypeb></b> or <b>stream</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");
            }
            if (owner.ContentType == null || owner.ContentType.Param_Boundary == null)
            {
                throw new ParseException("Multipart entity has not required 'boundary' paramter.");
            }

            MIME_b_MultipartSigned retVal = new MIME_b_MultipartSigned(owner.ContentType);

            ParseInternal(owner, owner.ContentType.TypeWithSubtype, stream, retVal);

            return(retVal);
        }
Exemple #2
0
        // TODO:
        //public MIME_Entity GetEntityByPartsSpecifier(string partsSpecifier)

        #region method ConvertToMultipartSigned

        /// <summary>
        /// Converts message to multipart/signed message.
        /// </summary>
        /// <param name="signerCert">>Signer certificate</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>signerCert</b> is null reference.</exception>
        /// <exception cref="InvalidOperationException">Is raised when this method is called for already signed message.</exception>
        public void ConvertToMultipartSigned(X509Certificate2 signerCert)
        {
#if NETSTANDARD
            // TODO check back after netstandard 2.0
            throw new NotImplementedException("MIME_b_MultipartSigned unsupported currently");
#else
            if (signerCert == null)
            {
                throw new ArgumentNullException("signerCert");
            }
            if (this.IsSigned)
            {
                throw new InvalidOperationException("Message is already signed.");
            }

            MIME_Entity msgEntity = new MIME_Entity();
            msgEntity.Body = this.Body;
            msgEntity.ContentDisposition      = this.ContentDisposition;
            msgEntity.ContentTransferEncoding = this.ContentTransferEncoding;
            this.ContentTransferEncoding      = null;

            MIME_b_MultipartSigned multipartSigned = new MIME_b_MultipartSigned();
            this.Body = multipartSigned;
            multipartSigned.SetCertificate(signerCert);
            multipartSigned.BodyParts.Add(msgEntity);
#endif
        }
        /// <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>mediaTypedefaultContentTypeb></b> or <b>stream</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");
            }
            if(owner.ContentType == null || owner.ContentType.Param_Boundary == null){
                throw new ParseException("Multipart entity has not required 'boundary' paramter.");
            }
            
            MIME_b_MultipartSigned retVal = new MIME_b_MultipartSigned(owner.ContentType);
            ParseInternal(owner,owner.ContentType.TypeWithSubtype,stream,retVal);

            return retVal;
        }
        // TODO:
        //public MIME_Entity GetEntityByPartsSpecifier(string partsSpecifier)


        /// <summary>
        /// Converts message to multipart/signed message.
        /// </summary>
        /// <param name="signerCert">>Signer certificate</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>signerCert</b> is null reference.</exception>
        /// <exception cref="InvalidOperationException">Is raised when this method is called for already signed message.</exception>
        public void ConvertToMultipartSigned(X509Certificate2 signerCert)
        {
            if (signerCert == null)
            {
                throw new ArgumentNullException("signerCert");
            }
            if (this.IsSigned)
            {
                throw new InvalidOperationException("Message is already signed.");
            }

            MIME_Entity msgEntity = new MIME_Entity();

            msgEntity.Body = this.Body;
            msgEntity.ContentDisposition      = this.ContentDisposition;
            msgEntity.ContentTransferEncoding = this.ContentTransferEncoding;
            this.ContentTransferEncoding      = null;

            MIME_b_MultipartSigned multipartSigned = new MIME_b_MultipartSigned();

            this.Body = multipartSigned;
            multipartSigned.SetCertificate(signerCert);
            multipartSigned.BodyParts.Add(msgEntity);
        }
        // TODO:
        //public MIME_Entity GetEntityByPartsSpecifier(string partsSpecifier)

        #region method ConvertToMultipartSigned

        /// <summary>
        /// Converts message to multipart/signed message.
        /// </summary>
        /// <param name="signerCert">>Signer certificate</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>signerCert</b> is null reference.</exception>
        /// <exception cref="InvalidOperationException">Is raised when this method is called for already signed message.</exception>
        public void ConvertToMultipartSigned(X509Certificate2 signerCert)
        {
            if(signerCert == null){
                throw new ArgumentNullException("signerCert");
            }
            if(this.IsSigned){
                throw new InvalidOperationException("Message is already signed.");
            }

            MIME_Entity msgEntity = new MIME_Entity();
            msgEntity.Body = this.Body;
            msgEntity.ContentDisposition = this.ContentDisposition;
            msgEntity.ContentTransferEncoding = this.ContentTransferEncoding;
            this.ContentTransferEncoding = null;

            MIME_b_MultipartSigned multipartSigned = new MIME_b_MultipartSigned();
            this.Body = multipartSigned;
            multipartSigned.SetCertificate(signerCert);
            multipartSigned.BodyParts.Add(msgEntity);
        }
        /// <summary>
        /// Creates mail message.
        /// </summary>
        /// <param name="signerCert">Signer certificate,</param>
        /// <param name="from">From: address.</param>
        /// <param name="to">To: address.</param>
        /// <param name="cc">Cc: address. Value null means not used.</param>
        /// <param name="bcc">bcc: address. Value null means not used.</param>
        /// <param name="subject">Message subject.</param>
        /// <param name="text">Message body text.</param>
        /// <param name="html">Message HTML text. Value null means not used.</param>
        /// <param name="attachments">Message attachments. Value null means not used.</param>
        /// <returns>Returns created mail message.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>signerCert</b> is null reference.</exception>
        public static Mail_Message Create_MultipartSigned(X509Certificate2 signerCert,Mail_t_Mailbox from,Mail_t_Address[] to,Mail_t_Address[] cc,Mail_t_Address[] bcc,string subject,string text,string html,Mail_t_Attachment[] attachments)
        {
            if(signerCert == null){
                throw new ArgumentNullException("signerCert");
            }

            // Create header.
            Mail_Message msg = new Mail_Message();
            msg.MimeVersion = "1.0";
            msg.MessageID = MIME_Utils.CreateMessageID();
            msg.Date = DateTime.Now;
            if(from != null){
                msg.From = new Mail_t_MailboxList();
                msg.From.Add(from);
            }
            if(to != null){
                msg.To = new Mail_t_AddressList();
                foreach(Mail_t_Address address in to){
                    msg.To.Add(address);
                }
            }
            msg.Subject = subject;

            // Create message without attachments.
            if(attachments == null || attachments.Length == 0){
                // multipart/signed
                //  text/plain
                //  application/pkcs7-signature  MIME library adds this when message saved out.
                if(string.IsNullOrEmpty(html)){
                    MIME_b_MultipartSigned multipartSigned = new MIME_b_MultipartSigned();
                    msg.Body = multipartSigned;
                    multipartSigned.SetCertificate(signerCert);
                        multipartSigned.BodyParts.Add(MIME_Entity.CreateEntity_Text_Plain(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,text));
                }
                // multipart/signed
                //  multipart/alternative
                //   text/plain
                //   text/html
                //  application/pkcs7-signature  MIME library adds this when message saved out.
                else{
                    MIME_b_MultipartSigned multipartSigned = new MIME_b_MultipartSigned();
                    msg.Body = multipartSigned;
                    multipartSigned.SetCertificate(signerCert);
                        MIME_Entity entity_multipart_alternative = new MIME_Entity();
                        MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative();
                        entity_multipart_alternative.Body = multipartAlternative;
                        multipartSigned.BodyParts.Add(entity_multipart_alternative);
                            multipartAlternative.BodyParts.Add(MIME_Entity.CreateEntity_Text_Plain(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,text));
                            multipartAlternative.BodyParts.Add(MIME_Entity.CreateEntity_Text_Html(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,html));
                }                
            }
            // Create message with attachments.
            else{
                // multipart/signed
                //  multipart/mixed
                //   text/plain
                //   application/octet-stream
                //  application/pkcs7-signature  MIME library adds this when message saved out.
                if(string.IsNullOrEmpty(html)){
                    MIME_b_MultipartSigned multipartSigned = new MIME_b_MultipartSigned();
                    msg.Body = multipartSigned;
                    multipartSigned.SetCertificate(signerCert);
                        MIME_Entity entity_multipart_mixed = new MIME_Entity();
                        MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed();
                        entity_multipart_mixed.Body = multipartMixed;
                        multipartSigned.BodyParts.Add(entity_multipart_mixed);
                            multipartMixed.BodyParts.Add(MIME_Entity.CreateEntity_Text_Plain(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,text));

                            // Add attachments.
                            foreach(Mail_t_Attachment attachment in attachments){
                                try{
                                    multipartMixed.BodyParts.Add(MIME_Entity.CreateEntity_Attachment(attachment.Name,attachment.GetStream()));
                                }
                                finally{
                                    attachment.CloseStream();
                                }
                            }
                }
                // multipart/signed
                //  multipart/mixed
                //   multipart/alternative
                //    text/plain
                //    text/html
                //   application/octet-stream
                //  application/pkcs7-signature  MIME library adds this when message saved out.
                else{
                    MIME_b_MultipartSigned multipartSigned = new MIME_b_MultipartSigned();
                    msg.Body = multipartSigned;
                    multipartSigned.SetCertificate(signerCert);
                        MIME_Entity entity_multipart_mixed = new MIME_Entity();
                        MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed();
                        entity_multipart_mixed.Body = multipartMixed;
                        multipartSigned.BodyParts.Add(entity_multipart_mixed);
                            MIME_Entity entity_multipart_alternative = new MIME_Entity();
                            MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative();
                            entity_multipart_alternative.Body = multipartAlternative;
                            multipartMixed.BodyParts.Add(entity_multipart_alternative);
                                multipartAlternative.BodyParts.Add(MIME_Entity.CreateEntity_Text_Plain(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,text));
                                multipartAlternative.BodyParts.Add(MIME_Entity.CreateEntity_Text_Html(MIME_TransferEncodings.QuotedPrintable,Encoding.UTF8,html));

                            // Add attachments.
                            foreach(Mail_t_Attachment attachment in attachments){
                                try{
                                    multipartMixed.BodyParts.Add(MIME_Entity.CreateEntity_Attachment(attachment.Name,attachment.GetStream()));
                                }
                                finally{
                                    attachment.CloseStream();
                                }
                            }
                }                
            }

            return msg;
        }