Eml2Pdf / Eml2PdfWinApp / Form1.cs
Code · 177 lines · 3889 bytes
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177using Eml2MimePart;
using MimePart2Pdf;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Eml2PdfWinApp;

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
	}

	private async void Button1_Click(object sender, EventArgs e)
	{
		this.button1.Enabled = false;
		this.txtInput.Enabled = false;
		this.txtOutput.Enabled = false;

		Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

		foreach (var emlPath in Directory.GetFiles(this.txtOutput.Text, "*.eml"))
		{
			var name = Path.GetFileNameWithoutExtension(emlPath);

			var pdfPath = Path.Combine(this.textBox3.Text, $"{name}.pdf");

			var email = await MimePart.ReadEmlAsync(emlPath);

			PdfHelper.CreatePdf(email, pdfPath);

		}

		this.txtInput.Enabled = true;
		this.txtOutput.Enabled = true;
		this.button1.Enabled = true;
	}

	static async Task SaveMailAttachementsAsync(TextBox log, string OutputDir, MimePart part)
	{
		var contentType = part["Content-Type"];

		if (part["Content-Disposition"].Contains("attachment"))
		{
			if (contentType.Contains("message/rfc822"))
			{
				if (DateTime.TryParse(part.Parts[0]["Date"], out DateTime dtm))
				{
					var outputPath = Path.Combine(OutputDir, $"{dtm:yyyyMMdd-HHmmss}.eml");

					try
					{
						await part.SaveAsync(outputPath, dtm);
					}
					catch (Exception ex)
					{
						log.Invoke((MethodInvoker)delegate
						{
							log.Text = $"{outputPath} {ex.Message}";
						});
					}
				}
				else
				{
					Debug.WriteLine("No Date found in message/rfc822 attachement");
				}
			}
		}

		foreach (var subpart in part.Parts)
		{
			await SaveMailAttachementsAsync(log, OutputDir, subpart);
		}
	}

	static async Task SaveHtmlMailBodyAsync(TextBox log, string OutputDir, MimePart part)
	{

		if (DateTime.TryParse(part["Date"], out DateTime dtm))
		{
			var outputPath = Path.Combine(OutputDir, $"{dtm:yyyyMMdd-HHmmss}.eml");

			try
			{
				await part.SaveAsync(outputPath, dtm);
			}
			catch (Exception ex)
			{
				log.Invoke((MethodInvoker)delegate
				{
					log.Text = $"{outputPath} {ex.Message}";
				});
			}
		}
	}





	private async void Button2_Click(object sender, EventArgs e)
	{
		this.button2.Enabled = false;

		Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

		foreach (var emlPath in Directory.GetFiles(this.txtInput.Text, "*.eml"))
		{
			var email = await MimePart.ReadEmlAsync(emlPath);

			//await SaveHtmlMailBodyAsync(this.txtLog, this.txtOutput.Text, email);
			await SaveMailAttachementsAsync(this.txtLog, this.txtOutput.Text, email);
		}

		this.button2.Enabled = true;
	}

	private static string GetHtml(MimePart email)
	{
		if (email["Content-Type"].Contains("text/html"))
			return email.TextContent;
		foreach (var part in email.Parts)
		{
			var html = GetHtml(part);
			if (!string.IsNullOrEmpty(html))
				return html;
		}
		return string.Empty;
	}


	private async void ButtonEml2Html_Click(object sender, EventArgs e)
	{
		this.button3.Enabled = false;

		Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

		var htmlDir = this.txtHtml.Text;

		foreach (var emlPath in Directory.GetFiles(this.txtOutput.Text, "*.eml"))
		{
			var email = await MimePart.ReadEmlAsync(emlPath);

			var html = GetHtml(email);

			if (string.IsNullOrWhiteSpace(html))
				return;

			var name = Path.GetFileNameWithoutExtension(emlPath);

			var dtm = DateTime.ParseExact(name, "yyyyMMdd-HHmmss", null);

			var output = Path.Combine(htmlDir, name + ".html");

			try
			{
				await File.WriteAllTextAsync(output, html);

				//File.SetAttributes(output, FileAttributes.ReadOnly);
				File.SetLastWriteTimeUtc(output, dtm);
				File.SetCreationTime(output, dtm);

			}
			catch (Exception eee)
			{
				this.txtLog.Invoke((MethodInvoker)delegate
				{
					this.txtLog.Text = $"{name} {eee.Message}";
				});
			}
		}

		this.button3.Enabled = true;
	}
}