I have made a webstore using RazorPages for a hunting organization. Every Thursday they have a lottery where every terrain booked by a user for Saturday or Sunday goes into the lottery. Each user is able to win one terrain, and there is only one winner per terrain per day.
I have the program set up so that each winner receives an email telling them which terrain they won in the lottery. There are more users than terrains, so there will be someone that doesn't win any terrains. I gather all the winners and losers for each terrain on each date, and place them in a winner and loser list. Then I remove every winner from the loser list, so that a user will only either receive a mail notifying them of a win, or a mail notifying them of a loss.
The mails for the winners go smoothly, and everyone gets their mails.
I then run this code to send a mail to each of the losers:
public void SendLoserMails(List<string> distinctLosers){
foreach (string loserMail in distinctLosers)
{
_mailSender.SetRecipient(loserMail);
_mailSender.SetSubject("Du har dessverre ikke blitt tildelt jakt!");
_mailSender.SetContents("<html><body> <h1>Jaktdag ikke tildelt.</h1>" +
"Du har dessverre ikke blitt tildelt noen jaktdager i trekningen " + DateTime.Today.ToString("dd.MM.yyyy")
+ "<br /> <br />"
+ "Jaktkortet ditt vil bli lagt tilbake på profilen din og kan brukes ved neste bestilling"
+ "<br /> <br />"
+ "Med vennlig hilsen <br/> - Telemark Jeger- og Fiskerforening <br />" + loserMail
+ "</body></html>");
_mailSender.SendMail();
}
}
Every time I try it, I get through 2 iterations, meaning the first two receive a mail, and then the operation times out, and the rest of the losers never receive their mail, because it times out every time.
This is the MailSender class I use:
public class MailSender
{
private string Recipient;
private string Subject;
private string Contents;
private string SenderName;
private string SenderMail;
private string SmtpUsername;
private string SmtpPassword;
private string SmtpMail;
private SmtpClient smtpClient;
public MailSender() { }
public void SetRecipient(string recipient) => Recipient = recipient;
public void SetSubject(string subject) => Subject = subject;
public void SetContents(string contents) => Contents = contents;
public void SetSecrets(IConfiguration config)
{
SenderName = config.GetValue<string>("SMTP_SENDER_NAME");
SenderMail = config.GetValue<string>("SMTP_SENDER_MAIL");
SmtpUsername = config.GetValue<string>("SMTP_CRED_USERNAME");
SmtpPassword = config.GetValue<string>("SMTP_CRED_PASSWORD");
SmtpMail = config.GetValue<string>("SMTP_CRED_MAIL");
}
public void SendMail()
{
if (Recipient == default || Subject == default || Contents == default)
return;
try
{
var newMail = new MailMessage();
smtpClient = new SmtpClient(SmtpMail);
newMail.From = new MailAddress(SenderMail, SenderName);
newMail.To.Add(Recipient);
newMail.Subject = Subject;
newMail.IsBodyHtml = true;
newMail.Body = Contents;
smtpClient.EnableSsl = false;
smtpClient.Port = 587;
smtpClient.Timeout = 100000;
smtpClient.Credentials = new NetworkCredential(
SmtpUsername,
SmtpPassword);
smtpClient.Send(newMail);
}
catch (Exception ex)
{
Console.WriteLine("Error - " + ex.Message);
}
}
}
Is there any reason why it always times out after sending a couple emails to the losers?
Keep in mind that all the winners receive their emails every time. So the MailSender works to an extent.