forked from DigitalRuby/MailDemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailDemonApp.cs
125 lines (110 loc) · 4.85 KB
/
MailDemonApp.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DnsClient;
using MailKit;
using MailKit.Net;
using MailKit.Net.Smtp;
using Microsoft.Extensions.Configuration;
using MimeKit;
namespace MailDemon
{
public class MailDemonApp
{
private MailDemonService mailService;
private MailDemonWebApp webApp;
private CancellationTokenSource cancel = new CancellationTokenSource();
private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
cancel.Cancel();
}
private static async Task TestClientConnectionAsync(MailDemonService demon, string server, string to, string file)
{
SmtpClient client = new SmtpClient()
{
SslProtocols = System.Security.Authentication.SslProtocols.None,
Timeout = 60000 // 60 secs
};
await client.ConnectAsync(server, 25, MailKit.Security.SecureSocketOptions.StartTlsWhenAvailable);
await client.AuthenticateAsync(new NetworkCredential(demon.Users.First().UserName, demon.Users.First().Password));
MimeMessage msg = new MimeMessage();
msg.From.Add(demon.Users.First().MailAddress);
foreach (string toAddress in to.Split(',', ';'))
{
msg.To.Add(new MailboxAddress(toAddress));
}
msg.Subject = "Test Subject";
BodyBuilder bodyBuilder = new BodyBuilder();
Multipart multipart = new Multipart("mixed");
bodyBuilder.HtmlBody = "<html><body><b>Test Email Html Body Which is Bold 12345</b></body></html>";
multipart.Add(bodyBuilder.ToMessageBody());
if (file != null && File.Exists(file))
{
byte[] bytes = System.IO.File.ReadAllBytes(file);
var attachment = new MimePart("binary", "bin")
{
Content = new MimeContent(new MemoryStream(bytes), ContentEncoding.Binary),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Binary, // Base64 for DATA test, Binary for BINARYMIME test
FileName = Path.GetFileName(file)
};
multipart.Add(attachment);
}
msg.Body = multipart;
await client.SendAsync(msg);
await client.DisconnectAsync(true);
Console.WriteLine("Test message sent");
}
private Task Run(string[] args)
{
Console.CancelKeyPress += Console_CancelKeyPress;
// read config
string rootDir = Directory.GetCurrentDirectory();
IConfigurationBuilder configBuilder = new ConfigurationBuilder().SetBasePath(rootDir);
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.debug.json")))
{
configBuilder.AddJsonFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.debug.json"));
}
else
{
configBuilder.AddJsonFile("appsettings.json");
}
IConfigurationRoot config = configBuilder.Build();
CertificateCache cache = new CertificateCache(config); // singleton
// start mail server
mailService = new MailDemonService(args, config);
Task mailTask = mailService.StartAsync(cancel.Token);
MailDemonLog.Info("Mail demon running");
// test sending with the server:
// test localhost toaddress@domain.com,toaddress@otherdomain.com [full path to file to attach]
if (args.Length > 0 && args[0].StartsWith("test", StringComparison.OrdinalIgnoreCase))
{
mailService.DisableSending = true;
string file = args.Length > 2 ? args[3] : null;
if (file != null)
{
TestClientConnectionAsync(mailService, args[1], args[2], file).ConfigureAwait(false).GetAwaiter().GetResult();
TestClientConnectionAsync(mailService, args[1], args[2], file).ConfigureAwait(false).GetAwaiter().GetResult();
}
args = new string[0];
}
// start web server
webApp = new MailDemonWebApp(args, rootDir, config, mailService);
Task webTask = webApp.StartAsync(cancel.Token);
return Task.WhenAll(mailTask, webTask);
}
public static Task Main(string[] args)
{
MailDemonApp app = new MailDemonApp();
return app.Run(args);
}
}
}