LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 读取eml文件 例子(解析eml文件)

admin
2017年10月21日 18:25 本文热度 6860
【实例简介】标题/邮箱/信件内容等

【实例截图】


【核心代码】


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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);
        }
    }
}

该文章在 2017/10/21 18:25:30 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved