serilog / serilog-sinks-email

A Serilog sink that writes events to SMTP email
Apache License 2.0
70 stars 68 forks source link

MailKit.Security.SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connection. #94

Closed tqk2811 closed 3 years ago

tqk2811 commented 3 years ago

Asp.net core 3.1

In Startup ConfigureServices

 services.AddLogging(builder =>
      {
        var emailConnInfo = new EmailConnectionInfo()
        {
          EnableSsl = true,
          IsBodyHtml = false,
          FromEmail = appsettings.EmailFrom,
          MailServer = appsettings.SmtpHost,
          Port = appsettings.SmtpPort,
          ToEmail = appsettings.EmailSendLogging,
          EmailSubject = "Error",
          NetworkCredentials = new NetworkCredential(appsettings.SmtpUser, appsettings.SmtpPass)
        };
        builder.AddConfiguration(Configuration.GetSection("Logging"));
        builder.AddSerilog(new LoggerConfiguration().MinimumLevel.Information()
          .Enrich.FromLogContext()
          .WriteTo.File("serilog.txt",LogEventLevel.Information)
          .WriteTo.Email(emailConnInfo, new JsonFormatter(), LogEventLevel.Information, 1, TimeSpan.FromSeconds(5))
          .CreateLogger());
        builder.AddConsole();
#if DEBUG
        builder.AddDebug();
#endif
      });

But System.Net.Mail.SmtpClient send mail success

 public void Send(string to, string subject, string html, string from = null, string displayName = null)
    {
      if (string.IsNullOrEmpty(from)) from = _appSettings.EmailFrom;
      if (string.IsNullOrEmpty(displayName)) displayName = _appSettings.EmailDisplayName;

      using MailMessage mailMessage = new MailMessage();
      mailMessage.From = new MailAddress(from, displayName, Encoding.UTF8);
      mailMessage.To.Add(new MailAddress(to));
      mailMessage.Subject = subject;
      mailMessage.Body = html;
      mailMessage.IsBodyHtml = true;

      using SmtpClient smtp = new SmtpClient(_appSettings.SmtpHost,_appSettings.SmtpPort);
      smtp.EnableSsl = true;
      smtp.Credentials = new NetworkCredential(_appSettings.SmtpUser, _appSettings.SmtpPass);
      smtp.Send(mailMessage);
    }

Am i missing something? Thank!

tqk2811 commented 3 years ago

Sorry, i using wrong port smtp.gmail.com | StartTLS | 587 => smtp.gmail.com | SSL | 465