Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Sign Javamail with DKIM

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?

like image 356
ryandlf Avatar asked Dec 12 '12 05:12

ryandlf


People also ask

What is JavaMail API?

The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.


1 Answers

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 java-utils-mail-dkim (GitHub), which is an active 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 old.

like image 103
Benny Bottema Avatar answered Nov 08 '22 06:11

Benny Bottema