How to sign Javamail with DKIM

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

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 would like to sign a letter with DKIM before starting work. I understand that I need to implement the DKIM signature on the james server and plan on using jDKIM for this, I also understand that I need to create keys using something like www.port25.com, but how can I actually sign the email in java before I send it?

+6
source share
2 answers

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

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

 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(); } } } 
+3
source

Simple Java mail recently added DKIM signature support. Here is 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 may be File , InputStream or byte[] .

Interestingly, behind the scenes, Simple Java Mail uses java-utils-mail-dkim (GitHub), which is the active fork on the inactive DKIM-for-JavaMail (GitHub), which was a continuation of the library you are currently using, DKIM for Javamail (SourceForge ) So the one you are using is very old.

+2
source

All Articles