CDO解析eml文件为html,C# 读取eml文件 例子(解析eml文件)

【实例简介】标题/邮箱/信件内容等

【实例截图】

bb0c49eab2fa0e4240cb9cd1b1b74699.png

3169b8167c965cc83dbaf9d08b8e07df.png

【核心代码】

using System.IO;

using System.Linq;

using System.Windows.Forms;

using ADODB;

using CDO;

using System.Diagnostics;

using Stream = ADODB.Stream;

using System;

namespace EmlReader.WinForm

{

public partial class EmlViewer : Form

{

public EmlViewer()

{

InitializeComponent();

}

public void OpenEml(string filepath)

{

try

{

var stream = new StreamClass();

stream.Open();

stream.LoadFromFile(filepath);

stream.Flush();

var mail = new MessageClass();

mail.DataSource.OpenObject(stream, "_Stream");

mail.DataSource.Save();

fromBox.Text = mail.From;

toBox.Text = mail.To;

ccBox.Text = mail.CC;

bccBox.Text = mail.BCC;

subjectBox.Text = mail.Subject;

bodyBox.Text = mail.TextBody;

attachmentsPanel.Controls.Clear();

foreach (IBodyPart attachment in mail.Attachments)

{

var linkLabel = new LinkLabel

{

Text = attachment.FileName,

AutoSize = true,

};

linkLabel.Links.Add(0, linkLabel.Text.Length, attachment.GetDecodedContentStream());

linkLabel.LinkClicked = LinkLabelLinkClicked;

linkLabel.Disposed = LinkLabelDisposed;

attachmentsPanel.Controls.Add(linkLabel);

}

stream.Close();

}

catch (Exception)

{

MessageBox.Show(this, "An error occured while opening the file", "Email could not be opened",

MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

static void LinkLabelDisposed(object sender, EventArgs e)

{

var linkLabel = sender as LinkLabel;

if (linkLabel == null) return;

foreach (LinkLabel.Link link in linkLabel.Links)

((Stream)link.LinkData).Close();

}

static void LinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

{

var linkLabel = sender as LinkLabel;

if (linkLabel == null) return;

var fileName = linkLabel.Text;

linkLabel.LinkVisited = true;

var stream = e.Link.LinkData as Stream;

if (stream == null) return;

var tempFile = Path.Combine(Path.GetTempPath(), fileName);

stream.SaveToFile(tempFile, SaveOptionsEnum.adSaveCreateOverWrite);

Process.Start(tempFile);

}

private void EmlViewerDragDrop(object sender, DragEventArgs e)

{

var files = (string[])e.Data.GetData(DataFormats.FileDrop);

OpenEml(files.First());

}

private void EmlViewerDragEnter(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;

}

private void OpenButtonClick(object sender, EventArgs e)

{

openFileDialog.ShowDialog(this);

}

private void OpenFileDialogFileOk(object sender, System.ComponentModel.CancelEventArgs e)

{

var dialog = sender as FileDialog;

if (dialog == null) return;

OpenEml(dialog.FileName);

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值