| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SmtpSecureSocketOptionsResolver.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MailKit.Security; |
| | | 9 | | using MyNet.Mail.Smtp; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Mail.MailKit; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Maps <see cref="SmtpClientOptions"/> to MailKit <see cref="SecureSocketOptions"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class SmtpSecureSocketOptionsResolver |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Resolves the secure socket options for the given SMTP options. |
| | | 20 | | /// </summary> |
| | | 21 | | public static SecureSocketOptions Resolve(SmtpClientOptions options) |
| | | 22 | | { |
| | 21 | 23 | | ArgumentNullException.ThrowIfNull(options); |
| | | 24 | | |
| | 21 | 25 | | return options.SecurityMode != SmtpSecurityMode.Auto |
| | 21 | 26 | | ? Map(options.SecurityMode) |
| | 21 | 27 | | : options.Port == 465 |
| | 21 | 28 | | ? SecureSocketOptions.SslOnConnect |
| | 21 | 29 | | : options.UseSsl |
| | 21 | 30 | | ? SecureSocketOptions.StartTls |
| | 21 | 31 | | : SecureSocketOptions.None; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | private static SecureSocketOptions Map(SmtpSecurityMode mode) => |
| | 12 | 35 | | mode switch |
| | 12 | 36 | | { |
| | 3 | 37 | | SmtpSecurityMode.None => SecureSocketOptions.None, |
| | 3 | 38 | | SmtpSecurityMode.StartTls => SecureSocketOptions.StartTls, |
| | 3 | 39 | | SmtpSecurityMode.StartTlsWhenAvailable => SecureSocketOptions.StartTlsWhenAvailable, |
| | 3 | 40 | | SmtpSecurityMode.SslOnConnect => SecureSocketOptions.SslOnConnect, |
| | 0 | 41 | | _ => SecureSocketOptions.Auto |
| | 12 | 42 | | }; |
| | | 43 | | } |
| | | 44 | | |