Пример #1
0
        public void LoadData(String filePath)
        {
            FileInfo fi = null;

            Byte[]     b   = null;
            FileStream fsm = 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.ContentTransferEncoding     = TransferEncoding.Base64;
            this.ContentDisposition.Value    = "attachment";

            b = new Byte[fi.Length];
            using (fsm = new FileStream(filePath, FileMode.Open))
            {
                fsm.Read(b, 0, b.Length);
                this.BodyText = Convert.ToBase64String(b);
                fsm.Close();
            }
        }
Пример #2
0
 static SmtpContent()
 {
     SmtpContent.InitializeFileExtenstionContentType();
 }
Пример #3
0
        public String GetDataText()
        {
            StringBuilder sb       = new StringBuilder();
            CultureInfo   ci       = CultureInfo.CurrentCulture;
            Field         f        = null;
            SmtpContent   ct       = null;
            String        line     = "";
            String        bodyText = "";

            if (this.IsMultiPart == false &&
                this.Contents.Count > 0)
            {
                this.ContentType.Value = "multipart/mixed";
            }


            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);
                }
            }

            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);
                }
            }

            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.IsMultiPart == true)
            {
                if (String.IsNullOrEmpty(this.MultiPartBoundary) == true)
                {
                    this.MultiPartBoundary = MailParser.GenerateBoundary();
                }

                if (String.IsNullOrEmpty(this.BodyText) == false)
                {
                    ct                         = new SmtpContent();
                    ct.BodyText                = this.BodyText;
                    ct.ContentEncoding         = this.ContentEncoding;
                    ct.ContentTransferEncoding = this.ContentTransferEncoding;
                    if (this.Contents.Exists(delegate(SmtpContent c) { return(c.IsBody); }) == false)
                    {
                        this.Contents.Insert(0, ct);
                    }
                }


                sb.AppendFormat("Content-Type: {0}; boundary=\"{1}\"", this.ContentType.Value, this.MultiPartBoundary);
                sb.Append(MailParser.NewLine);
                sb.Append(MailParser.NewLine);


                sb.Append(MailParser.ThisIsMultiPartMessageInMIMEFormat);
                sb.Append(MailParser.NewLine);
                for (int i = 0; i < this._Contents.Count; i++)
                {
                    sb.Append("--");
                    sb.Append(this.MultiPartBoundary);
                    sb.Append(MailParser.NewLine);
                    sb.Append(this.Contents[i].GetDataText());
                    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);
                        }

                        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());
        }