繁体   English   中英

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

[英]Display confirmation message on email sent in ASP.NET

在我的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.");
        }

这个电话

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM