简体   繁体   中英

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

Here my code

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

I have a curious problem, is about the message, this work when the user sends a message, always arrive on my email, but what I want is to display an alert window type "message sent" when the user gives the submit button but not achievement that shows the alert window ... so how can show this alert ???

Provided you have a ScriptManager on you page, this should work...

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

Answer to your second question

Ok, you're not actually calling client.Send() anywhere in your try block. This is why you are always seeing the 'Mensaje enviado!' alert message, when you're expecting it to fail.

Add the following line below client.EnableSsl = true;

client.Send(mail);   

If the message is sent without an exception being thrown, you will see the 'Mensaje enviado!' alert message.

If there was an exception, the code in your Catch block will run and should display the 'Por favor intentalo nuevamente' alert message instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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