Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add support for multiple version of MailMessage internal Send method

  • Loading branch information...
commit 120ada02a7da74b1ff6c366adc5327bc99adbd90 1 parent a7b8a93
Damien McGivern authored

Showing 1 changed file with 37 additions and 9 deletions. Show diff stats Hide diff stats

  1. +37 9 src/DKIM.Net/MailMessage/MailMessageText.cs
46 src/DKIM.Net/MailMessage/MailMessageText.cs
@@ -10,15 +10,16 @@ namespace DKIM
10 10 public static class MailMessageText
11 11 {
12 12 private static readonly Func<Stream, object> MailWriterFactory;
13   - private static readonly Action<MailMessage, object, bool, bool> Send;
  13 + private static readonly Action<MailMessage, object, bool, bool> Send3;
  14 + private static readonly Action<MailMessage, object, bool> Send2;
14 15 private static readonly Action<object> Close;
15 16
16 17 static MailMessageText()
17   - {
  18 + {
18 19
19 20 var messageType = typeof(MailMessage);
20 21 var mailWriterType = messageType.Assembly.GetType("System.Net.Mail.MailWriter");
21   -
  22 +
22 23
23 24 // mail writer constructor
24 25 {
@@ -27,18 +28,37 @@ static MailMessageText()
27 28 var conExp = Expression.New(constructorInfo, argument);
28 29 MailWriterFactory = Expression.Lambda<Func<Stream, object>>(conExp, argument).Compile();
29 30 }
30   -
31   -
  31 +
  32 +
32 33 // mail message Send method
  34 + // void Send(BaseWriter writer, Boolean sendEnvelope)
  35 + // void Send(BaseWriter writer, bool sendEnvelope, bool allowUnicode)
33 36 {
34 37 var sendMethod = messageType.GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
35 38 var mailWriter = Expression.Parameter(typeof(object), "mailWriter");
36 39 var sendEnvelope = Expression.Parameter(typeof(bool), "sendEnvelope");
37 40 var allowUnicode = Expression.Parameter(typeof(bool), "allowUnicode");
38 41 var instance = Expression.Parameter(messageType, "instance");
39   - var call = Expression.Call(instance, sendMethod, Expression.Convert(mailWriter, mailWriterType), sendEnvelope, allowUnicode);
40 42
41   - Send = Expression.Lambda<Action<MailMessage, object, bool, bool>>(call, instance, mailWriter, sendEnvelope, allowUnicode).Compile();
  43 + var pars = sendMethod.GetParameters();
  44 + if (pars.Length == 3)
  45 + {
  46 + var call = Expression.Call(instance, sendMethod, Expression.Convert(mailWriter, mailWriterType),
  47 + sendEnvelope, allowUnicode);
  48 +
  49 + Send3 =
  50 + Expression.Lambda<Action<MailMessage, object, bool, bool>>(call, instance, mailWriter,
  51 + sendEnvelope, allowUnicode).Compile();
  52 + }
  53 + else if (pars.Length == 2)
  54 + {
  55 + var call = Expression.Call(instance, sendMethod, Expression.Convert(mailWriter, mailWriterType),
  56 + sendEnvelope);
  57 +
  58 + Send2 =
  59 + Expression.Lambda<Action<MailMessage, object, bool>>(call, instance, mailWriter,
  60 + sendEnvelope).Compile();
  61 + }
42 62 }
43 63
44 64
@@ -53,7 +73,7 @@ static MailMessageText()
53 73
54 74
55 75
56   - }
  76 + }
57 77
58 78
59 79 /// <summary>
@@ -77,7 +97,15 @@ public static string GetText([NotNull]this MailMessage message)
77 97
78 98 object mailWriter = MailWriterFactory(internalStream);
79 99
80   - Send(message, mailWriter, false, true);
  100 + if (Send2 != null)
  101 + {
  102 + Send2(message, mailWriter, false);
  103 + }
  104 + else
  105 + {
  106 + Send3(message, mailWriter, false, true);
  107 + }
  108 +
81 109 Close(mailWriter);
82 110
83 111 internalStream.Position = 0;

0 comments on commit 120ada0

Please sign in to comment.
Something went wrong with that request. Please try again.