0

I have an ASP.NET Core 3.1 Web API project. I am using Entity Framework.

When I pass data statically to a smtp client, mail is sent and there is no problem:

SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("development@mail.com.tr", "password");
smtp.Port = 587;
smtp.Host = "mail.com.tr";
smtp.EnableSsl = false;

MailMessage mail = new MailMessage();
mail.To.Add("atakan@mail.com.tr");              
mail.From = new MailAddress("development@mail.com.tr");
mail.Subject = PostaSubject;
mail.IsBodyHtml = true;
mail.Body = MailTemplate(subject, subject);

smtp.Send(mail);

But when I get the smtp client data from the data in the context, sometimes the mail is sent, sometimes it cannot be thrown and it works unstable. How can I fix it?

SmtpClient smtp = new SmtpClient();
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(_context.MailConfigs.Select(x => x.UserName).FirstOrDefault(), _context.MailConfigs.Select(x => x.UserPassword).FirstOrDefault());
smtp.Port = (int)_context.MailConfigs.Select(x => x.SmtpPort).FirstOrDefault();
smtp.Host = _context.MailConfigs.Select(x => x.SmtpHost).FirstOrDefault();
smtp.EnableSsl = false;

MailMessage mail = new MailMessage();
mail.To.Add(_context.MailConfigs.Select(x => x.Reciever).FirstOrDefault());               
mail.From = new MailAddress(_context.MailConfigs.Select(x => x.UserName).FirstOrDefault());
mail.Subject = PostaSubject;
mail.IsBodyHtml = true;
mail.Body = MailTemplate(subject, subject);

smtp.Send(mail);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    You could add logging to make sure the values you get from the database are what you expect – stuartd Aug 01 '22 at 19:34
  • In addition to the suggestion about logging, also if you haven't already, add a try-catch and try to capture the exception while sending the mail – degant Aug 01 '22 at 19:37
  • I have a try -catch block and I'm not getting any errors. In addition, I am sure that the data coming from the database is correct, when I run the program, it sends mail, but when I close it and open it again, it does not send mail. – Atakan Ertürk Aug 01 '22 at 19:42
  • Not sure if this is related at all because there's not enough info to troubleshoot or provide suggestions, but as a best practice you should always dispose both the `SmtpClient` and the `MailMessage` objects after use: https://stackoverflow.com/questions/7276375/what-are-best-practices-for-using-smtpclient-sendasync-and-dispose-under-net-4 – degant Aug 01 '22 at 19:52
  • First I would like to be damn sure that there are definitely values, and yes you mentioned there are, then go with `First()` for every `FirstOrDefault()`. Second, in your context's `MailConfigs` there seem more than one hosts too, so you are getting either of the host and one of them is failing. **Suggestion** - Test if you are able to send emails using all hosts successfully or not. – Vikram Singh Saini Aug 02 '22 at 03:51

1 Answers1

0

FirstOrDefault will return a value or null, why not use a string variable to capture the value of FirstOrDefault result and if it is not null then send the email or if the value is null then just ignore or write to a log.