0

Smtp client has the following:

    SmtpClient client = new SmtpClient(server);
    client.UseDefaultCredentials = true;

using these credentials I can send emails.

Now when trying to hook Serilog email sink there is no such option, it wants actual username&password:

new EmailConnectionInfo
                    {
                        FromEmail = "",
                        ToEmail = "",
                        MailServer = "",

                        //I want default credentials, not this
                        NetworkCredentials = new NetworkCredential
                        {
                            UserName = "",
                            Password = ""
                        },

                        EnableSsl = false,
                        ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => true,
                        Port = 587,
                        EmailSubject = ""
                    }

Is there a way to make it accept default credentials?

Nick Farsi
  • 366
  • 4
  • 19
  • The [email sink is open source](https://github.com/serilog/serilog-sinks-email/tree/dev/src/Serilog.Sinks.Email/Sinks/Email), and pretty simple code to read. Have you tried looking at it to see if there's a way of setting it to use default credentials? Or considered forking or submitting a PR that adds the ability to UseDefaultCredentials? – mason Mar 05 '22 at 17:03
  • With just a tiny amount of exploration, [we can see that by setting NetworkCredentials to NULL, it will set UseDefaultCredentials to true](https://github.com/serilog/serilog-sinks-email/blob/644fe13c006ed470aaa7ce4774f224cca73fa7b7/src/Serilog.Sinks.Email/Sinks/Email/SystemMailEmailTransport.cs#L64). – mason Mar 05 '22 at 17:04
  • good thinking, but this does not work for some reason: 2022-03-05T17:22:05.6441123Z Failed to send email: MailKit.ServiceNotAuthenticatedException: 5.7.1 Client was not authenticated at MailKit.Net.Smtp.SmtpClient.ProcessMailFromResponse(MimeMessage message, MailboxAddress mailbox, SmtpResponse response) at MailKit.Net.Smtp.SmtpClient.FlushCommandQueueAsync(MimeMessage message, MailboxAddress sender, IList`1 recipients, Boolean doAsync, CancellationToken cancellationToken) I'm using the latest version – Nick Farsi Mar 05 '22 at 17:30
  • Okay, so you're using MailKit as the transport. Looks like [MailKit is more complicated](https://stackoverflow.com/questions/38113437/is-it-possible-to-use-default-network-credentials-with-mailkit-and-exchange). The same still applies: if it doesn't do what you want, you'll need to modify it to do what you want, or use something different. – mason Mar 05 '22 at 17:33
  • I don't have any MailKit packages. Where does it come from? Is it not part of the sink? – Nick Farsi Mar 05 '22 at 17:41
  • If we [look at the dependencies of Serilog.Sinks.Email on NuGet](https://www.nuget.org/packages/Serilog.Sinks.Email/2.4.0#dependencies-tab), we can see that on many frameworks, MailKit is indeed included. – mason Mar 05 '22 at 17:43

1 Answers1

0

Use port 25 and it will work with null credentials

Nick Farsi
  • 366
  • 4
  • 19