EML格式是微軟公司在Outlook中所使用的一種遵循RFC822及其後續擴展的文件格式,併成爲各種電子郵件軟件的通用格式。html
作個筆記,C# 郵件處理保存爲eml格式;dom
1、網上好多這樣的寫法,能夠在.net formwork4.5裏面 百試不爽…………this
/// <summary> /// 2.0能夠容許 4.5不能夠 /// </summary> /// <param name="msg"></param> /// <returns></returns> public static byte[] MailMessageToArray(MailMessage msg) { const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy; using (var ms = new MemoryStream()) { var assembly = typeof(SmtpClient).Assembly; var writerType = assembly.GetType("System.Net.Mail.MailWriter"); var writer = Activator.CreateInstance(writerType, flags, null, new object[] { ms }, CultureInfo.InvariantCulture); msg.GetType().GetMethod("Send", flags).Invoke(msg, new[] { writer, true }); return ms.ToArray(); } }
.netformwork 4.5 運行報錯---「用戶代碼未處理TargetParameterCountException 參數不匹配」編碼
2、柳暗花明spa
不得不說仍是老外的技術牛逼些…….net
/// <summary> /// MailMessage 轉換成eml /// </summary> /// <param name="msg">MailMessage</param> /// <returns>eml字符串</returns> static string MailMessageToEml(MailMessage msg) { Assembly assembly = typeof(SmtpClient).Assembly; Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter"); using (MemoryStream stream = new MemoryStream()) { 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); sendMethod.Invoke(msg, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null); return System.Text.Encoding.UTF8.GetString(stream.ToArray()); //return Encoding.Default.GetString( stream.ToArray()); } }
效果槓桿的code
國外的大牛就是同樣,發現和他的差距太TM的不小。。。。還有一種解決方案orm
public static class MailMessageExtensions { public static string RawMessage(this MailMessage m) { var smtpClient = new SmtpClient { DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory }; using (var tempDir = new TemporaryDirectory()) { smtpClient.PickupDirectoryLocation = tempDir.DirectoryPath; smtpClient.Send(m); var emlFile = Directory.GetFiles(smtpClient.PickupDirectoryLocation).FirstOrDefault(); if (emlFile != null) { return File.ReadAllText(emlFile); } else return null; } return null; } } class TemporaryDirectory : IDisposable { public TemporaryDirectory() { DirectoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(DirectoryPath); } public string DirectoryPath { get; private set; } public void Dispose() { if (Directory.Exists(DirectoryPath)) Directory.Delete(DirectoryPath, true); } }
調用方法htm
public void Mime(string mailFrom, string subject, string body, string mailTo) { MailMessage message = new MailMessage(); message.From = new MailAddress(mailFrom, "", Encoding.UTF8); message.To.Add(new MailAddress(mailTo)); message.Subject = subject; message.SubjectEncoding = Encoding.UTF8;////主題編碼 message.Body = body;// "<html><body><h1>Welcome</h1><br>This is an HTML message for outofmemory.cn.</body></html>"; message.IsBodyHtml = true; message.BodyEncoding = Encoding.UTF8;//郵件內容編碼 message.Priority = MailPriority.High;//優先級 //string str=MailMessageToEml(message); string ems = MailMessageExtensions.RawMessage(message); }
結果真是使人興奮,對比而言選擇方案一,至於爲何……看看代碼就曉得了!blog