1

I am sending email using SmtpClient.Send Method, but when I try to send to a non-existent email account, the Send method throws no exception, how can I tell that the recipient mail address is valid, or the mail won't arrive, or detect this issue.

code:

            Properties.Settings appSettings = new Properties.Settings();
            MailMessage message = new MailMessage();

            message.From = new MailAddress(appSettings.senderMail);        
            message.Subject = appSettings.mailsubj;
            message.Body = appSettings.mailbody;
            Attachment attach = new Attachment(sOutput);
            message.Attachments.Add(attach);

            SmtpClient client = new SmtpClient(appSettings.SMTP);

            client.SendCompleted += this.SendCompletedCallback;
            foreach (String clientEmail in clientEmailList)
            {
                message.To.Clear();
                message.To.Add(clientEmail);
                //client.Send(message);

                try
                {
                    client.Send(message);
                    AddToLog(String.Format("\t{0}{1}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_SUCCESS"), clientEmail));
                }
                catch (SmtpFailedRecipientException ex)
                {
                    AddToLog(String.Format("\t{0}{1}:\n\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, ex.Message));
                }
                catch (SmtpException smtp_Ex)
                {
                    AddToLog(String.Format("\t{0}{1}:\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, "Cannot connect to SMTP server."));
                }
                catch (Exception ex)
                {
                    AddToLog(String.Format("\t{0}{1}:\n\t{2}", Properties.Resources.ResourceManager.GetString("MAIL_SEND_ERROR"), clientEmail, ex.Message));
                }
              //  client.SendAsync(message, clientEmail);
            }
David
  • 34,223
  • 3
  • 62
  • 80
AlexandruC
  • 3,527
  • 6
  • 51
  • 80

1 Answers1

3

When SMTP client sends a message, it doesn't know anything about validity of e-mail address, if this address belongs to the domain, that differs from SMTP server's domain.

This will be known only after your SMTP server will try to deliver a message to the recipient from another domain. Usually, in this case you'll receive a mail message with error.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I want to show and error, in that case, how do I find that out so I can display a message saying the mail hasn't been delivered – AlexandruC Oct 31 '13 at 18:59
  • You can't do that synchronously using SMTP. From the point of SMTP client the mail was sent. – Dennis Oct 31 '13 at 19:10
  • 1
    @A.K: [My answer to a similar question](http://stackoverflow.com/a/5052382/56778) explains in a little more detail. – Jim Mischel Oct 31 '13 at 19:22
  • I understand, but is there a possible way to interact with the server to ask him if the mail has been successfully sent, or a workaround for what I'm trying to achieve? – AlexandruC Nov 01 '13 at 07:04
  • 1
    @A.K: In the general case, no. It can take an arbitrary amount of time (milliseconds to days) to deliver a message or determine that the message is undeliverable. In the case of an undeliverable message, notification (if any is sent) is done by sending email back to the sender's address. But there is no requirement that servers send such notifications. – Jim Mischel Nov 01 '13 at 16:08