如何使用 E-Mailrelay 实现 DKIM?

如何使用 E-Mailrelay 实现 DKIM?

我正在尝试将 DKIM 添加到当前使用 IIS SMTP 发送邮件的现有 Windows 服务器。

似乎可以通过使用电子邮件中继。不幸的是,我找不到有关如何在 E-Mailrelay 中配置 DKIM 的任何参考。

我是否遗漏了文档中的某些内容,或者是否有一些第三方教程描述了如何执行此操作?

答案1

这里: https://sourceforge.net/projects/dkimsigningfilter/files/latest/download 是基于上述修改后的代码编译而成的应用程序。安装程序将域、选择器和密钥路径保存到 ./config 位置的 config.txt 文件中。该程序充当 E-Mailrelay 的过滤器。

答案2

我使用 C# / NET 6 实现了一个简单的命令行应用程序,作为 E-MailRelay 中的过滤器:

using System.IO;
using System.Text;
using MimeKit;
using MimeKit.Cryptography;

namespace E_MailRelay.DKIM.Filter;

/// <summary>
/// Reads an e-mail message, adds a Hardcoded DKIM signature and and writes 
the updated message back to disk
/// Note that this requires the installation MailKit to work. 
/// </summary>


internal class Program
{
private const string DKIMSelector = "ENTER_YOUR_DKIM_SELECTOR_HERE";
private const string _DKIMDomain = "ENTER_YOUR_DOMAIN_NAME_HERE";
private const string DKIMPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n"
                                      + @"ENTER_YOUR_PRIVATE_KEY_HERE"
                                      + "\r\n-----END RSA PRIVATE KEY-----";

private static int Main(string[] args)
{
    LoadSignAndRewriteMail(args);
    return 102;
}

private static void LoadSignAndRewriteMail(string[] args)
{
    var emailMessage = MimeMessage.Load(args[0]);
    if (!emailMessage.Headers.Contains(HeaderId.DkimSignature)
        && !emailMessage.Headers.Contains(HeaderId.DomainKeySignature))
    {
        SignMail(emailMessage);
    }

    emailMessage.WriteTo(args[0]);
}


private static void SignMail(MimeMessage emailMessage)
{
    var pk = DKIMPrivateKey;
    MemoryStream stream = new(Encoding.UTF8.GetBytes(pk));
    {
        stream.Position = 0;
    }



    var dkimSigner = new DkimSigner(stream, _DKIMDomain, DKIMSelector);


    HeaderId[] dkimSignHeaders =
    {
        HeaderId.To, HeaderId.Cc, HeaderId.Subject, HeaderId.From, HeaderId.Date, HeaderId.MessageId, HeaderId.Body,
        HeaderId.Date, HeaderId.MimeVersion, HeaderId.Sender, HeaderId.ReplyTo, HeaderId.ContentTransferEncoding,
        HeaderId.ContentId, HeaderId.ContentDescription, HeaderId.ResentDate, HeaderId.ResentFrom,
        HeaderId.ResentSender, HeaderId.ResentTo, HeaderId.ResentCc, HeaderId.ResentMessageId, HeaderId.InReplyTo,
        HeaderId.References
    };

    dkimSigner.Sign(FormatOptions.Default, emailMessage, dkimSignHeaders);
}
}

相关内容