dmcgiv / DKIM.Net

DomainKeys Identified Mail (DKIM) and DomainKey email signing for .Net (C#)
30 stars 9 forks source link

Exception on DKIM.MailMessageExtensions.DkimSign #8

Open isendrak opened 9 years ago

isendrak commented 9 years ago

Hi there, found this really promising project, after almost smashing my keyboard against the next wall, while searching for a way to DKIM-Sign emails in C#. Compiling works perfectly (not a single warning/error), but when i try to execute even a simple program, like the example one from the readme, it gives me an exception when calling the DkimSign-method. Here's, what it says:

Unhandled Exception:
System.TypeInitializationException: An exception was thrown by the type initializer for DKIM.MailMessageText ---> System.NullReferenceException: Object reference not set to an instance of an object
  at DKIM.MailMessageText..cctor () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at DKIM.MailMessageExtensions.DkimSign (System.Net.Mail.MailMessage message, DKIM.DkimSigner signer) [0x00000] in <filename unknown>:0 
  at Sendmail.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for DKIM.MailMessageText ---> System.NullReferenceException: Object reference not set to an instance of an object
  at DKIM.MailMessageText..cctor () [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at DKIM.MailMessageExtensions.DkimSign (System.Net.Mail.MailMessage message, DKIM.DkimSigner signer) [0x00000] in <filename unknown>:0 
  at Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 

Any idea? P.S.: Here's an example of where exactly the exception occurs:

using System.Net.Mail;
using DKIM;
static class Program{
    public static void Main(string[] args){
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("sender@domain.org");
        mail.To.Add(new MailAddress("receiver@another-domain.com"));
        mail.Subject="DKIM.Net Test";
        mail.Body="Some stuff about stuff and such...";
        IPrivateKeySigner pks = PrivateKeySigner.Create(@"-----BEGIN RSA PRIVATE KEY-----
<The private key goes here>
-----END RSA PRIVATE KEY-----
");
        DkimSigner dkim = new DkimSigner(pks, "domain.org", "dkim", new string[] { "From", "To", "Subject" });
        mail.DkimSign(dkim); //And here the exception goes off...
    }
}
jstedfast commented 9 years ago

It looks like the MailMessageText static constructor is failing due to a null reference somewhere.

My guess is that since MailMessageText relies heavily on reflecting various non-public types from the BCL, that this is the problem.

I've just added support for DKIM signatures to MimeKit that you can use instead which does not rely on any non-public BCL classes because I've implemented my own MIME parser and serializer.

Since you'll probably need to send the message via SMTP, you'll probably also want to take a look at MailKit.

Hopefully Damien doesn't mind me making this suggestion.