How to Sign Javamail with DKIM

后端 未结 2 1369
Happy的楠姐
Happy的楠姐 2021-02-06 02:02

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 sen         


        
2条回答
  •  逝去的感伤
    2021-02-06 02:14

    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 on 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.

提交回复
热议问题