當我的 Datagrid 中的專案更新時,一封電子郵件會發送到我的 gmail。這很好用!
我想做出改變。僅當 [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);
}
uj5u.com熱心網友回復:
Total如果不是以下內容,您可以在發送任何電子郵件之前從該方法回傳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]);
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/414734.html
標籤:
下一篇:谷歌云厘米意圖不向用戶發送郵件
