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

推荐订阅源

W
WeLiveSecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cloudbric
Cloudbric
V
Visual Studio Blog
L
LangChain Blog
A
About on SuperTechFans
B
Blog
T
Tenable Blog
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
博客园 - 三生石上(FineUI控件)
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Palo Alto Networks Blog
U
Unit 42
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
C
Check Point Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence
Application and Cybersecurity Blog
Application and Cybersecurity Blog
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
S
Securelist
Security Archives - TechRepublic
Security Archives - TechRepublic
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Latest news
Latest news
GbyAI
GbyAI
T
Troy Hunt's Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
V2EX - 技术
V2EX - 技术
小众软件
小众软件
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
Netflix TechBlog - Medium
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed

博客园 - 阿齐

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