using System.Data; using System.Net; using System.Net.Mail; using System.Net.Sockets; using System.Text; namespace SmtpClient; internal class Program { private static string? _host = null; private static string? _username = null; private static string? _password = null; private static string? _from = null; private static List? _rcpts = []; private static List? _rcptsCc = []; private static string? _subject = null; private static string? _body = null; private static bool _secure = false; static async Task Main() { Console.WriteLine("Relay Test Client"); Console.WriteLine("\nSelect Input:"); Console.WriteLine("\t1-Static"); Console.WriteLine("\t2-Interactive"); switch (Console.ReadKey().Key) { case ConsoleKey.D1: case ConsoleKey.NumPad1: StaticInput(); break; case ConsoleKey.D2: case ConsoleKey.NumPad2: InteractiveInput(); break; default: return; } Console.WriteLine("\nSelect Method:"); Console.WriteLine("\t1-Single Mail"); Console.WriteLine("\t2-Single Mail (Raw Pipelined)"); Console.WriteLine("\t3-Mutli Mail"); try { switch (Console.ReadKey(true).Key) { case ConsoleKey.D1: case ConsoleKey.NumPad1: await SendAsync(); break; case ConsoleKey.D2: case ConsoleKey.NumPad2: await SendPipelinedAsync(); break; case ConsoleKey.D3: case ConsoleKey.NumPad3: await SendMultiAsync(); break; default: Console.WriteLine("\nUndefined Method"); break; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } static void StaticInput() { _host = "localhost:2525"?.Trim(); _secure = false; _username = "relay@example.local"; _password = "supersecret"; _from = "me@example.local"?.Trim(); _rcpts = "noreply@example.local"?.Trim()?.Split(';').ToList(); _rcptsCc = []; _subject = "Testnachricht"; _body = "Das ist eine Testnachricht."; } static void InteractiveInput() { Console.WriteLine("\nEnter Relay Host: "); _host = Console.ReadLine()?.Trim(); Console.WriteLine("\nEnter Username (Optional): "); _username = Console.ReadLine()?.Trim(); if (!string.IsNullOrWhiteSpace(_username)) { Console.WriteLine("\nEnter Password: "); _password = Console.ReadLine()?.Trim(); } Console.WriteLine("\nEnter Sender:"); _from = Console.ReadLine()?.Trim(); Console.WriteLine("\nEnter Receiver (delimit with ';'):"); _rcpts = Console.ReadLine()?.Trim()?.Split(';').ToList(); Console.WriteLine("\nEnter Cc Receiver (delimit with ';'):"); _rcptsCc = Console.ReadLine()?.Trim()?.Split(';').ToList(); Console.WriteLine("\nEnter Subject:"); _subject = Console.ReadLine()?.Trim(); Console.WriteLine("\nEnter Body:"); _body = Console.ReadLine()?.Trim(); } static async Task SendAsync() { var from = new MailAddress(_from!); var to = _rcpts!.Select(p => new MailAddress(p)).ToList(); var cc = _rcptsCc!.Select(p => new MailAddress(p)).ToList(); var firstTo = to.First(); to.Remove(firstTo); using var message = new MailMessage(from, firstTo) { Priority = MailPriority.Normal, Subject = _subject ?? "Undefined", IsBodyHtml = false, Body = _body ?? string.Empty, }; foreach (var x in to) message.To.Add(x); foreach (var x in cc) message.CC.Add(x); message.Attachments.Add(new Attachment("attachment.txt")); var credential = new NetworkCredential(); if (!string.IsNullOrWhiteSpace(_username)) { credential.UserName = _username; credential.Password = _password; } var host = _host; var port = 25; if (_host!.Contains(':')) { host = _host.Split(':')[0]; port = int.Parse(_host.Split(":")[1]); } using var smtp = new System.Net.Mail.SmtpClient(host, port) { UseDefaultCredentials = false, Credentials = credential, EnableSsl = _secure, DeliveryMethod = SmtpDeliveryMethod.Network }; await smtp.SendMailAsync(message); } static async Task SendMultiAsync() { var from = new MailAddress(_from!); var to = _rcpts!.Select(p => new MailAddress(p)).ToList(); var cc = _rcptsCc!.Select(p => new MailAddress(p)).ToList(); var firstTo = to.First(); to.Remove(firstTo); using var message = new MailMessage(from, firstTo) { Priority = MailPriority.Normal, Subject = _subject ?? "Undefined", IsBodyHtml = false, Body = _body ?? string.Empty }; foreach (var x in to) message.To.Add(x); foreach (var x in cc) message.CC.Add(x); message.Attachments.Add(new Attachment("attachment.txt")); var credential = new NetworkCredential(); if (!string.IsNullOrWhiteSpace(_username)) { credential.UserName = _username; credential.Password = _password; } var host = _host; var port = 25; if (_host!.Contains(':')) { host = _host.Split(':')[0]; port = int.Parse(_host.Split(":")[1]); } using var smtp = new System.Net.Mail.SmtpClient(host, port) { UseDefaultCredentials = false, Credentials = credential, EnableSsl = _secure, DeliveryMethod = SmtpDeliveryMethod.Network }; await smtp.SendMailAsync(message); await smtp.SendMailAsync(message); } static async Task SendPipelinedAsync() { var raw = "EHLO SmtpClient\r\n"; if (!string.IsNullOrWhiteSpace(_username)) { raw += $"AUTH PLAIN {Convert.ToBase64String(Encoding.UTF8.GetBytes($"\0{_username}\0{_password}"))}\r\n"; } raw += $"MAIL FROM:<{_from}> SIZE=512\r\n"; foreach (var rcpt in _rcpts!) { raw += $"RCPT TO:<{rcpt}>\r\n"; } raw += "DATA\r\n"; raw += "MIME-Version: 1.0\r\n"; raw += $"From: {_from}\r\n"; raw += $"To: {string.Join(", ", _rcpts!)}\r\n"; if (_rcptsCc != null && _rcptsCc.Count > 0) { raw += $"Cc: {string.Join(", ", _rcptsCc!)}\r\n"; } raw += "Date: 29 Oct 2025 21:10:56 +0100\r\n"; raw += $"Subject:{_subject}\r\n"; raw += "\r\n" ; raw += $"{_body}"; raw += "\r\n.\r\n"; raw += "QUIT\r\n"; var bytes = Encoding.ASCII.GetBytes(raw); var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var host = _host; var port = 25; if (_host!.Contains(':')) { host = _host.Split(':')[0]; port = int.Parse(_host.Split(":")[1]); } await sock.ConnectAsync(host!, port); await sock.SendAsync(bytes); Console.WriteLine("wait 3 seconds..."); await Task.Delay(3000); } }