MailBee.NET Objects 7.2

DomainKeys.Verify Method (MailMessage, Smtp)

Verifies the DomainKeys and DKIM signatures of the mail message.

public DomainKeysVerifyResult Verify(
   MailMessage msg,
   Smtp dnsRequestor
);

Parameters

msg
The mail message to be verified.
dnsRequestor
The instance of Smtp class to be used for making DNS queries.

Return Value

The result of the verification.

Remarks

In order to verify DomainKeys/DKIM signature of e-mails, this method has to make DNS queries to the DomainKeys server of the sender. MailBee uses Smtp class for this. Thus, you must have the appropriate license for Smtp component. Or, you can use DomainKeysVerify which is simpler to use and does not require Smtp license (but it's more limited in features).

There is a chance your mail server may already check DomainKeys/DKIM signatures of incoming e-mails. If it's so, you should simply check extra headers which contain DomainKeys/DKIM checking results and get added by your mail server to the message. For instance, Gmail adds "Authentication-Results" header for that purpose.

Note   This method may take significant time to execute because it needs to make a network operation - DNS query. Also, if there is a problem with the DNS server, it may fail (return DnsQueryFailed) even if the DomainKeys signature itself is correct. Also, if the message has both classic DomainKeys (DK) and newer DKIM signatures, the method will verify both of them to check if they match (this may double the execution time in case if DNS information for DK and DKIM records of the sending domain is different). If you wish to check only one type of DomainKeys signature, use Verify overload.

This method is thread-safe: you can call Verify method for the same instance of DomainKeys class to speed up validating signatures of multiple e-mails. However, you should pass separate instance of Smtp class as dnsRequestor value for each Verify method call.

Note   If the message contains only DKIM signature created using SHA256 algorithm and the operating system does not support this algorithm (the OS is older than WinXP SP3), the method will return Sha256NotSupported. All DK signatures and those DKIM signatures which use SHA1 algorithm are fully supported on all Windows platforms.

Exceptions

Exception TypeCondition
MailBeeInvalidArgumentExceptionmsg or dnsRequestor is a null reference (Nothing in Visual Basic) or dnsRequestor.DnsServers collection is empty.
MailBeeExceptionAn error occurred and ThrowExceptions is true.

Example

This sample loads an e-mail message from file, prepares Smtp object for making DNS queries and verifies that message.

[C#]
using System;
using MailBee;
using MailBee.Mime;
using MailBee.Security;
using MailBee.SmtpMail;

class Sample
{
    static void Main(string[] args)
    {
        // Load the message from file (we could also
        // get if from the mail server or elsewhere).
        MailMessage msg = new MailMessage();
        msg.LoadMessage(@"C:\Docs\msg.eml");

        // Prepare Smtp instance for making DNS queries.
        // We assume Smtp license key is already set in config file.
        Smtp mailer = new Smtp();
        mailer.DnsServers.Autodetect();

        // Verify DomainKeys signature.
        DomainKeys dk = new DomainKeys();
        DomainKeysVerifyResult dkResult = dk.Verify(msg, mailer);
        Console.WriteLine(dkResult.ToString());
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.Mime
Imports MailBee.Security
Imports MailBee.SmtpMail

Module Sample
    Sub Main(ByVal args As String())
        ' Load the message from file (we could also
        ' get if from the mail server or elsewhere).
        Dim msg As MailMessage = New MailMessage
        msg.LoadMessage("C:\Docs\msg.eml")

        ' Prepare Smtp instance for making DNS queries.
        ' We assume Smtp license key is already set in config file.
        Dim mailer As Smtp = New Smtp
        mailer.DnsServers.Autodetect()

        ' Verify DomainKeys signature.
        Dim dk As DomainKeys = New DomainKeys
        Dim dkResult As DomainKeysVerifyResult = dk.Verify(msg, mailer)
        Console.WriteLine(dkResult.ToString())
    End Sub
End Module

See Also

DomainKeys Class | MailBee.Security Namespace | DomainKeys.Verify Overload List