简体   繁体   English

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

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

In my ASP.NET application I am unable to show confirmation message to user that email sending is failed or success. 在我的ASP.NET应用程序中,我无法向用户显示电子邮件发送失败或成功的确认消息。 I used following code for sending email. 我使用以下代码发送电子邮件。 What to do to do so? 怎么做呢?

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

// Method to display confirmation message as given bellow, I need to show this message using label or message box. //显示给定消息的确认消息的方法,我需要使用标签或消息框显示此消息。 I need solution over here only. 我只需要在这里解决。

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

This call 这个电话

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

is asynchronous, that is mean that after the page have been send to the user is probably called and you lose the information's. 是异步的,这意味着在将页面发送给用户之后,可能会调用该页面,而您丢失了该信息。 It works for an application but not for web page and web application that way. 它适用于应用程序,但不适用于网页和Web应用程序。

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

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