I can send mails with no problem. But also my code should control that if the mail address is correct. If not it should go into the catch block. But it doesn't go into the catch. So it seems like all the mails I am trying send is being sent even the mail addresses aren't correct.enter code here Because of that I can't figure out if all the mails are sent to the mail addresses
using (var smtp = new SmtpClient())
{
smtp.Host = emailSetting.Host;
smtp.EnableSsl = emailSetting.EnableSSL ?? true;
smtp.Port = Convert.ToInt32(emailSetting.Port);
smtp.UseDefaultCredentials = false;
var credentials = new NetworkCredential
{
UserName = emailSetting.EmailAdress,
Password = emailSetting.Password
};
smtp.Credentials = credentials;
int sendMailCount = 0;
var x = serviceEmail.GetQueueEmail();
foreach (var email in x.ToList())
{
using (var mailMessage = new MailMessage())
{
try
{
mailMessage.From = new MailAddress(emailSetting.EmailAdress, emailSetting.FromDisplayName);
mailMessage.To.Add("xyz@hotmail.com");//wrong email address
mailMessage.Subject = email.Subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = email.Body;
email.Status = EmailStatus.SEND;
smtp.Send(mailMessage);
}
catch (Exception ex)
{
email.Status = EmailStatus.FAILED;
continue;
}
}
}