| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SmtpHelper.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.IO; |
| | | 10 | | using System.Net; |
| | | 11 | | using System.Net.Sockets; |
| | | 12 | | using System.Text; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Mail.Smtp; |
| | | 15 | | |
| | | 16 | | public static class SmtpHelper |
| | | 17 | | { |
| | | 18 | | public static bool TestSmtpConnection(string? server, int port) |
| | | 19 | | { |
| | 24 | 20 | | if (string.IsNullOrWhiteSpace(server) || port is < IPEndPoint.MinPort or > IPEndPoint.MaxPort) |
| | | 21 | | { |
| | 15 | 22 | | return false; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | try |
| | | 26 | | { |
| | 9 | 27 | | using var client = new TcpClient(); |
| | 9 | 28 | | var connectTask = client.ConnectAsync(server, port); |
| | 9 | 29 | | if (!connectTask.Wait(TimeSpan.FromSeconds(5))) |
| | | 30 | | { |
| | 0 | 31 | | return false; |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | client.SendTimeout = 5000; |
| | 0 | 35 | | client.ReceiveTimeout = 5000; |
| | | 36 | | |
| | 0 | 37 | | using var stream = client.GetStream(); |
| | 0 | 38 | | using var reader = new StreamReader(stream, Encoding.ASCII, detectEncodingFromByteOrderMarks: false, bufferS |
| | 0 | 39 | | using var writer = new StreamWriter(stream, Encoding.ASCII, bufferSize: 1024, leaveOpen: true); |
| | 0 | 40 | | writer.NewLine = "\r\n"; |
| | 0 | 41 | | writer.AutoFlush = true; |
| | | 42 | | |
| | 0 | 43 | | if (!CheckResponse(reader, 220)) |
| | | 44 | | { |
| | 0 | 45 | | return false; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | writer.WriteLine(string.Format(CultureInfo.InvariantCulture, "EHLO {0}", Dns.GetHostName())); |
| | 0 | 49 | | return CheckResponse(reader, 250); |
| | | 50 | | |
| | | 51 | | // if we got here it's that we can connect to the smtp server |
| | | 52 | | } |
| | 9 | 53 | | catch (Exception) |
| | | 54 | | { |
| | 9 | 55 | | return false; |
| | | 56 | | } |
| | 9 | 57 | | } |
| | | 58 | | |
| | | 59 | | private static bool CheckResponse(TextReader reader, int expectedCode) |
| | | 60 | | { |
| | | 61 | | while (true) |
| | | 62 | | { |
| | 0 | 63 | | var responseLine = reader.ReadLine(); |
| | 0 | 64 | | if (string.IsNullOrWhiteSpace(responseLine) || responseLine.Length < 3) |
| | | 65 | | { |
| | 0 | 66 | | return false; |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (!int.TryParse(responseLine.AsSpan(0, 3), NumberStyles.None, CultureInfo.InvariantCulture, out var respon |
| | | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | if (responseCode != expectedCode) |
| | | 75 | | { |
| | 0 | 76 | | return false; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // SMTP multiline responses use "250-..." and end with "250 ...". |
| | 0 | 80 | | if (responseLine.Length < 4 || responseLine[3] != '-') |
| | | 81 | | { |
| | 0 | 82 | | return true; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | |