如何使用DKIM签名Javamail [英] How to Sign Javamail with DKIM
问题描述
是否有一个库或没有外部库的方法?我正在使用apache james作为我的邮件服务器,目前正在发送这样的电子邮件:
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();
}
}
但是我想事先与DKIM签署电子邮件.我知道我需要在james服务器中实现DKIM签名并计划使用jDKIM来做到这一点,我也理解我需要使用www.port25.com之类的东西来创建密钥,但是我之前如何在Java中实际对电子邮件进行签名我发出去了吗?
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?
推荐答案
我最终使用了DKIM for Javamail,可以从以下位置下载该文件: DKIM for Javamail
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();
}
}
}
这篇关于如何使用DKIM签名Javamail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!