【问题标题】:Check email delivery failed asp.net c# [duplicate]检查电子邮件传递失败的asp.net c# [重复]
【发布时间】:2014-09-01 09:28:35
【问题描述】:

这是我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        client.EnableSsl = true;
        client.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "password");
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("xyz@gmail.com");
        msg.To.Add(new MailAddress(TextBox1.Text));
        msg.Subject = "Testing Email";
        String Body = "This is Testing Email.";
        msg.Body = Body;
        msg.IsBodyHtml = true;
        try
        {
            client.Send(msg);
            MsgBox("Send Successfully");
        }
        catch (Exception ex)
        {
            MsgBox("deleivery failed" + ex.ToString());
        }
    }
}

问题是,当我在 TextBox1 中输入正确的电子邮件 ID 时,会出现发送成功的警报,并且没有错误消息和 当我在 TextBox1 中输入错误的电子邮件 ID(如 12345xyz@gmail.com)时,还会出现发送成功的警报,并且没有错误消息,并且我在 xyz@gmail.com 收件箱中收到一封电子邮件发送失败的电子邮件。

我如何检查电子邮件是发送还是发送失败。 我正在使用 ASP.Net 和 C#。

【问题讨论】:

标签: c# asp.net


【解决方案1】:

将你的 catch 块异常类型更改为

 catch (SmtpFailedRecipientsException ex)
        {
           //Show error msg
        }

使用电子邮件发送时会引发 SmtpFailedRecipientsException SmtpClient 并且无法传递给所有收件人

添加Using System.Net.Mail

.也看SmtpException

【讨论】:

  • 这不起作用。
  • 我已经添加了 Using System.Net.Mail;
【解决方案2】:
   try
{
    client.Send(msg);
    Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
    Exception ex2 = ex;
    string errorMessage = string.Empty;
    while (ex2 != null)
    {
        errorMessage += ex2.ToString();
        ex2 = ex2.InnerException;
    }
    Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");
}

也许这会对你有所帮助?

【讨论】:

  • 此代码无效
  • 你要改变window.location吗?
猜你喜欢
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 2017-05-03
  • 2010-10-06
  • 2023-03-15
相关资源
最近更新 更多