发送数量少的电子邮件产品 [英] Send email products with low quantity

查看:70
本文介绍了发送数量少的电子邮件产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好继续在我实施的asp.net/c#网站上遇到问题。

我有一个数据库,里面有字段(Id,Name,Price,Quantity)当产品数量少于< 5时,通过以下代码向产品ID和名称发送电子邮件我只发送第一个产品数量少的时候发送电子邮件如果其他任何产品数量少,它没有发送任何东西我应该发送什么改变才能正常工作?



Thnx提前!!!!



---- -------------------------------------------------- -

Hi guys continuing to have problems with asp.net/c# web site where i' implemeting.
I have a database with fields (Id,Name,Price,Quantity) i want to sen an email to administrator with products id and name when the quantity of the product is less<5 with the following code i send an email when only the first product has low quantity if any other products has low quantity it didn't send anything what should i change in order to works properly?

Thnx in advance!!!!

-------------------------------------------------------

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand("select Id, quantity from products", con);
        int var = 0;
        string pro = "";
        con.Open();
        using (SqlDataReader read = sqlCommand.ExecuteReader())
        {
            while (read.Read())
            {
                string numquant = read["quantity"].ToString();
                string Id1;
                Id1 = read["Id"].ToString();
                Response.Write(numquant);
                pro ="You product with low quantity is :"+ Convert.ToString(Id1);
                GridView1.DataSource = read;
                GridView1.DataBind();
                var = Convert.ToInt32(numquant);
            }
           
            
            int quantity = var;
            if (quantity < 5)
            {

                        
                MailMessage mailObj = new MailMessage(
                "from", "to", "product low quantity",pro);

                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.UseDefaultCredentials = false;
                    SMTPServer.Host = "smtp.gmail.com";
                    SMTPServer.Port = 587;
                    SMTPServer.Credentials = new NetworkCredential("email", "password");
                    SMTPServer.EnableSsl = true;
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    // Label1.Text = ex.ToString();
                }
                read.Close();
                con.Close();
            }
        }
    }      
}



--------------- -------------------------------------------------- ------


-----------------------------------------------------------------------

推荐答案

更改你的代码并尝试:



Change your code something like and try :

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand("select Id, quantity from products", con);
        int var = 0;
        string pro = "";
        con.Open();
        using (SqlDataReader read = sqlCommand.ExecuteReader())
        {
            while (read.Read())
            {
                string numquant = read["quantity"].ToString();
                string Id1;
                Id1 = read["Id"].ToString();
                Response.Write(numquant);
                pro ="You product with low quantity is :"+ Convert.ToString(Id1);
                
                var = Convert.ToInt32(numquant);
                if (var < 5)      
                  sendmail(pro);
            }
           
            GridView1.DataSource = read;
            GridView1.DataBind();
            read.Close();
            con.Close();
        }
    } 

    private void sendmail(string msg)     
    {
                 MailMessage mailObj = new MailMessage(
                "from", "to", "product low quantity",msg);
 
                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.UseDefaultCredentials = false;
                    SMTPServer.Host = "smtp.gmail.com";
                    SMTPServer.Port = 587;
                    SMTPServer.Credentials = new NetworkCredential("email", "password");
                    SMTPServer.EnableSsl = true;
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    // Label1.Text = ex.ToString();
                }
    }
}


嘿那里,



如果我没记错的话,我给了你另一个使用DataKeyNames发送电子邮件的解决方案。



无论如何,尝试移动在while循环内发送电子邮件的代码。



如果有效,请告诉我。 />


Azee ...
Hey there,

If I remember correctly, I gave you another solution to send email using DataKeyNames.

Anyway, try moving the code that sends email inside the while loop.

Let me know if it works.

Azee...


您好,



请检查这个。



在一封邮件中发送低产品数量详情

Hi,

Please check this.

Sending low product quantity details in a single mail
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        SqlCommand sqlCommand = new SqlCommand("select Id from products where quantity<5", con);
        int var = 0;
        string pro = "You product with low quantity are :";
        con.Open();
        using (SqlDataReader read = sqlCommand.ExecuteReader())
        {
            while (read.Read())
            {
                string Id1;
                Id1 = read["Id"].ToString();
                pro += Convert.ToString(Id1)+", ";                
            }
                pro = pro.Substring(0,pro.Length - 2);       
                MailMessage mailObj = new MailMessage(
                "from", "to", "product low quantity",pro);
 
                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.UseDefaultCredentials = false;
                    SMTPServer.Host = "smtp.gmail.com";
                    SMTPServer.Port = 587;
                    SMTPServer.Credentials = new NetworkCredential("email", "password");
                    SMTPServer.EnableSsl = true;
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    // Label1.Text = ex.ToString();
                }
                read.Close();
                con.Close();
        }
    }      
}


这篇关于发送数量少的电子邮件产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆