diff --git a/src/NETCore.MailKit/Core/EmailService.cs b/src/NETCore.MailKit/Core/EmailService.cs index f1efb3f..4b02ac3 100644 --- a/src/NETCore.MailKit/Core/EmailService.cs +++ b/src/NETCore.MailKit/Core/EmailService.cs @@ -2,6 +2,7 @@ using MimeKit.Text; using NETCore.MailKit.Infrastructure.Internal; using NETCore.MailKit.Shared; +using System; using System.IO; using System.Linq; using System.Text; @@ -297,7 +298,18 @@ private void SendEmail(string mailTo, string mailCc, string mailBcc, string subj using (var client = _MailKitProvider.SmtpClient) { - client.Send(mimeMessage); + try + { + client?.Send(mimeMessage); + } + catch (Exception ex) + { + throw ex; + } + finally + { + client?.Disconnect(true); + } } } } diff --git a/src/NETCore.MailKit/MailKitProvider.cs b/src/NETCore.MailKit/MailKitProvider.cs index de4feb7..0cbeec9 100644 --- a/src/NETCore.MailKit/MailKitProvider.cs +++ b/src/NETCore.MailKit/MailKitProvider.cs @@ -37,32 +37,42 @@ private Lazy lazySmtpClient() private SmtpClient InitSmtpClient() { - var client = new SmtpClient(); - - client.ServerCertificateValidationCallback = (s, c, h, e) => true; - - - if (!Options.Security) + try { - client.Connect(Options.Server, Options.Port, SecureSocketOptions.None); + var client = new SmtpClient(); + + client.CheckCertificateRevocation = false; + //设置timeout 避免长时间的等待 + client.Timeout = 5000; + client.ServerCertificateValidationCallback = (s, c, h, e) => true; + + + if (!Options.Security) + { + client.Connect(Options.Server, Options.Port, SecureSocketOptions.None); + } + else + { + // fix issue #6 + client.Connect(Options.Server, Options.Port, SecureSocketOptions.Auto); + } + + // Note: since we don't have an OAuth2 token, disable + // the XOAUTH2 authentication mechanism. + client.AuthenticationMechanisms.Remove("XOAUTH2"); + + // user login smtp server (fix issue #9) + if (!string.IsNullOrEmpty(Options.Account) && !string.IsNullOrEmpty(Options.Password)) + { + client.Authenticate(Options.Account, Options.Password); + } + + return client; } - else + catch(Exception e) { - // fix issue #6 - client.Connect(Options.Server, Options.Port, SecureSocketOptions.Auto); + throw e; } - - // Note: since we don't have an OAuth2 token, disable - // the XOAUTH2 authentication mechanism. - client.AuthenticationMechanisms.Remove("XOAUTH2"); - - // user login smtp server (fix issue #9) - if (!string.IsNullOrEmpty(Options.Account) && !string.IsNullOrEmpty(Options.Password)) - { - client.Authenticate(Options.Account, Options.Password); - } - - return client; } #endregion diff --git a/src/NETCore.MailKit/NETCore.MailKit.csproj b/src/NETCore.MailKit/NETCore.MailKit.csproj index 264ac45..0e6b9a2 100644 --- a/src/NETCore.MailKit/NETCore.MailKit.csproj +++ b/src/NETCore.MailKit/NETCore.MailKit.csproj @@ -10,21 +10,22 @@ netstandard2.1 True - 2.1.0 - Lvcc + 2.1.2 + jason MailKit extension for asp.net core. Easy send email in asp.net core project. - Copyright 2017 (c) Lvcc. All rights reserved - https://github.com/myloveCc/NETCore.MailKit - https://github.com/myloveCc/NETCore.MailKit + Copyright 2023 (c) Flame. All rights reserved + https://github.com/mswinner/NETCore.MailKit2 + https://github.com/mswinner/NETCore.MailKit2 MailKit ASP.NETCore git - 2.1.0 - 2.1.0 + 2.1.1 + 2.1.1 1: Update Mailkit to version 3.2.0. 2: Adds support for sending emails with attachments. #19 3: Update example sdk to .NET 6 + Flame.MailKit