Пример #1
0
        private void WriteEncodedBodyText(Stream stream, String value, TransferEncoding encodeType, Encoding encoding, Int32 maxCharCount)
        {
            Stream str = stream;

            Byte[] bb = null;

            if (maxCharCount > MaxCharCountPerRow)
            {
                throw new ArgumentException("maxCharCount must less than MimeWriter.MaxCharCountPerRow.");
            }

            switch (encodeType)
            {
            case TransferEncoding.None: bb = GetAsciiBytes(value); break;

            case TransferEncoding.SevenBit: bb = GetAsciiBytes(value); break;

            case TransferEncoding.EightBit: bb = encoding.GetBytes(value); break;

            case TransferEncoding.Binary: bb = Encoding.UTF8.GetBytes(value); break;

            case TransferEncoding.Base64: bb = _Base64BodyConverter.Encode(encoding.GetBytes(value)); break;

            case TransferEncoding.QuotedPrintable: bb = _QuotedPrintableBodyConverter.Encode(encoding.GetBytes(value)); break;

            default: throw new InvalidOperationException();
            }
            stream.Write(bb);
            stream.Write(ByteData.NewLine);
        }
        public void QuotedPrintableConverter_Encode_InsertNewline()
        {
            var qc = new QuotedPrintableConverter(2000, QuotedPrintableConvertMode.Default);

            qc.InsertNewline = true;

            var bb          = qc.Encode(Encoding.GetEncoding("Iso-2022-JP").GetBytes(BodyText));
            var encodedText = Encoding.UTF8.GetString(bb);

            Assert.AreEqual("=1B$B5@1`@:<K$N>b$N@<=1B(B=0D=0A=1B$B=3Dt9TL5>o$N6A$-$\"$j=1B(B=0D=0A=1B$B:=" + Environment.NewLine
                            + ";MeAP<y$N2V$N?'=1B(B=0D=0A=1B$B@9<TI,?j$NM}$r$\"$i$o$9=1B(B=0D=0A", encodedText);
        }
Пример #3
0
        private void Write(Stream stream, SmtpContent content)
        {
            var ct = content;

            ThrowExceptionIfValueIsNull(this.HeaderEncoding, "You must set HeaderEncoding property of SmtpContent object.");
            ThrowExceptionIfValueIsNull(ct.ContentDisposition, "You must set ContentDisposition property of SmtpContent object.");
            ThrowExceptionIfValueIsNull(ct.ContentType, "You must set ContentType property of SmtpContent object.");
            ThrowExceptionIfValueIsNull(ct.ContentType.CharsetEncoding, "You must set CharsetEncoding property of ContentType property of SmtpContent object.");

            foreach (var header in ct.Headers)
            {
                this.WriteHeader(stream, header.Key, header.Value);
            }

            if (ct.ContentType.IsMultipart == true && String.IsNullOrEmpty(ct.ContentType.Boundary) == true)
            {
                ct.ContentType.Boundary = MimeWriter.GenerateBoundary();
            }
            this.WriteEncodedHeader(stream, ct.ContentType);
            this.WriteEncodedHeader(stream, ct.ContentDisposition);

            stream.Write(ByteData.NewLine);

            if (ct.ContentType.IsMultipart == true)
            {
                var boundary = ct.ContentType.CharsetEncoding.GetBytes("--" + ct.ContentType.Boundary + "\r\n");
                for (int i = 0; i < ct.Contents.Count; i++)
                {
                    stream.Write(boundary);

                    Write(stream, ct.Contents[i]);
                }
                stream.Write(ct.ContentType.CharsetEncoding.GetBytes("--" + ct.ContentType.Boundary + "--\r\n"));
            }
            else
            {
                if (ct.ContentType.IsText == true && String.IsNullOrEmpty(ct.BodyText) == false)
                {
                    this.WriteEncodedBodyText(stream, ct.BodyText, ct.ContentTransferEncoding, ct.ContentType.CharsetEncoding, 74);
                }
                else if (ct.BodyData != null)
                {
                    Byte[] bb = null;
                    switch (ct.ContentTransferEncoding)
                    {
                    case TransferEncoding.Base64:
                    {
                        var converter = new Base64Converter(ct.BodyData.Length);
                        bb = converter.Encode(ct.BodyData);
                    }
                    break;

                    case TransferEncoding.QuotedPrintable:
                    {
                        var converter = new QuotedPrintableConverter(ct.BodyData.Length, QuotedPrintableConvertMode.Default);
                        bb = converter.Encode(ct.BodyData);
                    }
                    break;

                    case TransferEncoding.None:
                    case TransferEncoding.SevenBit:
                    case TransferEncoding.EightBit:
                    case TransferEncoding.Binary:
                    default: throw new InvalidOperationException();
                    }
                    stream.Write(bb, 0, bb.Length);
                    stream.Write(ByteData.NewLine);
                }
            }
        }
 public void EncodeStringTest1(string?quoted)
 => Assert.AreEqual(string.Empty, QuotedPrintableConverter.Encode(quoted, 0));