FUNC/SMTP - System.Net.Mail.SmtpException: The operation has timed out

I haver a function where i call smtp function and send email. This email is scheduled using system agent. very recently, i started seeing this timeout issues, not consistent but random. when you run manually, they run well.

SMTP timeouts are going to happen whenever- whether that’s automated or not.

At least in my experience.

What are you looking for info on or help with?

I was looking if any settings or solution that can fix this error

I see, other than seeing if there are settings to increase the re-try period idk.

Most people like to ask for the code that you’re using to send this (without any personal info in it), so they can see what they’re working with and what options might be available to you.

-Utah

DateTime startDate = DateTime.Now.Date.AddDays(-4);/* your start date here /;
DateTime endDate = DateTime.Now.Date; /
your end date here */;
decimal thresholdHours = 37.5M;

// Step 1: Get all supervisors
var supervisors = (from sup in Db.EmpBasic
where sup.EmpStatus == “A” && sup.Company == “***”
select new { sup.EmpID, sup.Name, sup.EMailAddress }).ToList();

foreach (var supervisor in supervisors)
{
// Step 2: Get all employees reporting to this supervisor
var employees = (from emp in Db.EmpBasic
where emp.SupervisorID == supervisor.EmpID && emp.EmpStatus == “A” && emp.Company == “**” && emp.Shift == 001
select new { emp.EmpID, emp.Name }).ToList();

List<string> lowHourEmployees = new List<string>();

foreach (var emp in employees)
{
    // Step 3: Get total hours for employee in date range
    var totalHours = (from ld in Db.LaborDtl
                      where ld.EmployeeNum == emp.EmpID
                            && ld.PayrollDate >= startDate
                            && ld.PayrollDate <= endDate
                            && ld.Company == "****"
                      select ld.LaborHrs).DefaultIfEmpty(0).Sum();

    if (totalHours < thresholdHours)
    {
        lowHourEmployees.Add($"{emp.Name} ({emp.EmpID}): {totalHours} hrs");
    }
}

if (lowHourEmployees.Any())
{
    // Step 4: Send email to the supervisor
    string subject = $"Canada OTT:Employees with low or missing time entry";
    string body = $"<p>Please reveiw the following employees who have logged less than {thresholdHours} hours between {startDate:d} and {endDate:d}:</p><ul>";
    foreach (var emp in lowHourEmployees)
    {
    //body += string.Join("\n", lowHourEmployees);
    body += $"<li>{emp}</li>";
    }
    body += "</ul>";
    string Email = supervisor.EMailAddress;
    string ccEmail = "****";
     // Send email
          var mailMessage = new System.Net.Mail.MailMessage();
          mailMessage.To.Add(Email);
          mailMessage.CC.Add(ccEmail);
          mailMessage.Subject = subject;
          mailMessage.Body = body;
          mailMessage.IsBodyHtml = true; // Set to true if you are sending HTML content
          mailMessage.From = new System.Net.Mail.MailAddress("*****"); // Replace with your actual email address
           // Configure SMTP client
          var smtpClient = new System.Net.Mail.SmtpClient("mail.smtp2go.com"); // Replace with your SMTP server
          smtpClient.Port = ***; // Replace with your SMTP port
          smtpClient.Credentials = new System.Net.NetworkCredential("***@****.com", "*****"); // Replace with your SMTP credentials
          smtpClient.EnableSsl = false; // Set to true if your SMTP server requires SSL
          // Send the email
          smtpClient.Send(mailMessage);
}

}

Other than switching mail providers for a more reliable one (i.e. sendgrid) I’d need someone who understands the objects you’re calling to see if there’s a way to re-try, but I don’t think there is.

We’ll wait for others to chime in.

At the end of the day smtp2go is timing out- what you can do about it client side, through code/execption handling, I’m not sure… At that point you’re working around a service issue on their end, at which point it may make more sense to switch to something that doesn’t time out so often.

Hey @Sam_Jose Do you have SMTP setup in Email Setting Maintenance? If so, you can updated your code to use that vs creating your own SMTP service.

Here is a snippit of code that can get you going using the built in SMTP vs creating your own in code.

// Initialize email service and message components
var mailer = this.GetMailer(async: false); // Get synchronous mailer instance
var message = new Ice.Mail.SmtpMail(); // Create new SMTP email message

// Configure email parameters
var from = “info@XXX.com”; // Sender email address
var to = “XX@X.com;”; // Recipient (with optional CC commented out)
//var to = TaskEmail; // + “; XXX@XXX.com;”; // Recipient (with optional CC commented out)
var subject = $“Action Required: ECO #{this.ECONum} - New Task Assignment”; // Professional subject line

// Create professional email body with proper formatting
var body = $@“Dear"d message. Please do not reply to this email.”;

// Set message properties with enhanced formatting and send
message.SetFrom(from); // Set sender address
message.SetTo(to); // Set recipient address
message.SetSubject(subject); // Set professional subject line
message.SetBody(body); // Set formatted email body content

// Optional: Set message priority for urgent ECOs
// message.Priority = Ice.Mail.MailPriority.High;

// Optional: Add HTML formatting for better presentation
// message.IsBodyHtml = true; // Enable if HTML body formatting is desired

mailer.Send(message); // Send the professional email message

Nice share @knash! Hence the reason I was asking @Sam_Jose how they were doing it.