簡體   English   中英

如果值為 0 發送電子郵件 C# WPF

[英]If value 0 send e-mail C# WPF

當我的 Datagrid 中的項目更新時,一封電子郵件會發送到我的 gmail。 這很好用!

我想做出改變。 email 只能在 [Stock].Amount 更新為 0 時發送。希望任何人都可以幫助我。

這是我的查詢:

 private void BtnSend_Click(object sender, RoutedEventArgs e)
    {

        DataRowView o = (DataRowView)g2.SelectedItem;
        int id = Convert.ToInt32(o.Row.ItemArray[0]);
        int Total = Convert.ToInt32(o.Row.ItemArray[5]);

        try
        {
            const string query = @"UPDATE [Stock] SET [Stock].Amount = @Total WHERE [Stock].Id = @Id;";
            using (SqlConnection con = new SqlConnection(connectionstring))
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id;
                cmd.Parameters.Add("@Total", SqlDbType.Int).Value = Total;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("update completed successfully");
            binddatagrid();

            {
                    SqlCommand second = new SqlCommand($"SELECT COUNT(*) FROM [Stock] WHERE [Stock].Amount = 0;'");
                    var fromAddress = new MailAddress("FROM-Email", "From Name");
                    var toAddress = new MailAddress("To-Email", "To Name");
                    const string fromPassword = "*****";
                    const string subject = "Test";
                    const string body = "text";
                    var smtp = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                        Timeout = 20000
                    };
                    using (var message = new MailMessage(fromAddress, toAddress)
                    {
                        Subject = subject,
                        Body = body
                    })
                    {
                        smtp.Send(message);
                        MessageBox.Show("E-mail has been sent.", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                }
        }
        catch
        {
            MessageBox.Show("A field was entered incorrectly. Correct this and try adding the information again.", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
        }

如果Total不是0 ,您可以在發送任何 email 之前從該方法返回:

private void BtnSend_Click(object sender, RoutedEventArgs e)
{

    DataRowView o = (DataRowView)g2.SelectedItem;
    int id = Convert.ToInt32(o.Row.ItemArray[0]);
    int Total = Convert.ToInt32(o.Row.ItemArray[5]);
    if (Total != 0) //< --
        return;

    try
    {
        //same code as before...
    }
    catch
    {
        MessageBox.Show("A field was entered incorrectly. Correct this and try adding the information again.", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM