Class DkimSigner

java.lang.Object
com.codename1.backend.mail.DkimSigner

public final class DkimSigner extends Object

Signs outgoing mail with DKIM (RFC 6376): rsa-sha256, relaxed header and relaxed body canonicalization.

The receiver finds the public key in DNS, in a TXT record at <selector>._domainkey.<domain>. Publish it before turning signing on.

With cn1.mail.dkim.domain, cn1.mail.dkim.selector and cn1.mail.dkim.privateKey set, every mail Mailers sends is signed, over SMTP and SES alike. The key is PEM text or the path of a PEM file.

DkimSigner signer = new DkimSigner("example.com", "mail", pem);
byte[] signed = signer.sign(message);
  • Constructor Details

    • DkimSigner

      public DkimSigner(String domain, String selector, String pemPrivateKey) throws IOException
      A signer for domain whose public key is published under selector.
      Parameters:
      domain - the signing domain, the d= tag
      selector - the key's selector, the s= tag
      pemPrivateKey - an RSA private key in PEM, PKCS#8 or PKCS#1
      Throws:
      IOException - when the key cannot be read
    • DkimSigner

      public DkimSigner(String domain, String selector, String pemPrivateKey, String[] headers) throws IOException
      A signer that signs the named headers, the ones present, in this order.
      Parameters:
      domain - the signing domain, the d= tag
      selector - the key's selector, the s= tag
      pemPrivateKey - an RSA private key in PEM, PKCS#8 or PKCS#1
      headers - the names of the headers to sign
      Throws:
      IOException - when the key cannot be read
  • Method Details

    • configured

      public static DkimSigner configured(String domain, String selector, String key) throws IOException
      The signer the cn1.mail.dkim.* settings describe, or null when none of them is set.
      Parameters:
      domain - cn1.mail.dkim.domain
      selector - cn1.mail.dkim.selector
      key - cn1.mail.dkim.privateKey: PEM text, or the path of a PEM file
      Returns:
      the signer, or null
      Throws:
      IOException - when only some of the three are set, or the key cannot be read
    • getDomain

      public String getDomain()
      The domain mail is signed for, the d= tag.
      Returns:
      the domain
    • getSelector

      public String getSelector()
      The selector, the s= tag.
      Returns:
      the selector
    • sign

      public byte[] sign(byte[] rawMessage) throws IOException
      rawMessage with a DKIM-Signature header in front of it. The message must use CRLF line ends, as MimeWriter writes them.
      Parameters:
      rawMessage - the message as it will be sent
      Returns:
      the signed message
      Throws:
      IOException - when signing fails