0

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;
                            }
                        }
                    }
T.Yuce
  • 11
  • 3
  • 1
    Why are you expecting `xyz@hotmail.com` to be considered a wrong/incorrect email address? It is perfectly valid from an Email Address standard point of view. – JayV Jun 11 '18 at 13:34
  • 2
    "I can't figure out if all the mails are sent to the mail addresses" - yes, that's normal. If you want to know if an email got delivered and read, you'll need them to click on a link or similar. It is perfectly normal to not know what happens when you send an email. As a metaphor: if I drop 200 letters into a postbox, I don't know if they are deliverable, either. – Marc Gravell Jun 11 '18 at 13:36
  • xyz@hotmail.com is perfectly fine email, which probably exists, try putting there an empty string and try again – styx Jun 11 '18 at 13:36
  • 1
    I think the OP is looking for an exception when the email is in correct format but does not exist. He put it in the title of its question :) – CodeNotFound Jun 11 '18 at 13:36
  • 1
    Presuming you want to detect an unknown recipient error you would need to deliver directly to the server indicated in the receiving domains MX records, you are most likely currently relaying through another server which is simply acknowledging your send request. – Alex K. Jun 11 '18 at 13:38
  • Possible duplicate of [Can I check if an email address exists using .net?](https://stackoverflow.com/questions/3883518/can-i-check-if-an-email-address-exists-using-net) – CodeNotFound Jun 11 '18 at 13:42
  • @JayV Email format is not important. Normally, when the mail can not be sent, the error catches into the code block. But it does not enter. – T.Yuce Jun 11 '18 at 13:51
  • Actually my question was not in email format. But I am sending the empty string.However, the catch does not fall into the code block. I can not understand. – T.Yuce Jun 11 '18 at 14:15
  • I am using outlook smtp. If I do not enter the format correctly or if I enter an email address that does not exist, the mail seems to be being sent and the mail status appears to be SEND listed on the page. Notification says "can't be delivered" to smtp mail address. – T.Yuce Jun 11 '18 at 14:33
  • "wrong" and "doesn't exist" are not the same thing when it comes to email addresses. [Mandatory XKCD](https://xkcd.com/1425/). – NetMage Jun 11 '18 at 23:54

0 Answers0