在ASP.NET中发送的电子邮件上显示确认消息

时间:2016-02-25 06:42:29

标签: c# asp.net email

在我的ASP.NET应用程序中,我无法向用户显示电子邮件发送失败或成功的确认消息。我使用以下代码发送电子邮件。该怎么做?

 public void SendMail()
        {
            try
            {
                login = new NetworkCredential("leavesmanagement@nworks.co", "password123456");
                client = new SmtpClient("smtp.1and1.com");
                client.Port = Convert.ToInt32(25);
                client.EnableSsl = true;
                client.Credentials = login;
                msg = new MailMessage { From = new MailAddress("leaves@nworks.co", "nWorks Employee", Encoding.UTF8) };
                msg.To.Add(new MailAddress(this.TextBoxEmail_Mobile.Text));
                msg.Subject = "Recover Password For Your nWorks Leave Management Account";
                msg.CC.Add(new MailAddress("dipak.a.akhade9192@gmail.com"));
                //       msg.CC.Add(new MailAddress("*************user mail id*************"));
                string strBody = string.Empty;
                Guid code = Guid.NewGuid();

                strBody += "<html><head></head><body><p>Click the following link to recover your password.</p>";
                strBody += Environment.NewLine;

                strBody += "<form action='' method='POST' name='myForm1'><a href='http://localhost:19343/ResetPasswordForm.aspx?guid="+code.ToString()+"&userid="+getUser()+"'><b>Reset Password</b></form><br>";


                strBody += "<br/>Thanks.</body></html>";

                msg.Body = strBody;

                msg.BodyEncoding = Encoding.UTF8;
                msg.IsBodyHtml = true;
                msg.Priority = MailPriority.Normal;
                msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                string userstate = "sending.......";
                client.SendAsync(msg, userstate);

                string q2 = "select count(*) from RecoverPwdStatus where uid='"+getUser()+"';";
                MySqlCommand cmd2 = new MySqlCommand(q2, conn);
                conn.Open();
                if ((long)(cmd2.ExecuteScalar() ?? 0) == 0)//user is not exist
                {
                    conn.Close();
                    string q1 = "insert into RecoverPwdStatus values('" + getUser() + "','" + code.ToString() + "');";
                    MySqlCommand cmd1 = new MySqlCommand(q1, conn);
                    conn.Open();
                    MySqlDataReader rdr1 = cmd1.ExecuteReader();
                    conn.Close();
                }
                else 
                {
                    conn.Close();
                    string q3 = "update recoverpwdstatus set guidcode='"+code.ToString()+"' where uid='"+getUser()+"';";
                    MySqlCommand cmd3 = new MySqlCommand(q3, conn);
                    conn.Open();
                    MySqlDataReader rdr3 = cmd3.ExecuteReader();
                    conn.Close();
                }
            }
            catch(Exception ex )
            {
                LabelMessage.Text = ex.Message;
            }
        }

//显示确认消息的方法如下所示,我需要使用标签或消息框显示此消息。我只需要解决方案。

 private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            var result = new TaskCompletionSource<string>();
            if (e.Cancelled)
                result.SetResult("Sending Mail is Canceled...!!");
            if (e.Error != null)
                result.SetResult(string.Format("{0} {1}", e.UserState, e.Error));
            else
                result.SetResult("Email sent successfully.");
        }

1 个答案:

答案 0 :(得分:0)

此次电话

  client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);  
  client.SendAsync(msg, userstate);

是异步的,这意味着在页面被发送给用户之后可能被调用并且您丢失了信息。它适用于应用程序,但不适用于网页和Web应用程序。