EML格式是微软公司在Outlook中所使用的一种遵循RFC822及其后续扩展的文件格式,并成为各类电子邮件软件的通用格式。

做个笔记,C# 邮件处理保存为eml格式;

一、网上好多这样的写法,可以在.net formwork4.5里面 百试不爽…………

  /// <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 参数不匹配”

MailMessage to EML-LMLPHP

二、柳暗花明

不得不说还是老外的技术牛逼些……

      /// <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());
}
}

效果杠杆的

MailMessage to EML-LMLPHP

国外的大牛就是一样,发现和他的差距太TM的不小。。。。还有一种解决方案

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);
}
}

调用方法

        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);
}

结果然是令人兴奋,对比而言选择方案一,至于为什么……看看代码就晓得了!

05-04 01:50