Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 通过mvc中的异常发送电子邮件_Asp.net Mvc_Email - Fatal编程技术网

Asp.net mvc 通过mvc中的异常发送电子邮件

Asp.net mvc 通过mvc中的异常发送电子邮件,asp.net-mvc,email,Asp.net Mvc,Email,我想从excel文件发送电子邮件。 当收到错误的电子邮件地址时,它将停止发送。 我希望它发送所有电子邮件,并在最后向我显示错误的电子邮件,是无法发送。 这是我读取Excel文件的代码: if (!string.IsNullOrWhiteSpace(excel)) { var src = excel; DataSet ds = new DataSet(); string fileExtension = Syst

我想从excel文件发送电子邮件。 当收到错误的电子邮件地址时,它将停止发送。 我希望它发送所有电子邮件,并在最后向我显示错误的电子邮件,是无法发送。 这是我读取Excel文件的代码:

if (!string.IsNullOrWhiteSpace(excel))
        {
            var src = excel;
            DataSet ds = new DataSet();
            string fileExtension = System.IO.Path.GetExtension(Server.MapPath("~/") + src);

            if (fileExtension == ".xls" || fileExtension == ".xlsx")
            {
                string fileLocation = Server.MapPath("~/") + src;

                string excelConnectionString = string.Empty;
                excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                if (fileExtension == ".xls")
                {
                    excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                    fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                }
                else if (fileExtension == ".xlsx")
                {
                    excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                    fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                }
                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                excelConnection.Open();
                DataTable dt = new DataTable();

                dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                if (dt == null)
                {
                    return null;
                }

                String[] excelSheets = new String[dt.Rows.Count];
                int t = 0;
                foreach (DataRow row in dt.Rows)
                {
                    excelSheets[t] = row["TABLE_NAME"].ToString();
                    t++;
                }
                OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);


                string query = string.Format("Select * from [{0}]", excelSheets[0]);
                using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                {
                    dataAdapter.Fill(ds);
                }
            }
            if (fileExtension.ToString().ToLower().Equals(".xml"))
            {
                string fileLocation = Server.MapPath("~/") + src;
                if (System.IO.File.Exists(fileLocation))
                {
                    System.IO.File.Delete(fileLocation);
                }

                Request.Files["FileUpload"].SaveAs(fileLocation);
                XmlTextReader xmlreader = new XmlTextReader(fileLocation);
                // DataSet ds = new DataSet();
                ds.ReadXml(xmlreader);
                xmlreader.Close();
            }
这是发送电子邮件的代码:

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string excelname = "";

                if (lang == "1")
                {
                    excelname = "<div style='text-align:right;'>" + ds.Tables[0].Rows[i][0].ToString() + "<br/>" + ds.Tables[0].Rows[i][1].ToString() + "</div>";
                }
                else
                {
                    excelname = "<div style='text-align:left;'>" + ds.Tables[0].Rows[i][0].ToString() + "<br/>" + ds.Tables[0].Rows[i][1].ToString() + "</div>";
                }
                exceltotal = excelname + text + newslink + attach;
                //sender
                message = string.Format(body, img, exceltotal, title, prehead, senderemail);
                MailMessage email = new MailMessage();
                email.To.Add(ds.Tables[0].Rows[i][2].ToString());
                email.From = new MailAddress(senderemail);
                email.Subject = maintitle;
                email.Body = message + makedelivey(ds.Tables[0].Rows[i][2].ToString(), maintitle);
                email.BodyEncoding = System.Text.Encoding.UTF8;
                email.IsBodyHtml = true;
                SmtpClient ssmtp = new SmtpClient();
                ssmtp.Host = server;
                ssmtp.Port = port;
                ssmtp.UseDefaultCredentials = false;
                ssmtp.Credentials = new System.Net.NetworkCredential(senderemail, senderpassword);
                ssmtp.EnableSsl = false;
                ssmtp.Send(email);
                exceltotal = "";
                message = "";
            }

它读取一个文本文件,并根据用户选择从excel或数据库中添加一些值。

我找不到发送电子邮件的代码行。但无论您在循环中发送电子邮件到哪里,请确保将该代码包含在try-catch块中。这样可以确保即使邮件发送操作失败,错误也会得到处理并继续处理其他邮件。您可以在catch块中包含失败问题的合并和格式化

   var consolidateErrors = string.Empty;
   for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
   {
      Try
       {
         //Mail sending logic here
       }
       Catch(Exception ex)
       {
         consolidateErrors +=  YourErrorDetails;
       }
   }

哪行代码发送电子邮件?