How to Sign Javamail with DKIM
Asked Answered
K

2

11

Is there a library or a way to do this without an external library? I am using apache james as my mail server and currently send email like this:

public void sendMessage(String to, String subject, String content) {
    MimeMessage message = new MimeMessage(session);
    try {
        message.addRecipients(Message.RecipientType.TO, to);
        message.setFrom(new InternetAddress(from));
        message.setSubject(subject);
        message.setContent(content, "text/html; charset=utf-8");
        Transport.send(message);
    } catch (MessagingException e) {
        e.printStackTrace();
    }       
}

But i'd like to sign the email with DKIM before hand. I understand I need to implement DKIM signing into the james server and plan on use jDKIM to do this, I also understand I need to create the keys using something like www.port25.com, but how do I actually sign the email in java before I send it out?

Kirstinkirstyn answered 12/12, 2012 at 5:1 Comment(0)
K
4

I ended up using DKIM for Javamail which can be downloaded at: DKIM For Javamail

Here is an example (Its pretty well documented in the examples in the download):

public void sendMessage(String to, String subject, String content) {
    //Create DKIM Signer
    DKIMSigner dkimSigner = null;
    try {
        dkimSigner = new DKIMSigner(properties.getProperty("mail.smtp.dkim.signingdomain"), properties.getProperty("mail.smtp.dkim.selector"), properties.getProperty("mail.smtp.dkim.privatekey"));
        dkimSigner.setIdentity(properties.getProperty("mail.user") + "@" + properties.getProperty("mail.smtp.dkim.signingdomain"));
        dkimSigner.setHeaderCanonicalization(Canonicalization.SIMPLE);
        dkimSigner.setBodyCanonicalization(Canonicalization.RELAXED);
        dkimSigner.setLengthParam(true);
        dkimSigner.setSigningAlgorithm(SigningAlgorithm.SHA1withRSA);
        dkimSigner.setZParam(true);
    } catch (Exception e) {
    e.printStackTrace();
        }
    if(dkimSigner != null) {
        //Create message
        Message message = new SMTPDKIMMessage(session, dkimSigner);
        try {
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            message.setFrom(new InternetAddress(from));
            message.setSubject(subject);
            message.setContent(content, "text/html; charset=utf-8");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }   
    }           
}
Kirstinkirstyn answered 12/12, 2012 at 6:58 Comment(3)
What are the advantages of using this as opposed to setting up DKIM on the mail server, such as openDKIM with Postfix?Ocam
@Ocam The advantage is you can use multiple MTAs without having to configure openDKIM for each one of themCuff
This example uses a very old version of DKIM for Java on Source Forge. It has since been moved to GitHub and became inactive. Later still it was forked to a still active project called java-utils-mail-dkim. You can see it in action in my other answer here.Saturday
S
12

Simple Java Mail recently added support for DKIM signing. Here's your code, but now with Simple Java Mail:

public void sendMessage(String to, String subject, String content) {
    final Email email = new Email.Builder()
            .from(null, from)
            .to(null, to)
            .subject(subject)
            .textHTML(content)
            .build();

    email.signWithDomainKey(new File(properties.getProperty("mail.smtp.dkim.privatekey")),
                            properties.getProperty("mail.smtp.dkim.signingdomain"),
                            properties.getProperty("mail.smtp.dkim.selector"));

    new Mailer(...).sendMail(email);
}

The private key argument can be a File, InputStream or a byte[].

Interestingly, Behind the scenes Simple Java Mail uses its own fork of java-utils-mail-dkim (GitHub), which is an archived fork of the dormant DKIM-for-JavaMail (GitHub), which was the continuation of the library you are using now, DKIM For Javamail (SourceForge). So, the one you are using is very very old.

Saturday answered 11/5, 2016 at 11:24 Comment(1)
I just used java-utils-mail-dkim and it works great - thanks for making me aware of it.Bergman
K
4

I ended up using DKIM for Javamail which can be downloaded at: DKIM For Javamail

Here is an example (Its pretty well documented in the examples in the download):

public void sendMessage(String to, String subject, String content) {
    //Create DKIM Signer
    DKIMSigner dkimSigner = null;
    try {
        dkimSigner = new DKIMSigner(properties.getProperty("mail.smtp.dkim.signingdomain"), properties.getProperty("mail.smtp.dkim.selector"), properties.getProperty("mail.smtp.dkim.privatekey"));
        dkimSigner.setIdentity(properties.getProperty("mail.user") + "@" + properties.getProperty("mail.smtp.dkim.signingdomain"));
        dkimSigner.setHeaderCanonicalization(Canonicalization.SIMPLE);
        dkimSigner.setBodyCanonicalization(Canonicalization.RELAXED);
        dkimSigner.setLengthParam(true);
        dkimSigner.setSigningAlgorithm(SigningAlgorithm.SHA1withRSA);
        dkimSigner.setZParam(true);
    } catch (Exception e) {
    e.printStackTrace();
        }
    if(dkimSigner != null) {
        //Create message
        Message message = new SMTPDKIMMessage(session, dkimSigner);
        try {
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            message.setFrom(new InternetAddress(from));
            message.setSubject(subject);
            message.setContent(content, "text/html; charset=utf-8");
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }   
    }           
}
Kirstinkirstyn answered 12/12, 2012 at 6:58 Comment(3)
What are the advantages of using this as opposed to setting up DKIM on the mail server, such as openDKIM with Postfix?Ocam
@Ocam The advantage is you can use multiple MTAs without having to configure openDKIM for each one of themCuff
This example uses a very old version of DKIM for Java on Source Forge. It has since been moved to GitHub and became inactive. Later still it was forked to a still active project called java-utils-mail-dkim. You can see it in action in my other answer here.Saturday

© 2022 - 2024 — McMap. All rights reserved.