SmtpFailedRecipientsException Not working

3.9k Views Asked by At

I need to check if mail was send to existing recipient

Here is my code

        try
        {
            var smtpServer = new SmtpClient("smtp.gmail.com", 587)
            {
               Credentials = new System.Net.NetworkCredential(MAIL_FROM, PASSWORD),
               EnableSsl = true
            };

            var mail = new MailMessage();
            mail.From = MAIL_FROM
            mail.To.Add(new MailAddress("[email protected]"));
            mail.Subject = title;
            mail.Body = content;
            smtpServer.Send(mailMessage);
        }
        catch (SmtpFailedRecipientsException ex)
        {
             //   never occures
        }

But SmtpFailedRecipientsException never occures when there is no recipient

Is there a way to configure SmtpServer to fire this exception?

2

There are 2 best solutions below

2
On BEST ANSWER

The problem is that GMail doesn't say that the user is invalid during the SMTP transaction. The reason is that spammers used to perform dictionary attacks looking for "[email protected]" and "[email protected]", etc. Any address that didn't get marked as "not there" would then be a valid address. So most SMTP servers now just accept all addresses during the SMTP transaction and either just silently drop the invalid ones or send a bounce message later. In either case there's no way in code to determine this.

1
On
    in source view
    ------------------------------------------------------------------------------------------
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head runat="server">
            <title>Send Mail using asp.net</title>
            </head>
            <body>
            <form id="form1" runat="server">
            <div>
            <table style=" border:1px solid" align="center">
            <tr>
            <td colspan="2" align="center">
            <b>Send Mail using asp.net</b>
            </td>
            </tr>
            <tr>
            <td>
            From:
            </td>
            <td>
            <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
            </td>
            </tr>
            <tr>
            <td>
            Subject:
            </td>
            <td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
            </td>
            </tr>
            <tr>
            <td>
            To:
            </td>
            <td>
            <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
            </td>
            </tr>
            <tr>
            <td valign="top">
            Body:
            </td>
            <td>
            <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
            </td>
            </tr>
            <tr>
            <td>
            </td>
            <td>
            <asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
            </td>
            </tr>
            </table>
            </div>
            </form>
            </body>
            </html>
    ---------------------------------------------------------------------------------------In codeview
------------
         namespace  using system.net;
         btnSubmit_click(object sender,eventargs e)
        {
             MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = txtFrom.Text;
                // Recipient e-mail address.
                Msg.To = txtTo.Text;
                Msg.Subject = txtSubject.Text;
                Msg.Body = txtBody.Text;
                // your remote SMTP server IP.
                SmtpMail.SmtpServer = "10.20.72.1";
                SmtpMail.Send(Msg);
                Msg = null;
                Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
                }
                catch (Exception ex)
                {
                Console.WriteLine("{0} Exception caught.", ex);
                }