【问题标题】:Adding alert message to server side submit button向服务器端提交按钮添加警报消息
【发布时间】:2013-02-19 23:02:07
【问题描述】:

美好的一天

我正在使用 asp.net 发送电子邮件。当点击“查询”或“提交”按钮时,电子邮件会发送,但我不知道如何让用户确认电子邮件已发送。

我该怎么做?

HTML:

<tr><td colspan="2"><asp:Button class="btn btn-success" ID="ButtonEnquire" OnClick="Button1_Click"  runat="server" Text="Enquire" /></td></tr>

服务器端代码隐藏:

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Web; 使用 System.Web.UI; 使用 System.Web.UI.WebControls;

namespace Contact.Secure
{
    public partial class Corporate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateDropDownTypeofCards();
            }
            //DropDownTypeofCards.DataSource = Enums.EnumList.CreateList(typeof(Enums.ContactMethodTypes));
            //DropDownList1.DataTextField = "EnumDescription";
            //DropDownList1.DataValueField = "EnumValue";

            //DropDownList1.DataBind();
        }

        private void PopulateDropDownTypeofCards()
        {
            DropDownTypeofCards.DataSource = Enums.EnumList.CreateList(typeof(Enums.CardTypes));
            DropDownTypeofCards.DataTextField = "EnumDescription";
            DropDownTypeofCards.DataValueField = "EnumValue";

            DropDownTypeofCards.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var html = string.Format("<html><head></head><body>{0}<br/>{1}<br />{2}<br />{3}<br />{4}<br />{5}</body></html>", TextBoxName.Text, TextBoxEmailAddress.Text, TextBoxCompanyName.Text, TextBoxCompanyAddress.Text, TextBoxPhoneNumber.Text, TextBoxAmiuntofCardstoSubscribe.Text, DropDownTypeofCards.Text);



            SendEmail(System.Configuration.ConfigurationManager.AppSettings["ContactUsFormSubmission"].ToString(), "Form Submission", html, true, "no-reply@mail.com", true, 587);

        }

        public static void SendEmail(string toEmail, string subject, string body, bool isHTML = false, string fromAddress = null, bool enableSSL = false, int SSLPort = 465, string[] Attachment = null)
        {


            try
            {
                if (bool.Parse(System.Configuration.ConfigurationManager.AppSettings["SendEmail"].ToString()))
                {
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

                    message.To.Add(toEmail);
                    message.Subject = subject;

                    if (string.IsNullOrEmpty(fromAddress) || string.IsNullOrWhiteSpace(fromAddress))
                    {
                        message.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["SMTPFromAddress"].ToString());
                    }
                    else
                    {

                        message.From = new System.Net.Mail.MailAddress(fromAddress);
                    }

                    message.IsBodyHtml = isHTML;

                    message.Body = body;

                    if (enableSSL)
                    {

                    }

                    if (Attachment != null)
                    {
                        if (Attachment.Count() > 0)
                        {
                            foreach (string a in Attachment)
                            {
                                System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(a);

                                message.Attachments.Add(attachment);
                            }
                        }
                    }
                    System.Net.Mail.SmtpClient smtp;
                    if (enableSSL)
                    {
                        smtp = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString(), SSLPort);
                        smtp.UseDefaultCredentials = false;
                    }
                    else
                    {
                        smtp = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString(), SSLPort);
                        smtp.UseDefaultCredentials = false;
                    }

                    smtp.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["SMTPUser"].ToString(), System.Configuration.ConfigurationManager.AppSettings["SMTPPassword"].ToString());

                    smtp.EnableSsl = enableSSL;

                    smtp.Timeout = 60000;


                    try
                    {
                        smtp.Send(message);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}

【问题讨论】:

标签: jquery asp.net html-email contact


【解决方案1】:

您可以将此代码添加到发送电子邮件的方法中,成功发送邮件后

var message = "Email has been sent";

string script = "<script language=\"javascript\"  type=\"text/javascript\">;alert('" + message + "');</script>";

ScriptManager.RegisterStartupScript(Page, this.GetType(), "AlertMessage", script, false);

它会在您的消息中显示 javascript 警报。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2021-03-10
    • 1970-01-01
    • 2018-05-07
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多