Proper way to asynchronously send an email in ASP.NET… (am i doing it right?)

前端 未结 9 1672
时光取名叫无心
时光取名叫无心 2020-12-23 10:57

When a user registers on my website, I don\'t see why I need to make him \"wait\" for the smtp to go through so that he gets an activation email.

I decided I want to

9条回答
  •  长情又很酷
    2020-12-23 11:18

    Use this way-

    private void email(object parameters)
        {
            Array arrayParameters = new object[2];
            arrayParameters = (Array)parameters;
            string Email = (string)arrayParameters.GetValue(0);
            string subjectEmail = (string)arrayParameters.GetValue(1);
            if (Email != "Email@email.com")
            {
                OnlineSearch OnlineResult = new OnlineSearch();
                try
                {
                    StringBuilder str = new StringBuilder();
                    MailMessage mailMessage = new MailMessage();
    
                    //here we set the address
                    mailMessage.From = fromAddress;
                    mailMessage.To.Add(Email);//here you can add multiple emailid
                    mailMessage.Subject = "";
                    //here we set add bcc address
                    //mailMessage.Bcc.Add(new MailAddress("bcc@site.com"));
                    str.Append("");
                    str.Append("");
                    str.Append("");
    
                    str.Append("
    "); str.Append(""); str.Append(""); //To determine email body is html or not mailMessage.IsBodyHtml = true; mailMessage.Body = str.ToString(); //file attachment for this e-mail message. Attachment attach = new Attachment(); mailMessage.Attachments.Add(attach); mailClient.Send(mailMessage); } } protected void btnEmail_Click(object sender, ImageClickEventArgs e) { try { string To = txtEmailTo.Text.Trim(); string[] parameters = new string[2]; parameters[0] = To; parameters[1] = PropCase(ViewState["StockStatusSub"].ToString()); Thread SendingThreads = new Thread(email); SendingThreads.Start(parameters); lblEmail.Visible = true; lblEmail.Text = "Email Send Successfully "; }

提交回复
热议问题