Пример #1
0
        ///// <summary>
        ///// 将一个数组合并成符合邮件的每行最大76字符字符串,结果追加至字符构建对象
        ///// </summary>
        ///// <param name="list"></param>
        ///// <param name="builder"></param>
        ///// <param name="lineStart">每行初始字符</param>
        //public static void JoinLine76Break(StringBuilder builder, IList<string> list, string lineStart)
        //{
        //    var lineLength = builder.Length;
        //    var lineStartLength = lineStart.Length;

        //    if (lineStartLength > 76)
        //    {
        //        throw new ArgumentOutOfRangeException("lineStart", "length limit less than 76.");
        //    }

        //    for (int i = 0, l = list.Count; i < l; i++)
        //    {
        //        var itemLength = list[i].Length;
        //        if (lineLength + itemLength > 76)
        //        {
        //            builder.Append(MailCommon.NewLine);
        //            builder.Append(lineStart);

        //            lineLength = itemLength + lineStartLength;
        //        }
        //        else
        //        {
        //            lineLength += itemLength;
        //        }
        //        builder.Append(list[i]);
        //    }
        //}

        /// <summary>
        /// 编码邮件地址头
        /// </summary>
        /// <param name="name"></param>
        /// <param name="address"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Base64EncodeAddress(string name, string address, Encoding encoding)
        {
            //判断姓名是否为空
            if (string.IsNullOrEmpty(name))
            {
                name = address.Split('@')[0];
            }

            //判断姓名是否为键盘可打印,如果是,则不进行编码,如果不是,则进行编码
            if (MailCommon.IsAscii(name) == false)
            {
                name = MailCommon.Base64EncodHead(name, encoding);
            }

            return("\"" + name + "\" <" + address + ">");
        }
Пример #2
0
        /// <summary>
        /// 获取邮件头,并填充 OutputHeaders
        /// </summary>
        /// <exception cref="InvalidOperationException">message.From invalid or message.To is empty or subject is empty</exception>
        /// <returns></returns>
        public string GetHead()
        {
            if (this.From == null || string.IsNullOrEmpty(this.From.Address))
            {
                throw new InvalidOperationException("message.From invalid");
            }

            if (this.To == null || this.To.Count == 0)
            {
                throw new InvalidOperationException("message.To is empty");
            }

            if (string.IsNullOrEmpty(this.Subject))
            {
                throw new InvalidOperationException("subject is empty");
            }

            this.outputHeaders = new NameValueCollection(15 + this.headers.Count);

            StringBuilder sb = new StringBuilder();

            //message id
            var messageId = "<" + this.messageId + ">";

            sb.Append("Message-ID: ");
            sb.Append(messageId);
            sb.Append(MailCommon.NewLine);
            outputHeaders["Message-ID"] = messageId;

            //from
            var from = MailCommon.Base64EncodeAddress(this.From.Name, this.From.Address, this.encoding);

            sb.Append("From: ");
            sb.Append(from);
            sb.Append(MailCommon.NewLine);
            outputHeaders["From"] = from;

            //replay
            if (this.ReplyTo != null)
            {
                var replayTo = MailCommon.Base64EncodeAddress(this.ReplyTo.Name, this.ReplyTo.Address, this.encoding);
                sb.Append("Replay-To: ");
                sb.Append(replayTo);
                sb.Append(MailCommon.NewLine);
                outputHeaders["Replay-To"] = replayTo;
            }
            //to
            var to = MailCommon.JoinAddressList(this.to, this.encoding);

            sb.Append("To: ");
            sb.Append(to);
            sb.Append(MailCommon.NewLine);
            outputHeaders["To"] = to;
            //cc
            if (this.cc != null && this.cc.Count > 0)
            {
                var cc = MailCommon.JoinAddressList(this.cc, this.encoding);
                sb.Append("CC: ");
                sb.Append(cc);
                sb.Append(MailCommon.NewLine);
                outputHeaders["CC"] = cc;
            }
            //Subject
            var subject = "";

            sb.Append("Subject: ");
            if (MailCommon.IsAscii(this.Subject))
            {
                subject = this.Subject;
            }
            else
            {
                subject = MailCommon.Base64EncodHead(this.Subject, this.encoding);
            }
            sb.Append(subject);
            sb.Append(MailCommon.NewLine);
            outputHeaders["Subject"] = subject;

            //Data
            var date = DateTime.UtcNow.ToString("R");

            sb.Append("Date: ");
            sb.Append(date);
            sb.Append(MailCommon.NewLine);
            outputHeaders["Date"] = date;

            //Mime
            sb.Append(MailCommon.MimeVersion);
            sb.Append(MailCommon.NewLine);
            outputHeaders["MIME-Version"] = "1.0";

            //priority
            if (this.Priority != MailPriority.Normal)
            {
                sb.Append("X-Priority: ");
                sb.Append((int)this.Priority);
                sb.Append(MailCommon.NewLine);
                outputHeaders["X-Priority"] = ((int)this.Priority).ToString();
            }
            //Mailer
            sb.Append(MailCommon.Mailer);
            sb.Append(MailCommon.NewLine);
            outputHeaders["X-Mailer"] = "http://www.aooshi.org/adf/";

            //Content-Type
            //sb.Append(CreateContentType());
            var contentType = "multipart/alternative;\r\n\tboundary=\"" + this.boundary + "\"";

            sb.Append("Content-Type: ");
            sb.Append(contentType);
            sb.Append(MailCommon.NewLine);
            outputHeaders["Content-Type"] = contentType;

            //encoding
            sb.Append("Content-Transfer-Encoding: base64");
            sb.Append(MailCommon.NewLine);
            outputHeaders["Content-Transfer-Encoding"] = "base64";

            //custom headers
            foreach (var key in this.headers.AllKeys)
            {
                sb.Append(key);
                sb.Append(": ");
                sb.Append(this.headers[key]);
                sb.Append(MailCommon.NewLine);

                //
                outputHeaders[key] = this.headers[key];
            }

            //dkim
            var dkim = this.dkim;

            if (dkim != null)
            {
                var dkimValue = dkim.Sign(this);
                //
                sb.Insert(0, dkimValue + MailCommon.NewLine);
            }

            //
            sb.Append(MailCommon.NewLine);

            return(sb.ToString());
        }