Пример #1
0
        public AppMailMessage(string sender, string xmlMessage)
        {
            emailMessage = null;

            if (sender == null)
            {
                sender = "unKnown";
            }

            this.sender = sender;

            if (xmlMessage == null)
            {
                xmlMessage = "";
            }

            XmlSerializer s = new XmlSerializer(typeof(EmailMessage));
            StringReader  r = new StringReader(xmlMessage);

            try
            {
                emailMessage = s.Deserialize(r) as EmailMessage;

                if (emailMessage == null)
                {
                    throw (new ConfigurationErrorsException("Message is empty."));
                }

                if (emailMessage.MailList == null)
                {
                    throw (new ConfigurationErrorsException("Message is missing receipient information."));
                }

                if (emailMessage.Message == null)
                {
                    throw (new ConfigurationErrorsException("Message content is missing."));
                }

                if (emailMessage.Message.Subject == null)
                {
                    throw (new ConfigurationErrorsException("Message subject is missing."));
                }

                if (emailMessage.Message.Body == null)
                {
                    throw (new ConfigurationErrorsException("Message body is missing."));
                }

                if (emailMessage.Message.Body.Length == 0)
                {
                    throw (new ConfigurationErrorsException("Message body is empty."));
                }
            }
            catch (InvalidOperationException)
            {
                // Validate the contents
                String       schemaFileName = String.Format("{0}.xsd.EmailMessage.xsd", defaultNameSpace);
                StreamReader schema;

                try
                {
                    schema = FileIO.GetFileFromResources(schemaFileName);
                }
                catch (ConfigurationErrorsException ex)
                {
                    throw (new ConfigurationErrorsException(
                               String.Format(
                                   "Failed to load embedded schema file, cannot validate email message. Schema file = \"{0}\"", schemaFileName), ex));
                }

                // Validate file, get 10 errors max
                XmlValidator xv     = new XmlValidator();
                String       errors = xv.ValidateString(xmlMessage, schema.BaseStream, 10);

                if (errors != "")
                {
                    throw (new ConfigurationErrorsException(
                               String.Format(
                                   "The email message contains syntax errors.\n{0}", errors)));
                }
            }
        }