DataKnight11
Member
I'm working on a solution that involves sending a single email with notifications. However, I'm running into an issue where the function is timing out after 10 minutes, even though the email only contains a single row of data. Can anyone offer some guidance on how to optimize this process or suggest alternative approaches to avoid the timeout?
NotificationEmail class:
public class NotificationEmail : IDisposable
{
private readonly IConfiguration _config;
private readonly SmtpClient _smtpClient;
public NotificationEmail(IConfiguration config)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
var smtpHost = _config.GetValue<string>("Host") ?? "smtp-mail.outlook.com";
var smtpPort = _config.GetValue<int>("Port");
var smtpUser = _config.GetValue<string>("UserEmail");
var smtpPass = _config.GetValue<string>("PasswordEmail");
if (string.IsNullOrWhiteSpace(smtpUser) || string.IsNullOrWhiteSpace(smtpPass))
{
throw new InvalidOperationException("SMTP configuration is missing or invalid.");
}
_smtpClient = new SmtpClient(smtpHost)
{
Port = smtpPort,
Credentials = new NetworkCredential(smtpUser, smtpPass),
EnableSsl = true,
Timeout = 10000
};
}
public string GetHtmlTable(string title, DataTable table)
{
try
{
string messageBody = "<font>" + title + " </font><br><br>";
if (table.Rows.Count == 0)
return messageBody;
string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
string htmlTableEnd = "</table>";
string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
string htmlHeaderRowEnd = "</tr>";
string htmlTrStart = "<tr style =\"color:#555555;\">";
string htmlTrEnd = "</tr>";
string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
string htmlTdEnd = "</td>";
messageBody += htmlTableStart;
messageBody += htmlHeaderRowStart;
foreach (DataColumn column in table.Columns)
messageBody += htmlTdStart + WebUtility.HtmlEncode(column.ColumnName) + htmlTdEnd;
messageBody += htmlHeaderRowEnd;
foreach (DataRow row in table.Rows)
{
messageBody += htmlTrStart;
foreach (var item in row.ItemArray)
{
messageBody += htmlTdStart;
messageBody += WebUtility.HtmlEncode(item?.ToString() ?? string.Empty);
messageBody += htmlTdEnd;
}
messageBody += htmlTrEnd;
}
messageBody += htmlTableEnd;
return messageBody;
}
catch (Exception e)
{
return null;
}
}
public async Task SendEmailAsync(string subject, string body, string toEmail, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(toEmail))
{
throw new ArgumentException("Recipient email address is invalid.");
}
try
{
var fromEmail = _config.GetValue<string>("UserEmail");
using (var mailMessage = new MailMessage(fromEmail, toEmail))
{
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
Console.WriteLine("Sending email...");
await _smtpClient.SendMailAsync(mailMessage, cancellationToken);
Console.WriteLine("Email sent successfully.");
}
}
catch (SmtpException ex)
{
Console.WriteLine($"SMTP Exception: {ex.Message}");
throw new InvalidOperationException("An error occurred while sending the email. Please check your SMTP settings.", ex);
}
catch (OperationCanceledException)
{
Console.WriteLine("Email sending was canceled.");
throw;
}
catch (Exception ex)
{
Console.WriteLine($"General Exception: {ex.Message}");
throw new InvalidOperationException("An unexpected error occurred while sending the email.", ex);
}
}
public void Dispose()
{
_smtpClient?.Dispose();
}
}