| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="MailKitSmtpOptionsValidator.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Mail.MailKit.Exceptions; |
| | | 9 | | using MyNet.Mail.Smtp; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Mail.MailKit; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Validates <see cref="SmtpClientOptions"/> before connecting or sending mail. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class MailKitSmtpOptionsValidator |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Validates SMTP client options and throws if configuration is invalid. |
| | | 20 | | /// </summary> |
| | | 21 | | public static void Validate(SmtpClientOptions options) |
| | | 22 | | { |
| | 33 | 23 | | ArgumentNullException.ThrowIfNull(options); |
| | | 24 | | |
| | 33 | 25 | | if (options.UsePickupDirectory) |
| | | 26 | | { |
| | 18 | 27 | | if (string.IsNullOrWhiteSpace(options.MailPickupDirectory)) |
| | | 28 | | { |
| | 3 | 29 | | throw new ArgumentException("Pickup directory must be defined when UsePickupDirectory is enabled."); |
| | | 30 | | } |
| | | 31 | | |
| | 15 | 32 | | return; |
| | | 33 | | } |
| | | 34 | | |
| | 15 | 35 | | if (string.IsNullOrWhiteSpace(options.Server)) |
| | | 36 | | { |
| | 3 | 37 | | throw new UndefinedServerException(); |
| | | 38 | | } |
| | | 39 | | |
| | 12 | 40 | | if (options.Port is < 1 or > 65535) |
| | | 41 | | { |
| | 3 | 42 | | throw new ArgumentOutOfRangeException(nameof(options), options.Port, "SMTP port must be between 1 and 65535. |
| | | 43 | | } |
| | | 44 | | |
| | 9 | 45 | | switch (options.RequiresAuthentication) |
| | | 46 | | { |
| | 9 | 47 | | case true when string.IsNullOrWhiteSpace(options.User): |
| | 3 | 48 | | throw new ArgumentException("SMTP user must be defined when authentication is required."); |
| | 6 | 49 | | case true when options.Password is null: |
| | 3 | 50 | | throw new ArgumentException("SMTP password must be defined when authentication is required."); |
| | | 51 | | } |
| | 3 | 52 | | } |
| | | 53 | | } |
| | | 54 | | |