How to Sign Javamail with DKIM

后端 未结 2 1360
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:32

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

提交回复
热议问题