Java Requires Chilkat v11.0.0+
Java
Sign an email with a key in the Windows Current User certificate store
See more DKIM Examples
Run on Windows as the account that owns the signing certificate. Copy its thumbprint from Current User > Personal and supply it as hexadecimal digits. The thumbprint selects a certificate. The DKIM signature itself uses RSA-SHA256.
The certificate needs an accessible, exportable RSA private key for GetPrivateKey. Publish its matching public key under the selector in DNS. DKIM recipients use that DNS key to verify the message. An X.509 chain is unnecessary for this step.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.CertStore;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Dkim;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.PrivateKey;
public class ChilkatExample {
public static void main(String[] args) {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CertStore store = new CertStore();
try {
store.openCurrentUserStore(true);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject search = new JsonObject();
search.updateString("thumbprint", "YOUR_CERTIFICATE_THUMBPRINT");
Cert cert = new Cert();
try {
store.findCert(search, cert);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
if (!cert.hasPrivateKey()) {
System.out.println("The selected certificate has no accessible private key.");
return;
}
PrivateKey privateKey = new PrivateKey();
try {
cert.getPrivateKey(privateKey);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Dkim signer = new Dkim();
signer.setDkimDomain("example.com");
signer.setDkimSelector("mail2026");
signer.setDkimAlg("rsa-sha256");
try {
signer.setDkimPrivateKey(privateKey);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
BinData message = new BinData();
try {
message.loadFile("message.eml");
} catch (ChilkatException e) {
System.out.println("Could not read message.eml.");
return;
}
try {
signer.dkimSign(message);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
try {
message.writeFile("signed.eml");
} catch (ChilkatException e) {
System.out.println("Could not write signed.eml.");
return;
}
System.out.println("Saved signed.eml.");
}
}