internal void EncodeHeaders(HeaderCollection headers)
 {
     if (this.headersEncoding == null)
     {
         this.headersEncoding = Encoding.GetEncoding("utf-8");
     }
     for (int i = 0; i < headers.Count; i++)
     {
         string key = headers.GetKey(i);
         if (MailHeaderInfo.IsUserSettable(key))
         {
             string[] values = headers.GetValues(key);
             string   str2   = string.Empty;
             for (int j = 0; j < values.Length; j++)
             {
                 if (MimeBasePart.IsAscii(values[j], false))
                 {
                     str2 = values[j];
                 }
                 else
                 {
                     str2 = MimeBasePart.EncodeHeaderValue(values[j], this.headersEncoding, MimeBasePart.ShouldUseBase64Encoding(this.headersEncoding), key.Length);
                 }
                 if (j == 0)
                 {
                     headers.Set(key, str2);
                 }
                 else
                 {
                     headers.Add(key, str2);
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        internal void SetContentFromString(string contentString, System.Net.Mime.ContentType contentType)
        {
            Encoding aSCII;

            if (contentString == null)
            {
                throw new ArgumentNullException("content");
            }
            if (this.part.Stream != null)
            {
                this.part.Stream.Close();
            }
            if ((contentType != null) && (contentType.CharSet != null))
            {
                aSCII = Encoding.GetEncoding(contentType.CharSet);
            }
            else if (MimeBasePart.IsAscii(contentString, false))
            {
                aSCII = Encoding.ASCII;
            }
            else
            {
                aSCII = Encoding.GetEncoding("utf-8");
            }
            byte[] bytes = aSCII.GetBytes(contentString);
            this.part.SetContent(new MemoryStream(bytes), contentType);
            if (MimeBasePart.ShouldUseBase64Encoding(aSCII))
            {
                this.part.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
            }
            else
            {
                this.part.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            }
        }
Exemplo n.º 3
0
        internal void SetContentFromString(string content, Encoding?encoding, string?mediaType)
        {
            ArgumentNullException.ThrowIfNull(content);

            _part.Stream?.Close();

            if (string.IsNullOrEmpty(mediaType))
            {
                mediaType = MediaTypeNames.Text.Plain;
            }

            //validate the mediaType
            int offset = 0;

            try
            {
                string value = MailBnfHelper.ReadToken(mediaType, ref offset, null);
                if (value.Length == 0 || offset >= mediaType.Length || mediaType[offset++] != '/')
                {
                    throw new ArgumentException(SR.MediaTypeInvalid, nameof(mediaType));
                }
                value = MailBnfHelper.ReadToken(mediaType, ref offset, null);
                if (value.Length == 0 || offset < mediaType.Length)
                {
                    throw new ArgumentException(SR.MediaTypeInvalid, nameof(mediaType));
                }
            }
            catch (FormatException)
            {
                throw new ArgumentException(SR.MediaTypeInvalid, nameof(mediaType));
            }


            ContentType contentType = new ContentType(mediaType);

            if (encoding == null)
            {
                if (MimeBasePart.IsAscii(content, false))
                {
                    encoding = Encoding.ASCII;
                }
                else
                {
                    encoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
                }
            }

            contentType.CharSet = encoding.BodyName;
            byte[] buffer = encoding.GetBytes(content);
            _part.SetContent(new MemoryStream(buffer), contentType);

            if (MimeBasePart.ShouldUseBase64Encoding(encoding))
            {
                _part.TransferEncoding = TransferEncoding.Base64;
            }
            else
            {
                _part.TransferEncoding = TransferEncoding.QuotedPrintable;
            }
        }
Exemplo n.º 4
0
        internal void SetContentFromString(string content, ContentType?contentType)
        {
            ArgumentNullException.ThrowIfNull(content);

            _part.Stream?.Close();

            Encoding encoding;

            if (contentType != null && contentType.CharSet != null)
            {
                encoding = Text.Encoding.GetEncoding(contentType.CharSet);
            }
            else
            {
                if (MimeBasePart.IsAscii(content, false))
                {
                    encoding = Text.Encoding.ASCII;
                }
                else
                {
                    encoding = Text.Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
                }
            }
            byte[] buffer = encoding.GetBytes(content);
            _part.SetContent(new MemoryStream(buffer), contentType);

            if (MimeBasePart.ShouldUseBase64Encoding(encoding))
            {
                _part.TransferEncoding = TransferEncoding.Base64;
            }
            else
            {
                _part.TransferEncoding = TransferEncoding.QuotedPrintable;
            }
        }
        internal string Encode(int charsConsumed)
        {
            string encodedString = string.Empty;

            if (!string.IsNullOrEmpty(this.displayName))
            {
                if (MimeBasePart.IsAscii(this.displayName, false))
                {
                    encodedString = string.Format("\"{0}\"", this.displayName);
                }
                else
                {
                    IEncodableStream stream = encoderFactory.GetEncoderForHeader(this.displayNameEncoding, false, charsConsumed);
                    byte[]           bytes  = this.displayNameEncoding.GetBytes(this.displayName);
                    stream.EncodeBytes(bytes, 0, bytes.Length);
                    encodedString = stream.GetEncodedString();
                }
                encodedString = encodedString + "\r\n ";
            }
            if (!string.IsNullOrEmpty(encodedString))
            {
                return(encodedString + this.SmtpAddress);
            }
            return(this.Address);
        }
Exemplo n.º 6
0
 private string GetUser(bool allowUnicode)
 {
     // Unicode usernames cannot be downgraded
     if (!allowUnicode && !MimeBasePart.IsAscii(userName, true))
     {
         throw new SmtpException(SR.GetString(SR.SmtpNonAsciiUserNotSupported, Address));
     }
     return(userName);
 }
Exemplo n.º 7
0
        internal void EncodeHeaders(HeaderCollection headers, bool allowUnicode)
        {
            if (_headersEncoding == null)
            {
                _headersEncoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
            }

            System.Diagnostics.Debug.Assert(_headersEncoding != null);

            for (int i = 0; i < headers.Count; i++)
            {
                string headerName = headers.GetKey(i);

                //certain well-known values are encoded by PrepareHeaders and PrepareEnvelopeHeaders
                //so we can ignore them because either we encoded them already or there is no
                //way for the user to have set them.  If a header is well known and user settable then
                //we should encode it here, otherwise we have already encoded it if necessary
                if (!MailHeaderInfo.IsUserSettable(headerName))
                {
                    continue;
                }

                string[] values       = headers.GetValues(headerName);
                string   encodedValue = string.Empty;
                for (int j = 0; j < values.Length; j++)
                {
                    //encode if we need to
                    if (MimeBasePart.IsAscii(values[j], false) ||
                        (allowUnicode && MailHeaderInfo.AllowsUnicode(headerName) &&  // EAI
                         !MailBnfHelper.HasCROrLF(values[j])))
                    {
                        encodedValue = values[j];
                    }
                    else
                    {
                        encodedValue = MimeBasePart.EncodeHeaderValue(values[j],
                                                                      _headersEncoding,
                                                                      MimeBasePart.ShouldUseBase64Encoding(_headersEncoding),
                                                                      headerName.Length);
                    }

                    //potentially there are multiple values per key
                    if (j == 0)
                    {
                        //if it's the first or only value, set will overwrite all the values assigned to that key
                        //which is fine since we have them stored in values[]
                        headers.Set(headerName, encodedValue);
                    }
                    else
                    {
                        //this is a subsequent key, so we must Add it since the first key will have overwritten the
                        //other values
                        headers.Add(headerName, encodedValue);
                    }
                }
            }
        }
Exemplo n.º 8
0
        internal void SetContentFromString(string contentString, Encoding encoding, string mediaType)
        {
            if (contentString == null)
            {
                throw new ArgumentNullException("content");
            }
            if (this.part.Stream != null)
            {
                this.part.Stream.Close();
            }
            if ((mediaType == null) || (mediaType == string.Empty))
            {
                mediaType = "text/plain";
            }
            int offset = 0;

            try
            {
                if (((MailBnfHelper.ReadToken(mediaType, ref offset, null).Length == 0) || (offset >= mediaType.Length)) || (mediaType[offset++] != '/'))
                {
                    throw new ArgumentException(SR.GetString("MediaTypeInvalid"), "mediaType");
                }
                if ((MailBnfHelper.ReadToken(mediaType, ref offset, null).Length == 0) || (offset < mediaType.Length))
                {
                    throw new ArgumentException(SR.GetString("MediaTypeInvalid"), "mediaType");
                }
            }
            catch (FormatException)
            {
                throw new ArgumentException(SR.GetString("MediaTypeInvalid"), "mediaType");
            }
            ContentType contentType = new ContentType(mediaType);

            if (encoding == null)
            {
                if (MimeBasePart.IsAscii(contentString, false))
                {
                    encoding = Encoding.ASCII;
                }
                else
                {
                    encoding = Encoding.GetEncoding("utf-8");
                }
            }
            contentType.CharSet = encoding.BodyName();
            byte[] bytes = encoding.GetBytes(contentString);
            this.part.SetContent(new MemoryStream(bytes), contentType);
            if (MimeBasePart.ShouldUseBase64Encoding(encoding))
            {
                this.part.TransferEncoding = TransferEncoding.Base64;
            }
            else
            {
                this.part.TransferEncoding = TransferEncoding.QuotedPrintable;
            }
        }
Exemplo n.º 9
0
 internal void SetContentTypeName(bool allowUnicode)
 {
     if (!allowUnicode && _name != null && _name.Length != 0 && !MimeBasePart.IsAscii(_name, false))
     {
         Encoding encoding = NameEncoding ?? Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
         MimePart.ContentType.Name = MimeBasePart.EncodeHeaderValue(_name, encoding, MimeBasePart.ShouldUseBase64Encoding(encoding));
     }
     else
     {
         MimePart.ContentType.Name = _name;
     }
 }
 internal void SetContentTypeName()
 {
     if (((this.name != null) && (this.name.Length != 0)) && !MimeBasePart.IsAscii(this.name, false))
     {
         Encoding nameEncoding = this.NameEncoding;
         if (nameEncoding == null)
         {
             nameEncoding = Encoding.GetEncoding("utf-8");
         }
         base.MimePart.ContentType.Name = MimeBasePart.EncodeHeaderValue(this.name, nameEncoding, MimeBasePart.ShouldUseBase64Encoding(nameEncoding));
     }
     else
     {
         base.MimePart.ContentType.Name = this.name;
     }
 }
Exemplo n.º 11
0
        private string GetHost(bool allowUnicode)
        {
            string domain = host;

            // Downgrade Unicode domain names
            if (!allowUnicode && !MimeBasePart.IsAscii(domain, true))
            {
                IdnMapping mapping = new IdnMapping();
                try
                {
                    domain = mapping.GetAscii(domain);
                }
                catch (ArgumentException argEx)
                {
                    throw new SmtpException(SR.GetString(SR.SmtpInvalidHostName, Address), argEx);
                }
            }
            return(domain);
        }
Exemplo n.º 12
0
        internal void SetContentFromString(string contentString, ContentType contentType)
        {
            if (contentString == null)
            {
                throw new ArgumentNullException("content");
            }

            if (part.Stream != null)
            {
                part.Stream.Close();
            }

            Encoding encoding;

            if (contentType != null && contentType.CharSet != null)
            {
                encoding = Text.Encoding.GetEncoding(contentType.CharSet);
            }
            else
            {
                if (MimeBasePart.IsAscii(contentString, false))
                {
                    encoding = Text.Encoding.ASCII;
                }
                else
                {
                    encoding = Text.Encoding.GetEncoding(MimeBasePart.defaultCharSet);
                }
            }
            byte[] buffer = encoding.GetBytes(contentString);
            part.SetContent(new MemoryStream(buffer), contentType);


            if (MimeBasePart.ShouldUseBase64Encoding(encoding))
            {
                part.TransferEncoding = TransferEncoding.Base64;
            }
            else
            {
                part.TransferEncoding = TransferEncoding.QuotedPrintable;
            }
        }
Exemplo n.º 13
0
 public MailAddress(string address, string displayName, Encoding displayNameEncoding)
 {
     if (address == null)
     {
         throw new ArgumentNullException("address");
     }
     if (address == string.Empty)
     {
         throw new ArgumentException(SR.GetString("net_emptystringcall", new object[] { "address" }), "address");
     }
     this.displayNameEncoding = displayNameEncoding;
     this.displayName         = displayName;
     this.ParseValue(address);
     if ((this.displayName != null) && (this.displayName != string.Empty))
     {
         if ((this.displayName[0] == '"') && (this.displayName[this.displayName.Length - 1] == '"'))
         {
             this.displayName = this.displayName.Substring(1, this.displayName.Length - 2);
         }
         this.displayName = this.displayName.Trim();
     }
     if ((this.displayName != null) && (this.displayName.Length > 0))
     {
         if (!MimeBasePart.IsAscii(this.displayName, false) || (this.displayNameEncoding != null))
         {
             if (this.displayNameEncoding == null)
             {
                 this.displayNameEncoding = Encoding.GetEncoding("utf-8");
             }
             this.encodedDisplayName = MimeBasePart.EncodeHeaderValue(this.displayName, this.displayNameEncoding, MimeBasePart.ShouldUseBase64Encoding(displayNameEncoding));
             StringBuilder builder = new StringBuilder();
             int           offset  = 0;
             MailBnfHelper.ReadQuotedString(this.encodedDisplayName, ref offset, builder, true);
             this.encodedDisplayName = builder.ToString();
         }
         else
         {
             this.encodedDisplayName = this.displayName;
         }
     }
 }
Exemplo n.º 14
0
        // Encodes the full email address, folding as needed
        internal string Encode(int charsConsumed, bool allowUnicode)
        {
            string           encodedAddress = String.Empty;
            IEncodableStream encoder;

            byte[] buffer;

            Debug.Assert(this.Address != null, "address was null");

            //do we need to take into account the Display name?  If so, encode it
            if (!String.IsNullOrEmpty(this.displayName))
            {
                //figure out the encoding type.  If it's all ASCII and contains no CRLF then
                //it does not need to be encoded for parity with other email clients.  We will
                //however fold at the end of the display name so that the email address itself can
                //be appended.
                if (MimeBasePart.IsAscii(this.displayName, false) || allowUnicode)
                {
                    encodedAddress = String.Format(CultureInfo.InvariantCulture, "\"{0}\"", this.displayName);
                }
                else
                {
                    //encode the displayname since it's non-ascii
                    encoder = encoderFactory.GetEncoderForHeader(this.displayNameEncoding, false, charsConsumed);
                    buffer  = displayNameEncoding.GetBytes(this.displayName);
                    encoder.EncodeBytes(buffer, 0, buffer.Length);
                    encodedAddress = encoder.GetEncodedString();
                }

                //address should be enclosed in <> when a display name is present
                encodedAddress += " " + GetSmtpAddress(allowUnicode);
            }
            else
            {
                //no display name, just return the address
                encodedAddress = GetAddress(allowUnicode);
            }

            return(encodedAddress);
        }