Exemple #1
0
        public static SmtpMessage Create(System.Net.Mail.MailMessage message, SmtpMessageCreateCondition condition)
        {
            SmtpMessage mg = new SmtpMessage();

            mg.Subject = condition.SubjectFormatter.CreateSubject(message);
            mg.From    = condition.Sender;
            mg.Sender  = condition.Sender;
            mg.ContentType.CharsetEncoding = message.BodyEncoding;
            mg.Priority = message.Priority.Cast();

            if (condition.BodyFormatter.IsHtml == true)
            {
                var html = condition.BodyFormatter.CreateBodyText(message);
                mg._BodyTextContent.LoadHtml(html);
            }
            else
            {
                mg.BodyText = condition.BodyFormatter.CreateBodyText(message);
            }
            AddMailAddress(message, condition.To, mg.To);
            AddMailAddress(message, condition.Cc, mg.Cc);
            AddMailAddress(message, condition.Bcc, mg.Bcc);

            if (condition.CopyAlternateViews == true)
            {
                foreach (var item in message.AlternateViews)
                {
                    var ct = new SmtpContent();
                    ct.LoadData(item.ContentStream);

                    ct.ContentTransferEncoding = item.TransferEncoding.Cast();
                    ct.ContentType.SetProperty(item.ContentType);

                    mg.Contents.Add(ct);
                }
            }
            if (condition.CopyAttachments == true)
            {
                foreach (var item in message.Attachments)
                {
                    var ct = new SmtpContent();
                    ct.LoadData(item.ContentStream);

                    ct.ContentTransferEncoding = item.TransferEncoding.Cast();
                    ct.ContentType.SetProperty(item.ContentType);
                    ct.ContentDisposition.SetProperty(item.ContentDisposition);
                    mg.Contents.Add(ct);
                }
            }
            return(mg);
        }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="text"></param>
        /// <param name="html"></param>
        public void SetBodyAsAlternativeContent(String text, String html)
        {
            SmtpContent ct = null;

            _BodyTextContent.ContentType.Value = "multipart/alternative";
            _BodyTextContent.Contents.Clear();

            ct = new SmtpContent();
            ct.LoadText(text);
            _BodyTextContent.Contents.Add(ct);

            ct = new SmtpContent();
            ct.LoadHtml(html);
            _BodyTextContent.Contents.Add(ct);
        }
Exemple #3
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;
        }
Exemple #4
0
        /// 指定したファイルパスのファイルデータを元にデータをセットします。
        /// <summary>
        /// 指定したファイルパスのファイルデータを元にデータをセットします。
        /// </summary>
        /// <param name="filePath"></param>
        public void LoadFileData(String filePath)
        {
            FileInfo fi = null;

            Byte[] b = null;

            fi = new FileInfo(filePath);

            this.ContentType.Value           = SmtpContent.GetContentType(Path.GetExtension(filePath).Replace(".", ""));
            this.ContentType.Name            = fi.Name;
            this.ContentDisposition.FileName = fi.Name;
            this.ContentDisposition.Value    = "attachment";

            b = new Byte[fi.Length];
            using (var stm = new FileStream(filePath, FileMode.Open))
            {
                stm.Read(b, 0, b.Length);
                this.BodyText = Convert.ToBase64String(b);
                stm.Close();
            }
            this.SetData();
        }
Exemple #5
0
        public static SmtpMessage Create(MailMessage message, SmtpMessageCreateCondition condition)
        {
            SmtpMessage mg = new SmtpMessage();

            mg.Subject = condition.SubjectFormatter.CreateSubject(message);
            mg.From    = condition.Sender;
            mg.Sender  = condition.Sender;
            mg.ContentType.SetProperty(message.ContentType);
            mg.ContentTransferEncoding = message.ContentTransferEncoding;
            if (condition.BodyFormatter.IsHtml == true)
            {
                mg._BodyTextContent.LoadHtml(condition.BodyFormatter.CreateBodyText(message));
            }
            else
            {
                mg.BodyText = condition.BodyFormatter.CreateBodyText(message);
            }

            AddMailAddress(message, condition.To, mg.To);
            AddMailAddress(message, condition.Cc, mg.Cc);
            AddMailAddress(message, condition.Bcc, mg.Bcc);

            if (condition.CopyContents == true)
            {
                foreach (var item in message.Contents)
                {
                    var ct = new SmtpContent();
                    ct.LoadData(item.RawData);
                    ct.ContentType.SetProperty(item.ContentType);
                    ct.ContentDisposition.SetProperty(item.ContentDisposition);
                    mg.Contents.Add(ct);
                }
            }

            return(mg);
        }
Exemple #6
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);
                }
            }
        }
Exemple #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);
                }
            }
        }
Exemple #8
0
        public static SmtpMessage Create(System.Net.Mail.MailMessage message, SmtpMessageCreateCondition condition)
        {
            SmtpMessage mg = new SmtpMessage();

            mg.Subject = condition.SubjectFormatter.CreateSubject(message);
            mg.From = condition.Sender;
            mg.Sender = condition.Sender;
            mg.ContentType.CharsetEncoding = message.BodyEncoding;
            mg.Priority = message.Priority.Cast();

            if (condition.BodyFormatter.IsHtml == true)
            {
                var html = condition.BodyFormatter.CreateBodyText(message);
                mg._BodyTextContent.LoadHtml(html);
            }
            else
            {
                mg.BodyText = condition.BodyFormatter.CreateBodyText(message);
            }
            AddMailAddress(message, condition.To, mg.To);
            AddMailAddress(message, condition.Cc, mg.Cc);
            AddMailAddress(message, condition.Bcc, mg.Bcc);

            if (condition.CopyAlternateViews == true)
            {
                foreach (var item in message.AlternateViews)
                {
                    var ct = new SmtpContent();
                    ct.LoadData(item.ContentStream);

                    ct.ContentTransferEncoding = item.TransferEncoding.Cast();
                    ct.ContentType.SetProperty(item.ContentType);

                    mg.Contents.Add(ct);
                }
            }
            if (condition.CopyAttachments == true)
            {
                foreach (var item in message.Attachments)
                {
                    var ct = new SmtpContent();
                    ct.LoadData(item.ContentStream);

                    ct.ContentTransferEncoding = item.TransferEncoding.Cast();
                    ct.ContentType.SetProperty(item.ContentType);
                    ct.ContentDisposition.SetProperty(item.ContentDisposition);
                    mg.Contents.Add(ct);
                }
            }
            return mg;
        }
Exemple #9
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;
        }
Exemple #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="html"></param>
        public void SetBodyAsAlternativeContent(String text, String html)
        {
            SmtpContent ct = null;

            _BodyTextContent.ContentType.Value = "multipart/alternative";
            _BodyTextContent.Contents.Clear();

            ct = new SmtpContent();
            ct.LoadText(text);
            _BodyTextContent.Contents.Add(ct);

            ct = new SmtpContent();
            ct.LoadHtml(html);
            _BodyTextContent.Contents.Add(ct);
        }
Exemple #11
0
        public static SmtpMessage Create(MailMessage message, SmtpMessageCreateCondition condition)
        {
            SmtpMessage mg = new SmtpMessage();

            mg.Subject = condition.SubjectFormatter.CreateSubject(message);
            mg.From = condition.Sender;
            mg.Sender = condition.Sender;
            mg.ContentType.SetProperty(message.ContentType);
            mg.ContentTransferEncoding = message.ContentTransferEncoding;
            if (condition.BodyFormatter.IsHtml == true)
            {
                mg._BodyTextContent.LoadHtml(condition.BodyFormatter.CreateBodyText(message));
            }
            else
            {
                mg.BodyText = condition.BodyFormatter.CreateBodyText(message);
            }

            AddMailAddress(message, condition.To, mg.To);
            AddMailAddress(message, condition.Cc, mg.Cc);
            AddMailAddress(message, condition.Bcc, mg.Bcc);

            if (condition.CopyContents == true)
            {
                foreach (var item in message.Contents)
                {
                    var ct = new SmtpContent();
                    ct.LoadData(item.RawData);
                    ct.ContentType.SetProperty(item.ContentType);
                    ct.ContentDisposition.SetProperty(item.ContentDisposition);
                    mg.Contents.Add(ct);
                }
            }

            return mg;
        }
Exemple #12
0
 static SmtpContent()
 {
     SmtpContent.InitializeFileExtenstionContentType();
 }
Exemple #13
0
        /// 実際に送信される文字列のデータを取得します。
        /// <summary>
        /// 実際に送信される文字列のデータを取得します。
        /// </summary>
        /// <returns></returns>
        public String GetDataText()
        {
            StringBuilder sb       = new StringBuilder(1024);
            CultureInfo   ci       = CultureInfo.CurrentCulture;
            Field         f        = null;
            SmtpContent   ct       = null;
            String        line     = "";
            String        bodyText = "";

            //ContentTransferEncoding
            f = InternetTextMessage.Field.FindField(this.Header, "Content-Transfer-Encoding");
            if (f == null)
            {
                f = new Field("Content-Transfer-Encoding", MailParser.ToTransferEncoding(this.ContentTransferEncoding));
                this.Header.Add(f);
            }
            else
            {
                f.Value = MailParser.ToTransferEncoding(this.ContentTransferEncoding);
            }

            for (int i = 0; i < this.Header.Count; i++)
            {
                f = this.Header[i];
                if (this._EncodeHeaderKeys.Contains(f.Key.ToLower()) == true)
                {
                    sb.AppendFormat("{0}: {1}{2}", f.Key
                                    , MailParser.EncodeToMailHeaderLine(f.Value, this.HeaderTransferEncoding, this.HeaderEncoding
                                                                        , MailParser.MaxCharCountPerRow - f.Key.Length - 2), MailParser.NewLine);
                }
                else if (f.Key.ToLower() != "content-type")
                {
                    sb.AppendFormat("{0}: {1}{2}", f.Key, f.Value, MailParser.NewLine);
                }
            }
            //Headerに設定されていない場合のみセットする
            //Priority
            f = Field.FindField(this.Header, "X-Priority");
            if (f == null)
            {
                sb.AppendFormat("X-Priority: {0}{1}", ((byte)this.Priority).ToString(), MailParser.NewLine);
            }
            //TO
            f = Field.FindField(this.Header, "To");
            if (f == null)
            {
                line = this.CreateMailAddressListText(this._To);
                if (String.IsNullOrEmpty(line) == false)
                {
                    sb.Append("To: ");
                    sb.Append(line);
                }
            }
            //CC
            f = Field.FindField(this.Header, "Cc");
            if (f == null)
            {
                line = this.CreateMailAddressListText(this._Cc);
                if (String.IsNullOrEmpty(line) == false)
                {
                    sb.Append("Cc: ");
                    sb.Append(line);
                }
            }

            if (this.Contents.Count > 0)
            {
                if (String.IsNullOrEmpty(this.MultiPartBoundary) == true)
                {
                    this.MultiPartBoundary = MailParser.GenerateBoundary();
                }
                //Multipartboundary
                sb.AppendFormat("Content-Type: multipart/mixed; boundary=\"{0}\"", this.MultiPartBoundary);
                sb.Append(MailParser.NewLine);
                sb.Append(MailParser.NewLine);

                //This is multi-part message in MIME format.
                sb.Append(MailParser.ThisIsMultiPartMessageInMimeFormat);
                sb.Append(MailParser.NewLine);
                //Add BodyText Content
                if (String.IsNullOrEmpty(this.BodyText) == false)
                {
                    ct = new SmtpContent();
                    if (this.IsHtml == true)
                    {
                        ct.LoadHtml(this.BodyText);
                    }
                    else
                    {
                        ct.LoadText(this.BodyText);
                    }
                    ct.ContentEncoding         = this.ContentEncoding;
                    ct.ContentTransferEncoding = this.ContentTransferEncoding;
                    if (this.Contents.Exists(delegate(SmtpContent c) { return(c.IsBody); }) == false)
                    {
                        sb.Append("--");
                        sb.Append(this.MultiPartBoundary);
                        sb.Append(MailParser.NewLine);
                        sb.Append(ct.Data);
                        sb.Append(MailParser.NewLine);
                    }
                }
                for (int i = 0; i < this._Contents.Count; i++)
                {
                    //Skip empty SmtpContent instance
                    if (String.IsNullOrEmpty(this._Contents[i].Data) == true)
                    {
                        continue;
                    }
                    sb.Append("--");
                    sb.Append(this.MultiPartBoundary);
                    sb.Append(MailParser.NewLine);
                    sb.Append(this.Contents[i].Data);
                    sb.Append(MailParser.NewLine);
                }
                sb.Append(MailParser.NewLine);
                sb.AppendFormat("--{0}--", this.MultiPartBoundary);
            }
            else
            {
                sb.AppendFormat("Content-Type: {0}; charset=\"{1}\"", this.ContentType.Value, this.ContentEncoding.WebName);
                sb.Append(MailParser.NewLine);
                sb.Append(MailParser.NewLine);
                bodyText = MailParser.EncodeToMailBody(this.BodyText, this.ContentTransferEncoding, this.ContentEncoding);
                if (this.ContentTransferEncoding == TransferEncoding.SevenBit)
                {
                    sb.Append(bodyText);
                }
                else
                {
                    for (int i = 0; i < bodyText.Length; i++)
                    {
                        if (i > 0 && i % 76 == 0)
                        {
                            sb.Append(MailParser.NewLine);
                        }
                        //Is current index is first char of line
                        if (i == 0 || (i > 2 && bodyText[i - 2] == '\r' && bodyText[i - 1] == '\n'))
                        {
                            if (bodyText[i] == '.')
                            {
                                sb.Append(".");
                            }
                        }
                        sb.Append(bodyText[i]);
                    }
                }
            }
            sb.Append(MailParser.NewLine);
            sb.Append(MailParser.NewLine);
            sb.Append(".");
            sb.Append(MailParser.NewLine);

            return(sb.ToString());
        }