protected virtual void SendEmail()

in src/log4net/Appender/SmtpAppender.cs [292:340]


  protected virtual void SendEmail(string messageBody)
  {
    // Create and configure the smtp client
#pragma warning disable CS0618 // Type or member is obsolete
    using SmtpClient smtpClient = new();
#pragma warning restore CS0618 // Type or member is obsolete
    if (!string.IsNullOrEmpty(SmtpHost))
    {
      smtpClient.Host = SmtpHost;
    }
    smtpClient.Port = Port;
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.EnableSsl = EnableSsl;

    if (Authentication == SmtpAuthentication.Basic)
    {
      // Perform basic authentication
      smtpClient.Credentials = new System.Net.NetworkCredential(Username, Password);
    }
    else if (Authentication == SmtpAuthentication.Ntlm)
    {
      // Perform integrated authentication (NTLM)
      smtpClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
    }

    using var mailMessage = new MailMessage();
    mailMessage.Body = messageBody;
    mailMessage.BodyEncoding = BodyEncoding;
    mailMessage.From = new MailAddress(From.EnsureNotNull());
    mailMessage.To.Add(_to.EnsureNotNull());
    if (!string.IsNullOrEmpty(_cc))
    {
      mailMessage.CC.Add(_cc);
    }
    if (!string.IsNullOrEmpty(_bcc))
    {
      mailMessage.Bcc.Add(_bcc);
    }
    if (!string.IsNullOrEmpty(ReplyTo))
    {
      mailMessage.ReplyToList.Add(new MailAddress(ReplyTo));
    }
    mailMessage.Subject = Subject;
    mailMessage.SubjectEncoding = SubjectEncoding;
    mailMessage.Priority = Priority;

    // TODO: Consider using SendAsync to send the message without blocking. We would need a SendCompletedCallback to log errors.
    smtpClient.Send(mailMessage);
  }