public static MailMessage DkimSign([NotNull] this MailMessage message, DkimSigner signer) { if (message == null) { throw new ArgumentNullException("message"); } if (signer == null) { throw new ArgumentNullException("signer"); } message.BodyEncoding = signer.Encoding; message.SubjectEncoding = signer.Encoding; message.HeadersEncoding = signer.Encoding; // get email content and generate initial signature var email = Email.Parse(message.GetText()); if (!CanSign(email)) { throw new InvalidOperationException("Unable to Domain Key sign the message"); } var value = signer.GenerateDkimHeaderValue(email); // signature value get formatted so add dummy signature value then remove it message.Headers.Prepend(DkimSigner.SignatureKey, value + new string('0', 70)); email = message.Parse(); var formattedSig = email.Headers[DkimSigner.SignatureKey].Value; email.Headers[DkimSigner.SignatureKey].Value = formattedSig.Substring(0, formattedSig.Length - 70); // sign email value += signer.GenerateSignature(email); message.Headers.Set(DkimSigner.SignatureKey, value); return(message); }
public static MailMessage DkimSign([NotNull]this MailMessage message, DkimSigner signer) { if (message == null) { throw new ArgumentNullException("message"); } if (signer == null) { throw new ArgumentNullException("signer"); } message.BodyEncoding = signer.Encoding; message.SubjectEncoding = signer.Encoding; message.HeadersEncoding = signer.Encoding; // get email content and generate initial signature var email = Email.Parse(message.GetText()); if (!CanSign(email)) { throw new InvalidOperationException("Unable to Domain Key sign the message"); } var value = signer.GenerateDkimHeaderValue(email); // signature value get formatted so add dummy signature value then remove it message.Headers.Prepend(DkimSigner.SignatureKey, value + new string('0', 70)); email = message.Parse(); var formattedSig = email.Headers[DkimSigner.SignatureKey].Value; email.Headers[DkimSigner.SignatureKey].Value = formattedSig.Substring(0, formattedSig.Length - 70); // sign email value += signer.GenerateSignature(email); message.Headers.Set(DkimSigner.SignatureKey, value); return message; }