ASP.Net 中的警报消息 System.Net.Mail

Alert Message in ASP.Net with System.Net.Mail

这是我的代码

  protected void Unnamed_Click(object sender, EventArgs e)
    {
        try
        {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(ConfigurationManager.AppSettings["EmailTo"].ToString());
            mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailUse"].ToString(), "Contacto_PortalWeb", System.Text.Encoding.UTF8);
            mail.Subject = txtsubject.Text;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = "Nombre: " + txtName.Text + "<br/><br/>Correo: " + txtemail.Text + "<br/><br/>Telefóno: " + txtphone.Text + "<br/><br/>Compañia: " + txtcmpnm.Text + "<br/><br/>Contenido: <br/>" + txtmsg.Text;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            //SMTP CLIENT
            SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["smtp"].ToString());
            client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["EmailUse"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
            client.Port = int.Parse(ConfigurationManager.AppSettings["Port"].ToString());
            client.Host = ConfigurationManager.AppSettings["smtp"].ToString();
            client.EnableSsl = true;

            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "alertMessage", "alert('Mensaje enviado!');", true);
            CleartextBoxes(this);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            //ClientScript.RegisterStartupScript(Page.GetType(), "UserMsg", "<script type='text / javascript'>alert('Not send');if(alert){ window.location='contact.aspx';}</script>", true);

            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "alertMessage", "alert('Por favor intentalo nuevamente ' "+ex2.ToString()+");", true);
        }
    }

我有一个奇怪的问题,是关于消息的,当用户发送消息时,这项工作总是到达我的电子邮件,但我想要的是显示一个警报 window 类型 "message sent" 当用户给出提交按钮而不是显示警报的成就时 window ...那么如何显示此警报???

如果您的页面上有 ScriptManager,这应该可以工作...

 ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "alertMessage","alert('Message sent!)');", true);

第二个问题的答案

好的,您实际上并没有在 try 块中的任何地方调用 client.Send()。这就是为什么您总是看到 'Mensaje enviado!' 警报消息,而您却以为它会失败。

在下面添加以下行 client.EnableSsl = true;

client.Send(mail);   

如果发送消息时没有抛出异常,您将看到 'Mensaje enviado!' 警报消息。

如果出现异常,您的 Catch 块中的代码将 运行 并且应该显示 'Por favor intentalo nuevamente' 警报消息。