如何使用 E-Mailrelay 實作 DKIM?

如何使用 E-Mailrelay 實作 DKIM?

我正在嘗試將 DKIM 新增到目前使用 IIS SMTP 發送郵件的現有 Windows 伺服器。

看來這可以透過使用來完成電子郵件中繼。不幸的是,我找不到任何有關如何在電子郵件中繼中準確配置 DKIM 的參考。

我在文件或第三方教程中是否遺漏了一些描述如何執行此操作的內容?

答案1

這裡: https://sourceforge.net/projects/dkimsigningfilter/files/latest/download 是一個基於上面修改後的程式碼編譯的應用程式。安裝程式將網域、選擇器和金鑰路徑儲存到位置 ./config 的 config.txt 檔案中。該程式充當電子郵件中繼的過濾器。

答案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);
}
}

相關內容