Beispiel #1
0
        public void EncoderAndDecoder_ShouldEncodeAndDecode(string testHeader, int expectedFoldedCount)
        {
            string result = MimeBasePart.EncodeHeaderValue(testHeader, Encoding.UTF8, true);

            Assert.StartsWith("=?utf-8?B?", result, StringComparison.Ordinal);
            Assert.EndsWith("?=", result, StringComparison.Ordinal);

            string[] foldedHeaders = result.Split('\r');
            Assert.Equal(expectedFoldedCount, foldedHeaders.Length);
            foreach (string foldedHeader in foldedHeaders)
            {
                Assert.InRange(foldedHeader.Length, 0, 76);
            }

            Assert.Equal(testHeader, MimeBasePart.DecodeHeaderValue(result));
        }
Beispiel #2
0
        public void EncodeHeader_Base64Encoding_ShouldSplitBetweenCodepoints()
        {
            // header parts split by max line length in base64 encoding = 70 with respect to codepoints
            string headerPart1     = "Emoji subject : 🕐🕑🕒🕓🕔🕕";
            string headerPart2     = "🕖🕗🕘🕙🕚";
            string longEmojiHeader = headerPart1 + headerPart2;

            string encodedHeader = MimeBasePart.EncodeHeaderValue(longEmojiHeader, Encoding.UTF8, true);

            string encodedPart1 = MimeBasePart.EncodeHeaderValue(headerPart1, Encoding.UTF8, true);
            string encodedPart2 = MimeBasePart.EncodeHeaderValue(headerPart2, Encoding.UTF8, true);

            Assert.Equal("=?utf-8?B?RW1vamkgc3ViamVjdCA6IPCflZDwn5WR8J+VkvCflZPwn5WU8J+VlQ==?=", encodedPart1);
            Assert.Equal("=?utf-8?B?8J+VlvCflZfwn5WY8J+VmfCflZo=?=", encodedPart2);

            string expectedEncodedHeader = encodedPart1 + "\r\n " + encodedPart2;

            Assert.Equal(expectedEncodedHeader, encodedHeader);
        }
Beispiel #3
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);
        }
Beispiel #4
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;
            }
        }
Beispiel #5
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;
         }
     }
 }
Beispiel #6
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);
        }
Beispiel #7
0
        public void EncodeHeader_QEncoding_ShouldSplitBetweenCodepoints()
        {
            // header parts split by max line length in q-encoding = 70 with respect to codepoints
            string headerPart1     = "Emoji subject : 🕐🕑🕒";
            string headerPart2     = "🕓🕔🕕🕖";
            string headerPart3     = "🕗🕘🕙🕚";
            string longEmojiHeader = headerPart1 + headerPart2 + headerPart3;

            string encodedHeader = MimeBasePart.EncodeHeaderValue(longEmojiHeader, Encoding.UTF8, false);

            string encodedPart1 = MimeBasePart.EncodeHeaderValue(headerPart1, Encoding.UTF8, false);
            string encodedPart2 = MimeBasePart.EncodeHeaderValue(headerPart2, Encoding.UTF8, false);
            string encodedPart3 = MimeBasePart.EncodeHeaderValue(headerPart3, Encoding.UTF8, false);

            Assert.Equal("=?utf-8?Q?Emoji_subject_=3A_=F0=9F=95=90=F0=9F=95=91=F0=9F=95=92?=", encodedPart1);
            Assert.Equal("=?utf-8?Q?=F0=9F=95=93=F0=9F=95=94=F0=9F=95=95=F0=9F=95=96?=", encodedPart2);
            Assert.Equal("=?utf-8?Q?=F0=9F=95=97=F0=9F=95=98=F0=9F=95=99=F0=9F=95=9A?=", encodedPart3);

            string expectedEncodedHeader = encodedPart1 + "\r\n " + encodedPart2 + "\r\n " + encodedPart3;

            Assert.Equal(expectedEncodedHeader, encodedHeader);
        }
Beispiel #8
0
 internal MimePartAsyncResult(MimeBasePart part, object?state, AsyncCallback?callback) : base(part, state, callback)
 {
 }
Beispiel #9
0
        internal void PrepareHeaders(bool sendEnvelope, bool allowUnicode)
        {
            string headerName;

            if (_headersEncoding == null)
            {
                _headersEncoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
            }

            //ContentType is written directly to the stream so remove potential user duplicate
            Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ContentType) !);

            Headers[MailHeaderInfo.GetString(MailHeaderID.MimeVersion)] = "1.0";

            // add sender to headers first so that it is written first to allow the IIS smtp svc to
            // send MAIL FROM with the sender if both sender and from are present
            headerName = MailHeaderInfo.GetString(MailHeaderID.Sender) !;
            if (Sender != null)
            {
                Headers.InternalAdd(headerName, Sender.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            headerName = MailHeaderInfo.GetString(MailHeaderID.From) !;
            Headers.InternalAdd(headerName, From !.Encode(headerName.Length, allowUnicode));

            headerName = MailHeaderInfo.GetString(MailHeaderID.To) !;
            if (To.Count > 0)
            {
                Headers.InternalAdd(headerName, To.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            headerName = MailHeaderInfo.GetString(MailHeaderID.Cc) !;
            if (CC.Count > 0)
            {
                Headers.InternalAdd(headerName, CC.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            headerName = MailHeaderInfo.GetString(MailHeaderID.ReplyTo) !;
            if (ReplyTo != null)
            {
                Headers.InternalAdd(headerName, ReplyTo.Encode(headerName.Length, allowUnicode));
            }
            else if (ReplyToList.Count > 0)
            {
                Headers.InternalAdd(headerName, ReplyToList.Encode(headerName.Length, allowUnicode));
            }
            else
            {
                Headers.Remove(headerName);
            }

            Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc) !);

            if (_priority == MailPriority.High)
            {
                Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "1";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "urgent";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "high";
            }
            else if (_priority == MailPriority.Low)
            {
                Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "5";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "non-urgent";
                Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "low";
            }
            //if the priority was never set, allow the app to set the headers directly.
            else if (((int)_priority) != -1)
            {
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.XPriority) !);
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Priority) !);
                Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Importance) !);
            }

            Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Date) !,
                                MailBnfHelper.GetDateTimeString(DateTime.Now, null) !);

            headerName = MailHeaderInfo.GetString(MailHeaderID.Subject) !;
            if (!string.IsNullOrEmpty(_subject))
            {
                if (allowUnicode)
                {
                    Headers.InternalAdd(headerName, _subject);
                }
                else
                {
                    Headers.InternalAdd(headerName,
                                        MimeBasePart.EncodeHeaderValue(_subject, _subjectEncoding,
                                                                       MimeBasePart.ShouldUseBase64Encoding(_subjectEncoding),
                                                                       headerName.Length));
                }
            }
            else
            {
                Headers.Remove(headerName);
            }

            EncodeHeaders(_headers !, allowUnicode);
        }
Beispiel #10
0
 internal void SetContentTypeName(bool allowUnicode)
 {
     if (!allowUnicode && _name != null && _name.Length != 0 && !MimeBasePart.IsAscii(_name, false))
     {
         Encoding encoding = NameEncoding;
         if (encoding == null)
         {
             encoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
         }
         MimePart.ContentType.Name = MimeBasePart.EncodeHeaderValue(_name, encoding, MimeBasePart.ShouldUseBase64Encoding(encoding));
     }
     else
     {
         MimePart.ContentType.Name = _name;
     }
 }
Beispiel #11
0
        internal void SetContentFromString(string content, Encoding encoding, string mediaType)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

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

            if (mediaType == null || mediaType == string.Empty)
            {
                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(Strings.MediaTypeInvalid, nameof(mediaType));
                }
                value = MailBnfHelper.ReadToken(mediaType, ref offset, null);
                if (value.Length == 0 || offset < mediaType.Length)
                {
                    throw new ArgumentException(Strings.MediaTypeInvalid, nameof(mediaType));
                }
            }
            catch (FormatException)
            {
                throw new ArgumentException(Strings.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;
            }
        }
 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);
                 }
             }
         }
     }
 }
 internal void PrepareHeaders(bool sendEnvelope)
 {
     if (this.headersEncoding == null)
     {
         this.headersEncoding = Encoding.GetEncoding("utf-8");
     }
     this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ContentType));
     this.Headers[MailHeaderInfo.GetString(MailHeaderID.MimeVersion)] = "1.0";
     if (this.Sender != null)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Sender), this.Sender.Encode(MailHeaderInfo.GetString(MailHeaderID.Sender).ToString().Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Sender));
     }
     this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.From), this.From.Encode(MailHeaderInfo.GetString(MailHeaderID.From).ToString().Length));
     if (this.To.Count > 0)
     {
         string str = this.To.Encode(MailHeaderInfo.GetString(MailHeaderID.To).Length);
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.To), str);
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.To));
     }
     if (this.CC.Count > 0)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Cc), this.CC.Encode(MailHeaderInfo.GetString(MailHeaderID.Cc).Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Cc));
     }
     if (this.ReplyTo != null)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.ReplyTo), this.ReplyTo.Encode(MailHeaderInfo.GetString(MailHeaderID.ReplyTo).Length));
     }
     else if (this.ReplyToList.Count > 0)
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.ReplyTo), this.ReplyToList.Encode(MailHeaderInfo.GetString(MailHeaderID.ReplyTo).Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ReplyTo));
     }
     this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Bcc));
     if (this.priority == MailPriority.High)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "1";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "urgent";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "high";
     }
     else if (this.priority == MailPriority.Low)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "5";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "non-urgent";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "low";
     }
     else if (this.priority != ~MailPriority.Normal)
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.XPriority));
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Priority));
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Importance));
     }
     this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Date), MailBnfHelper.GetDateTimeString(DateTime.Now, null));
     if (!string.IsNullOrEmpty(this.subject))
     {
         this.Headers.InternalAdd(MailHeaderInfo.GetString(MailHeaderID.Subject), MimeBasePart.EncodeHeaderValue(this.subject, this.subjectEncoding, MimeBasePart.ShouldUseBase64Encoding(this.subjectEncoding), MailHeaderID.Subject.ToString().Length));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Subject));
     }
     this.EncodeHeaders(this.headers);
 }
Beispiel #14
0
 internal void PrepareHeaders(bool sendEnvelope)
 {
     this.Headers[MailHeaderInfo.GetString(MailHeaderID.MimeVersion)] = "1.0";
     this.Headers[MailHeaderInfo.GetString(MailHeaderID.From)]        = this.From.ToEncodedString();
     if (this.Sender != null)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Sender)] = this.Sender.ToEncodedString();
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Sender));
     }
     if (this.To.Count > 0)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.To)] = this.To.ToEncodedString();
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.To));
     }
     if (this.CC.Count > 0)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Cc)] = this.CC.ToEncodedString();
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Cc));
     }
     if (this.replyTo != null)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.ReplyTo)] = this.ReplyTo.ToEncodedString();
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.ReplyTo));
     }
     if (this.priority == MailPriority.High)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "1";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "urgent";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "high";
     }
     else if (this.priority == MailPriority.Low)
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.XPriority)]  = "5";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Priority)]   = "non-urgent";
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Importance)] = "low";
     }
     else if (this.priority != ~MailPriority.Normal)
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.XPriority));
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Priority));
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Importance));
     }
     this.Headers[MailHeaderInfo.GetString(MailHeaderID.Date)] = MailBnfHelper.GetDateTimeString(DateTime.Now, null);
     if ((this.subject != null) && (this.subject != string.Empty))
     {
         this.Headers[MailHeaderInfo.GetString(MailHeaderID.Subject)] = MimeBasePart.EncodeHeaderValue(this.subject, this.subjectEncoding, MimeBasePart.ShouldUseBase64Encoding(this.subjectEncoding));
     }
     else
     {
         this.Headers.Remove(MailHeaderInfo.GetString(MailHeaderID.Subject));
     }
 }
 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;
     }
 }