示例#1
0
        private string GenerateBody()
        {
            if (string.IsNullOrEmpty(this.Body))
            {
                throw new InvalidOperationException("message.Body is empty");
            }

            //编码完成
            var body = Convert.ToBase64String(this.encoding.GetBytes(this.Body));


            var sb = new StringBuilder();

            //begin
            sb.Append("--");
            sb.Append(this.boundary);
            sb.Append(MailCommon.NewLine);  //分隔完成

            if (this.IsBodyHtml == true)
            {
                sb.Append("Content-Type: text/html; charset=" + this.encoding.HeaderName);
            }
            else
            {
                sb.Append("Content-Type: text/plain; charset=" + this.encoding.HeaderName);
            }
            sb.Append(MailCommon.NewLine);
            sb.Append("Content-Transfer-Encoding: base64");
            sb.Append(MailCommon.NewLine);
            sb.Append(MailCommon.NewLine);
            sb.Append(MailCommon.Line76Break(body));
            sb.Append(MailCommon.NewLine);
            sb.Append(MailCommon.NewLine);

            //end
            // "--" + this.Boundary + "--" + MailCommon.NewLine;
            sb.Append("--");
            sb.Append(this.Boundary);
            sb.Append("--");
            sb.Append(MailCommon.NewLine);

            return(sb.ToString());
        }
示例#2
0
文件: DKIM.cs 项目: aooshi/adf
        /// <summary>
        /// DKIM message
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public string Sign(MailMessage message)
        {
            //http://dkimcore.org/specification.html#1
            //DKIM-Signature: v=1;a=rsa-sha256;bh=BODYHASH;c=relaxed;d=TOKEN;h=HEADERS;s=SELECTOR;b=SIGNATURE

            //http://www.dkim.org/specs/rfc4871-dkimbase.html#dkim-sig-hdr

            /*
             * DKIM-Signature: v=1; a=rsa-sha256; c=simple/relaxed; d=domain.com;
             * h=From:To:Subject:Date; q=dns/txt; s=newsletter17100; t=1510020630;
             * bh=+5tUEqyyuolGsLc9usglUSkelpZtxEkEWKaGC21PSEM=;
             * b=
             */

            var dsLength = "DKIM-Signature: ".Length;

            var build = new StringBuilder(128);

            build.Append("DKIM-Signature:");
            build.Append(" v=1;");


            //
            string algorithm = this.algorithm == DKIMAlgorithm.RSASha1 ? "rsa-sha1" : "rsa-sha256";

            build.Append(" a=").Append(algorithm).Append(';');

            //c=header type / bodey type;
            string htype = this.headerType == DKIMType.Simple ? "simple" : "relaxed";
            string bType = this.bodyType == DKIMType.Simple ? "simple" : "relaxed";

            //build.Append(" c=simple/relaxed;");
            build.Append(" c=").Append(htype).Append('/').Append(bType).Append(';').Append(MailCommon.NewLine);
            //create time
            long unixtime = Adf.UnixTimestampHelper.ToInt64Timestamp();

            build.Append("\tt=").Append(unixtime).Append(';');
            build.Append(" q=dns/txt;").Append(MailCommon.NewLine);
            // domain
            build.Append("\td=").Append(domain).Append(';');
            // DNS selector
            build.Append(" s=").Append(selector).Append(';').Append(MailCommon.NewLine);

            //headers
            build.Append("\th=").Append(string.Join(":", this.signHeaders)).Append(';').Append(MailCommon.NewLine);

            //body hash
            var body = "";

            if (this.bodyType == DKIMType.Relaxed)
            {
                body = this.RelaxedBodyCanonicalization(message.GetBody());
            }
            else
            {
                body = this.SimpleBodyCanonicalization(message.GetBody());
            }

            var bodyBytes  = message.Encoding.GetBytes(body);
            var bodySigned = this.Hash(bodyBytes);
            var bodyHash   = Convert.ToBase64String(bodySigned);

            build.Append("\tbh=").Append(bodyHash).Append(';').Append(MailCommon.NewLine);
            //b
            build.Append("\tb=");

            //build string

            /*
             * DKIM-Signature: v=1; a=rsa-sha256; c=simple/relaxed; d=caping.co.id;
             * s=newsletter17800; q=dns/txt; t=1510023325; h=From:To:Subject:Date;
             * h=yklAmb1CNh0SAfmrk97PlZTEHrGKV94Ps6R7KDwLLoo=;
             * b=
             */

            //set current DKIM-Signature
            var dkimHead = build.ToString(dsLength, build.Length - dsLength);

            message.OutputHeaders["DKIM-Signature"] = dkimHead;

            var headers = "";

            if (this.headerType == DKIMType.Relaxed)
            {
                headers = this.RelaxedHeaderCanonicalization(message.OutputHeaders);
            }
            else
            {
                headers = this.SimpleHeaderCanonicalization(message.OutputHeaders);
            }
            var headerBytes  = message.Encoding.GetBytes(headers);
            var headerSigned = this.Sign(headerBytes);

            // assumes signature ends with "b="
            var b = Convert.ToBase64String(headerSigned);

            b = MailCommon.Line76Break("b=" + b, "\t");


            //remove last b
            build.Length -= "\tb=".Length;
            //append b
            build.Append(b);


            //update DKIM-Signature
            dkimHead = build.ToString(dsLength, build.Length - dsLength);
            message.OutputHeaders["DKIM-Signature"] = dkimHead;

            var signed = build.ToString();

            return(signed);
        }