1

我是一名新的 ASP.NET 开发人员,我正在开发一个管理员/系统可以向用户发送电子邮件的项目。由于我不希望管理员等到系统发送电子邮件,我打算使用 SendAsync 方法。

但是,我遇到了一个非常奇怪的错误,即

“SmtpExecption 被捕获:发送邮件失败。”

我无法解决它。

供您参考,我使用的是 .NET Framework 4。

protected void btnSend_Click(object sender, EventArgs e)
    {
    try
            {
                Mailer mail = new Mailer();
                string senderE = ConfigurationManager.AppSettings["EmailID"].ToString();
                string senderName = "Test User"
                string recipientName = "TestSender";
                string subject = txtSubject.Text;
                string body = txtMessage.Text;
                mail.SendAsync(senderE, senderName, recipient, recipientName, subject, body);

                lblInfo.Text = "sent successfully :)";
                Response.Redirect("Test.aspx");
            }
            catch
            {
                lblInfo.Text = "Error!!!";
            }}

这是我的自定义 Mailer 类中的 SendAsync 方法的代码:

    public void SendAsync(string sender, string senderName, string recipient, string recipientName, string subject, string body)
        {
            var message = new MailMessage()
            {
                From = new MailAddress(sender, senderName),
                Subject = subject,
                Body = body,
                IsBodyHtml = true
            };
            message.To.Add(new MailAddress(recipient, recipientName));

            try
            {
                var client = new SmtpClient();
                client.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
                string userState = "Test";
                client.SendAsync(message, userState);
            }
            catch (Exception ex)
            {
                //handle exeption
                throw ex;
            }
            finally
            {
                //clean up.
                message.Dispose();
            }
        }

public static void MailDeliveryComplete(object sender, AsyncCompletedEventArgs e)
    {
        string message = "";
        // Get the unique identifier for this asynchronous operation.
        MailMessage mail = e.UserState as MailMessage;

        if (e.Error != null)
        {
            //handle error
            message = "Error " + e.Error.ToString() + " occurred";
        }
        else if (e.Cancelled)
        {
            //handle cancelled
            message = "Send canceled for mail with subject " + mail.ToString();
        }
        else
        {
            //handle sent email
            message = "Mail sent successfully";
        }

    }

那么我该如何解决呢?

更新#1:

顺便说一句,我将所有 SMTP 配置保存在 Web.Config 文件中。以下是设置:

<system.net>
    <mailSettings>
      <smtp from="My Gmail Account" deliveryMethod="Network">
        <network enableSsl="true" host="smtp.gmail.com" port="587" userName="Username" password="Password" defaultCredentials="false"/>
      </smtp>
    </mailSettings>
  </system.net>

更新#2: 在尝试了不同的建议后,我将 Async=True 添加到了 Page Directive 中,我收到了一条新的错误消息:

异步模块或处理程序已完成,而异步操作仍处于挂起状态。

我调试了代码,这次调试器进入 MailDeliveryMethod 并从以下块中抛出此错误:

if (e.Error != null)
        {
            //handle error
            message = "Error " + e.Error.ToString() + " occurred";
        }

关于我为什么会收到这种错误的任何想法?

4

0 回答 0