Skip to content

Commit 029fed8

Browse files
committed
.
1 parent 60ad11f commit 029fed8

2 files changed

Lines changed: 53 additions & 29 deletions

File tree

SMTP2Graph/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using Microsoft.Extensions.Logging.EventLog;
5+
6+
namespace SMTP2Graph
7+
{
8+
public static class Program
9+
{
10+
public static void Main()
11+
{
12+
HostApplicationBuilder appBuilder = Host.CreateApplicationBuilder();
13+
appBuilder.Services.AddWindowsService((opt) => {
14+
opt.ServiceName = "SMTP2Graph";
15+
});
16+
appBuilder.Logging.SetMinimumLevel(LogLevel.Trace);
17+
appBuilder.Logging.AddSimpleConsole((sc) => {
18+
sc.SingleLine = true;
19+
sc.TimestampFormat = "MM/dd/yyyy: hh:mm:ss: ";
20+
});
21+
if (OperatingSystem.IsWindows())
22+
{
23+
appBuilder.Logging.AddFilter<EventLogLoggerProvider>((level) => true);
24+
appBuilder.Logging.AddEventLog();
25+
}
26+
appBuilder.Services.AddHostedService<Worker>();
27+
IHost host = appBuilder.Build();
28+
host.Run();
29+
}
30+
}
31+
}
Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,23 @@
77
using Microsoft.Kiota.Abstractions;
88
using Microsoft.Win32;
99
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.Hosting;
1011

1112
namespace SMTP2Graph;
1213

13-
public static class Service
14+
public class Worker(ILogger<Worker> Logger) : BackgroundService
1415
{
15-
private readonly static ILoggerFactory LogFactory = LoggerFactory.Create((lf) => {
16-
lf.SetMinimumLevel(LogLevel.Trace);
17-
lf.AddSimpleConsole((sc) => {
18-
sc.SingleLine = true;
19-
sc.TimestampFormat = "MM/dd/yyyy: hh:mm:ss: ";
20-
});
21-
if (OperatingSystem.IsWindows()) lf.AddEventLog();
22-
});
23-
24-
private static readonly string[] GraphScopes = ["https://graph.microsoft.com/.default"];
25-
26-
private static readonly ClientSecretCredentialOptions GraphCredOptions = new() {
16+
private readonly string[] GraphScopes = ["https://graph.microsoft.com/.default"];
17+
18+
private readonly ClientSecretCredentialOptions GraphCredOptions = new() {
2719
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
2820
};
2921

30-
private static GraphServiceClient? GraphClient;
31-
32-
private static readonly ILogger Logger = LogFactory.CreateLogger(typeof(Service));
22+
private GraphServiceClient? GraphClient;
3323

34-
private static readonly TcpListener MailListener = new(IPAddress.Any, 25);
24+
private readonly TcpListener MailListener = new(IPAddress.Any, 25);
3525

36-
public static void Main()
26+
protected override Task ExecuteAsync(CancellationToken stoppingToken)
3727
{
3828
Logger.LogInformation("Initializing SMTP2Graph...");
3929

@@ -88,7 +78,7 @@ public static void Main()
8878
}
8979
}
9080

91-
private static string GetConfigOption(string Key)
81+
private string GetConfigOption(string Key)
9282
{
9383
string? env;
9484
env = Environment.GetEnvironmentVariable(Key);
@@ -100,7 +90,7 @@ private static string GetConfigOption(string Key)
10090
return string.Empty;
10191
}
10292

103-
private static async Task HandleConnection(TcpClient client, Stream stream, int Port)
93+
private async Task HandleConnection(TcpClient client, Stream stream, int Port)
10494
{
10595
string From = string.Empty;
10696
var sw = new StreamWriter(stream);
@@ -123,42 +113,42 @@ private static async Task HandleConnection(TcpClient client, Stream stream, int
123113
Logger.LogTrace(Port, "Recieved: {msg}", msg);
124114
if (msg.StartsWith("HELO") || msg.StartsWith("EHLO"))
125115
{
126-
await sw.WriteAsyncExt("250 OK", Port);
116+
await sw.WriteAsyncExt("250 OK", Logger, Port);
127117
continue;
128118
}
129119
if (msg.StartsWith("MAIL FROM:"))
130120
{
131121
#pragma warning disable SYSLIB1045
132122
From = Regex.Match(msg, "<(.*)>").Groups[1].Value;
133123
#pragma warning restore SYSLIB1045
134-
await sw.WriteAsyncExt("250 OK", Port);
124+
await sw.WriteAsyncExt("250 OK", Logger, Port);
135125
continue;
136126
}
137127
if (msg.StartsWith("RCPT TO:"))
138128
{
139-
await sw.WriteAsyncExt("250 OK", Port);
129+
await sw.WriteAsyncExt("250 OK", Logger, Port);
140130
continue;
141131
}
142132
if (msg.StartsWith("DATA"))
143133
{
144-
await sw.WriteAsyncExt("354 Start mail input; end with <CRLF>.<CRLF>", Port);
134+
await sw.WriteAsyncExt("354 Start mail input; end with <CRLF>.<CRLF>", Logger, Port);
145135
var MIME = await ReadMIME(sr);
146136
Logger.LogTrace(Port, "Recieved: {Message}", MIME[..(MIME.Length > 500 ? 500 : MIME.Length)]);
147-
await sw.WriteAsyncExt("250 OK", Port);
137+
await sw.WriteAsyncExt("250 OK", Logger, Port);
148138
await SendMessage(From, MIME, Port);
149139
continue;
150140
}
151141
if (msg.StartsWith("QUIT"))
152142
{
153-
await sw.WriteAsyncExt($"221 {Environment.MachineName} Service closing transmission channel", Port);
143+
await sw.WriteAsyncExt($"221 {Environment.MachineName} Service closing transmission channel", Logger, Port);
154144
break;
155145
}
156146
}
157147
Logger.LogInformation(Port, "Closing connection...");
158148
client.Close();
159149
}
160150

161-
private static async Task<string> ReadMIME(StreamReader SR)
151+
private async Task<string> ReadMIME(StreamReader SR)
162152
{
163153
var mime = new List<string?>();
164154
while (true)
@@ -174,7 +164,7 @@ private static async Task<string> ReadMIME(StreamReader SR)
174164
return string.Join("\r\n", mime);
175165
}
176166

177-
private static async Task SendMessage(string From, string MIME, int Port)
167+
private async Task SendMessage(string From, string MIME, int Port)
178168
{
179169
Logger.LogInformation(Port, "Sending MIME to MS Graph...");
180170
var mimeb64bytes = Encoding.UTF8.GetBytes(
@@ -192,8 +182,11 @@ private static async Task SendMessage(string From, string MIME, int Port)
192182
if (GraphClient != null) await GraphClient.RequestAdapter.SendNoContentAsync(request);
193183
Logger.LogInformation(Port, "Message sent to MS Graph.");
194184
}
185+
}
195186

196-
private static async Task WriteAsyncExt(this StreamWriter SW, string? Message, int Port)
187+
public static class Extensions
188+
{
189+
public static async Task WriteAsyncExt(this StreamWriter SW, string? Message, ILogger Logger, int Port)
197190
{
198191
Logger.LogTrace(Port, "Sent: {Message}", Message);
199192
await SW.WriteAsync($"{Message}\r\n");

0 commit comments

Comments
 (0)