Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public String GetRawBodyText(Encoding encoding)
        {
            MemoryStream mm = new MemoryStream();
            var          sw = new MimeWriter(mm);

            sw.WriteBody(this);
            return(encoding.GetString(mm.ToArray()));
        }
Example #2
0
        private void WriteHeader(Stream stream, String key, String value)
        {
            if (String.IsNullOrEmpty(value) == true)
            {
                return;
            }

            if (MimeWriter.AsciiCharOnly(value) == true)
            {
                this.WriteAsciiHeader(stream, key, value);
            }
            else
            {
                this.WriteEncodedHeader(stream, key, value);
            }
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="from"></param>
        /// <param name="message"></param>
        public SendMailCommand(String from, SmtpMessage message)
        {
            this.From = new MailAddress(from);
            List <MailAddress> l = new List <MailAddress>();

            l.AddRange(message.To);
            l.AddRange(message.Cc);
            l.AddRange(message.Bcc);
            this.RcptTo.AddRange(l);

            MemoryStream mm = new MemoryStream();
            var          sw = new MimeWriter(mm);

            sw.Write(message);
            this.Data = mm.ToArray();
        }
Example #4
0
        /// 初期化処理を行います。
        /// <summary>
        /// 初期化処理を行います。
        /// </summary>
        private void Initialize()
        {
            this.Headers   = new SmtpMailHeaderCollection();
            this._Contents = new List <SmtpContent>();

            this.Headers.Add("MIME-Version", "1.0");

            this.Date                 = DateTimeOffset.Now;
            this.Subject              = "";
            this.ContentType          = new ContentType("multipart/mixed");
            this.ContentType.Boundary = MimeWriter.GenerateBoundary();
            _BodyTextContent          = new SmtpContent();
            this.Contents.Add(_BodyTextContent);

            this.ContentType.CharsetEncoding = Default.ContentTypeCharsetEncoding;
            this.ContentTransferEncoding     = Default.ContentTransferEncoding;
        }
Example #5
0
        private void Write(Stream stream, SmtpMessage message)
        {
            var mg = message;

            ThrowExceptionIfValueIsNull(this.HeaderEncoding, "You must set HeaderEncoding property of MimeWriter object.");
            ThrowExceptionIfValueIsNull(mg.ContentType, "You must set ContentType property of SmtpMessage object.");

            if (mg.Contents.Count > 0 && String.IsNullOrEmpty(mg.ContentType.Boundary) == true)
            {
                mg.ContentType.Boundary = MimeWriter.GenerateBoundary();
            }
#if !NETFX_CORE
            if (this.DkimSignatureGenerator != null)
            {
                this.AddDkimSignatureHeader(mg);
            }
#endif
            foreach (var header in mg.Headers)
            {
                this.WriteHeader(stream, header.Key, header.Value);
            }
            //TO
            WriteMailAddressList(stream, "To", mg.To);
            //ReplyTo
            WriteMailAddressList(stream, "Reply-To", mg.ReplyTo);
            //Cc
            WriteMailAddressList(stream, "Cc", mg.Cc);
            //Bcc never write because blind for others

            this.WriteEncodedHeader(stream, mg.ContentType);

            stream.Write(ByteData.NewLine);

            WriteBody(stream, mg);

            stream.Write(ByteData.NewLine);
            stream.WriteByte(46);
            stream.Write(ByteData.NewLine);
        }
Example #6
0
        public IEnumerator <SmtpMailHeader> GetEnumerator()
        {
            yield return(new SmtpMailHeader("Date", MimeWriter.DateTimeOffsetString(this.Date)));

            if (this.Sender != null)
            {
                yield return(new SmtpMailHeader("Sender", this.Sender.RawValue));
            }
            if (this.From != null)
            {
                yield return(new SmtpMailHeader("From", this.From.RawValue));
            }
            yield return(new SmtpMailHeader("X-Priority", ((byte)this.Priority).ToString()));

            foreach (var item in _Headers)
            {
                yield return(item);
            }
            if (this.ContentTransferEncoding != TransferEncoding.None)
            {
                yield return(new SmtpMailHeader("Content-Transfer-Encoding", this.ContentTransferEncoding.ToHeaderText()));
            }
        }
Example #7
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);
                }
            }
        }
Example #8
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="encoding"></param>
 /// <returns></returns>
 public String GetRawText(Encoding encoding)
 {
     MemoryStream mm = new MemoryStream();
     var sw = new MimeWriter(mm);
     sw.Write(this);
     return encoding.GetString(mm.ToArray());
 }