Serilog - How to send mail programmatically

Cenk 1,036 Reputation points
2023-05-07T12:56:43.49+00:00

Hi,

I am working on a worker service. Here is my appsettings.json file;

I wonder if I can set the ToEmail inside of the worker?

{
        "Name": "CustomEmail",
        "Args": {
          "ConnectionInfo": {
            "NetworkCredentials": {
              "UserName": "******@palas.com",
              "Password": "*******"
            },
            "FromEmail": "******@palas.com",
            "MailServer": "smtp.gmail.com",
            "EmailSubject": "[{Level}] Game Code Email Sender For 3rd Party Services",
            "Port": "465",
            "IsBodyHtml": false,
            "EnableSsl": true,
            "ToEmail": "******@gmail.com",
            "RestrictedToMinimumLevel": "Information",
            "OutputTemplate": "{Message}{NewLine}"
          }

        }
      },

private async Task GetData()
        {
            var connString = _configuration["ConnectionStrings:Production"];
            await using var sqlConnection = new SqlConnection(connString);
            sqlConnection.Open();

            await using var command = new SqlCommand {Connection = sqlConnection};
            const string sql = @"select c.Pin, c.Serial, gcr.productCode, gcr.productDescription, gcr.purchaseStatusDate, gcr.totalPrice, o.orderBy, o.email, o.phone
                                    from GameConfirmResponses gcr
                                    inner join Coupons c ON gcr.Id = c.ConfirmResponseID
                                    inner join OrderDetail od ON od.referenceId = gcr.referenceId
                                    inner join Orders o On o.orderNo = gcr.clientTrxRef
                                    where od.status = 2";
            command.CommandText = sql;
            
            try
            {
                await using var reader = await command.ExecuteReaderAsync();
                while (reader.Read())
                {
                    _logger.LogInformation(
                        "* Coupon {Pin}, {Serial}, {productCode}, {productName}, {PurchaseDate}, {price}, {orderBy}, {email}, {phone} ",
                        reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3),
                        reader.GetDateTime(4), reader.GetDouble(5) , reader.GetString(6), reader.GetString(7), reader.GetString(8));

                    
                    //Here I want to send email with Serilog
                }
            }
            catch (SqlException exception)
            {
                _logger.LogError("Error Retrieving Data From OrderDetail: {Error} ", exception.Message);
               
            }
        }
Developer technologies ASP.NET ASP.NET Core
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.