2013-11-22 19 views
0

我想創建.eml文件。我使用下面的代碼。但是每當它到達方法。調用它給我錯誤參數計數不匹配。。以下是我創建eml文件的代碼。創建EML文件MethodInfo.Invoke給出參數計數不匹配

try 
     { 
      MailMessage m = new MailMessage(); 
      m.Body = htmlbody; 
      var htmlView = AlternateView.CreateAlternateViewFromString(m.Body, null, "text/html"); 

      MailAddress newFromAddress = new MailAddress("[email protected]", "(Please select sender)"); 
      m.Sender = null; 
      m.From = newFromAddress; 
      m.Headers.Add("X-Unsent", "1"); //Make the eml file open in compose mode 
      m.AlternateViews.Add(htmlView); //Add the html content to the email 
      m.Subject = subject; 
      m.IsBodyHtml = true; 

      var assembly = typeof(SmtpClient).Assembly; 
      var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter"); 
      using (MemoryStream stream = new MemoryStream()) 
      { 
       //Construct the mail message file .eml 
       ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null); 
       object mailWriter = mailWriterContructor.Invoke(new object[] { stream }); 
       MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic); 
       ///**This is line where I get error**/// sendMethod.Invoke(m, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null); 

       return stream.ToArray(); 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.ToString()); 
     } 

UPDATE: 我使用.NET Framework 2.0。這是造成問題嗎?

回答