lemta
lemta

Reputation: 211

Java create DKIM with jDKIM

Can you give me an example for create DKIM with jDKIM? I don't see any example for this.

http://james.apache.org/jdkim/

Thanks!

Upvotes: 2

Views: 1130

Answers (2)

Victor Marcus
Victor Marcus

Reputation: 146

Use: james-jdkim Have a look at this code, This is the answer you are looking for: lima here is the selector and domain is yahoogroups.com

PublicKeyRecordRetriever publicKeyRecordRetriever = new DNSPublicKeyRecordRetriever();
PublicKeyRecord keys = new DKIMVerifier()
            .publicKeySelector(publicKeyRecordRetriever.getRecords("dns/txt", "lima", "yahoogroups.com"));
String signedMIME = "DKIM-Signature: a=rsa-sha256; b=xxxxxxxxxxxxxxxxxx; s=lima; d=yahoogroups.com; v=1; bh=xxxxxxxxx; h=from:to;"
            + "From: [email protected]" + "To: [email protected]" + "body";
try {
        // Verify message against public key
    new DKIMVerifier(publicKeyRecordRetriever).verify(new ByteArrayInputStream(signedMIME.getBytes()));
} catch (Exception e) {
    throw e;
}

Upvotes: 1

Have a look if this helps. I am also starting with jDKIM. I just wrote a method to list the signature records for the input message. Puzzled how to parse the signature records.

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.james.jdkim.DKIMVerifier;
import org.apache.james.jdkim.api.SignatureRecord;
import org.apache.james.jdkim.exceptions.FailException;
import org.apache.mailet.Mail;  
import org.apache.mailet.Mailet;
public class AlgorithmDkimVerification {

    public List<SignatureRecord> verifyDkim(InputStream messageStream)
    {
        DKIMVerifier verifier = new DKIMVerifier();
        List<SignatureRecord> records = null;
        try {
            records = verifier.verify(messageStream);
        } catch (IOException | FailException e) {
            e.printStackTrace();
        }
        System.out.println(records);
        return records;
    }

}

Upvotes: 0

Related Questions