SmtpFailedRecipientsException不起作用

时间:2011-02-07 15:39:14

标签: c# system.net.mail

我需要检查邮件是否已发送给现有收件人

这是我的代码

        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("nonexisting@gmail.com"));
            mail.Subject = title;
            mail.Body = content;
            smtpServer.Send(mailMessage);
        }
        catch (SmtpFailedRecipientsException ex)
        {
             //   never occures
        }

但是,当没有收件人

时,不会发生SmtpFailedRecipientsException

有没有办法配置SmtpServer来触发此异常?

2 个答案:

答案 0 :(得分:11)

问题是GMail在SMTP事务期间没有说用户无效。原因是垃圾邮件制造者过去常常执行字典攻击,寻找“bob@gmail.com”和“tom@gmail.com”等。任何未被标记为“不存在”的地址都将成为有效地址。因此,大多数SMTP服务器现在只接受SMTP事务期间的所有地址,并且只是静默地删除无效地址或稍后发送退回消息。在任何一种情况下,代码都无法确定这一点。

答案 1 :(得分:0)

    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);
                }