惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
About on SuperTechFans
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
腾讯CDC
P
Proofpoint News Feed
S
Schneier on Security
S
Secure Thoughts
V
Visual Studio Blog
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
T
Threatpost
小众软件
小众软件
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
T
Tailwind CSS Blog
I
Intezer
C
CERT Recently Published Vulnerability Notes
U
Unit 42
V
V2EX
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
O
OpenAI News
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
博客园 - 叶小钗
S
Securelist
A
Arctic Wolf
The Cloudflare Blog
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
博客园 - Franky

博客园 - 阿齐

2013年1月18日 星期五 如何修改Panorama控件的Title 分享好用的截屏软件:SnagIt [转]如何在Windows Server 2012中安装.Net Framework 3.5? 屌丝笔记本玩Windows Phone 8开发(在Windows Server 2012中安装WP8 SDK) 在Ubuntu下的Firefox地址栏点击全选网页地址 Ubuntu下的Firefox差点让我崩溃 使用ICSharpCode.SharpZLib压缩/解压给定的数据 如何判断文本文件的编码方式? 使用Base64编码数据量变大33.4% 旧文一篇:7年项目总结 创业之三要素 让MSN Messager(Windows Live Messager)重回托盘 创业的Idea是怎样产生的? 我的恩师白玉玺先生-----转自北京崇文汇通武术网站,作者:柳宗林 记录一把,两个截取HTTP请求的工具 标记一下:两个免费的SVN服务 需要使用计算器才能做的投资,通常都不是好投资 解决“A problem has been encountered while loading the setup components. Canceling setup.”的问题 己丑年正月十九北京随想
在.Net中将MailMessage保存为本地eml文件
阿齐 · 2010-11-01 · via 博客园 - 阿齐

最近因故需要用.Net(C#)将邮件信息保存为本地eml文件,看起来是个费劲的活:首先得读懂MIME协议(RFC 2045~2049),然后写代码。还好,google后发现以前有朋友也有过这样的需求,并有现成解决方案,呵呵:System.Net.Mail命名空间里已经实现了MailWriter,只不知道为啥没有公开而已。

用Xenocode Fox 2007(Community Edition)加载System.dll,找到System.Net.Mail.SmtpClient类,可看到其Send(MailMessage)方法中有这么两句代码:

MailWriter writer;

writer = GetFileMailWriter(PickupDirectoryLocation);

我们来看看GetFileMailWriter方法:

internal MailWriter GetFileMailWriter (string pickupDirectory)
{
  string path;
  ...

  do
  {
    Guid guid1 = Guid.NewGuid();
    string path2 = guid1.ToString() + ".eml";
    path = Path.Combine(pickupDirectory, path2);
  }
  while (File.Exists(path));
  return new MailWriter(new FileStream(path, FileMode.CreateNew));
}

再往深里看便可知:MailWriter直接将MailMessage内容写成了“.eml”文件,而且是按MIME协议写入的,Yeah!

那接下来要做的就是用上MailWriter这个内部(internal)类,当然这是很简单的事:

public static byte[] MailMessageToArray(MailMessage msg)
{
    const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
    using (var ms = new MemoryStream())
    {
        var assembly = typeof (System.Net.Mail.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();
    }
}

如果我们使用的是C#3.0以后的版本,还可以为MailMessage添加扩展方法,这样使用起来更为自然。代码如下:

//定义扩展方法

public static class MailMessageExtend
{
    public static byte[] ToArray(this MailMessage msg)
    {
        const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
        using (var ms = new MemoryStream())
        {
            var assembly = typeof(System.Net.Mail.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();
        }
    }

}

//调用

private void button1_Click(object sender, EventArgs e)
{
    var msg = new MailMessage();
    ...
    var content = msg.ToArray();
    ...
}