Ejemplo n.º 1
0
        public void EncodeString_IsSameAsEncodeBytes_IfOneByteCodepointOnLineWrap(bool useBase64Encoding, string value)
        {
            var factory = new EncodedStreamFactory();
            IEncodableStream streamForEncodeString = factory.GetEncoderForHeader(Encoding.UTF8, useBase64Encoding, 0);
            IEncodableStream streamForEncodeBytes  = factory.GetEncoderForHeader(Encoding.UTF8, useBase64Encoding, 0);

            streamForEncodeString.EncodeString(value, Encoding.UTF8);
            string encodeStringResult = streamForEncodeString.GetEncodedString();

            byte[] bytes = Encoding.UTF8.GetBytes(value);
            streamForEncodeBytes.EncodeBytes(bytes, 0, bytes.Length);
            string encodeBytesResult = streamForEncodeBytes.GetEncodedString();

            Assert.Equal(encodeBytesResult, encodeStringResult);
        }
        public void EncodedStreamFactory_WhenAskedForEncodedStreamForHeader_WithBase64_ShouldReturnBase64Stream()
        {
            var esf = new EncodedStreamFactory();
            IEncodableStream test = esf.GetEncoderForHeader(Encoding.UTF8, true, 5);

            Assert.True(test is Base64Stream);
        }
        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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 public void EncodedStreamFactory_WhenAskedForEncodedStreamForHeader_WithBase64_ShouldReturnBase64Stream()
 {
     var esf = new EncodedStreamFactory();
     IEncodableStream test = esf.GetEncoderForHeader(Encoding.UTF8, true, 5);
     Assert.True(test is Base64Stream);
 }