java 如何使用 DKIM 签署 Javamail
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13833098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Sign Javamail with DKIM
提问by ryandlf
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:
有没有图书馆或没有外部图书馆的方法来做到这一点?我使用 apache james 作为我的邮件服务器,目前发送这样的电子邮件:
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?
但我想事先用 DKIM 签署电子邮件。我知道我需要实现 DKIM 登录到 james 服务器并计划使用 jDKIM 来执行此操作,我也知道我需要使用诸如 www.port25.com 之类的东西创建密钥,但是我之前如何在 java 中实际签署电子邮件我发出来?
采纳答案by ryandlf
I ended up using DKIM for Javamail which can be downloaded at: DKIM For Javamail
我最终使用了 DKIM for Javamail,可以在以下位置下载: 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();
}
}
}
回答by Benny Bottema
Simple Java Mailrecently added support for DKIM signing. Here's your code, but now with Simple Java Mail:
Simple Java Mail最近添加了对 DKIM 签名的支持。这是您的代码,但现在使用 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[]
.
私有密钥参数可以是一个File
,InputStream
或者一个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.
有趣的是,在幕后 Simple Java Mail 使用java-utils-mail-dkim(GitHub),它是休眠的DKIM-for-JavaMail(GitHub)上的一个活跃分支,它是您现在使用的库DKIM的延续对于 Javamail(SourceForge)。所以,你使用的那个已经很旧了。