我开发了一个简单的警报系统,它将读取SQL中的表并相应地发送电子邮件。有用户名,电子邮件地址,要在该SQL表中发送的消息。系统将每20分钟读取一次表格,并根据各自的电子邮件地址向用户发送电子邮件。但目前系统只向一个用户发送电子邮件。我想进一步开发这个系统,以便在一组完成后向多个用户发送电子邮件。我不知道该怎么做。是否有人可以帮助我。代码片段对于理解更有帮助。
下面是SQL表格模板
Name | Email | Factory| AlertTime| Description
User1 | user1@mydomain.com | FAC1 | 01:50:00 | UserMessage1
User1 | user1@mydomain.com | FAC2 | 01:50:00 | UserMessage2
User1 | user1@mydomain.com | FAC3 | 03:00:00 | UserMessage3
User2 | user2@mydomain.com | FAC1 | 01:20:00 | UserMessage1
User2 | user2@mydomain.com | FAC2 | 01:50:00 | UserMessage2
User2 | user2@mydomain.com | FAC3 | 03:00:00 | UserMessage3
User3 | user3@mydomain.com | FAC1 | 01:20:00 | UserMessage1
User3 | user3@mydomain.com | FAC2 | 01:50:00 | UserMessage2
User3 | user3@mydomain.com | FAC3 | 03:00:00 | UserMessage3
以下是我的C#cord
using System;
using System.Timers;
using System.Windows.Forms;
using System.Net.Mail;
using System.Data;
using System.Speech.Synthesis;
using System.Collections.Generic;
namespace Alerts
{
public partial class frmAlerts : Form
{
SpeechSynthesizer speechSynthesizerObj;
Common ComMsg = new Common();
DataSet DatMsg = new DataSet();
AlertException error = new AlertException();
List<string> AlertList = new List<string>();
string ToName;
string ToEmail;
string TotMsg;
public frmAlerts()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
}
private void frmAlerts_Load(object sender, EventArgs e)
{
try
{
System.Timers.Timer timer = new System.Timers.Timer(20 * 60 * 1000);
timer.Elapsed += new ElapsedEventHandler(SendAlerts);
timer.Start();
}
catch (Exception ex)
{
MessageBox.Show("Error in application Load: " + ex.Message);
}
}
public void SendAlerts(object source, ElapsedEventArgs e)
{
try
{
DatMsg = ComMsg.ReturnDataSet("SELECT RptAlertRecipient.Name, RptAlertRecipient.Email, RptAlerts.Factory, RptAlerts.AlertTime, RptAlerts.Description " +
"FROM RptAlerts " +
"INNER JOIN RptAlertTypes ON RptAlerts.AlertTypeID = RptAlertTypes.ID " +
"INNER JOIN RptAlertType_RecipientMapping ON RptAlertTypes.ID = RptAlertType_RecipientMapping.AlertTypeID " +
"INNER JOIN RptAlertRecipient ON RptAlertType_RecipientMapping.AlertRecipientID = RptAlertRecipient.ID " +
"WHERE RptAlertRecipient.Name= 'User1' " +
"ORDER BY RptAlertRecipient.Name ASC");
for (int j = 0; j < DatMsg.Tables[0].Rows.Count; j++)
{
ToEmail = DatMsg.Tables[0].Rows[j].ItemArray.GetValue(1).ToString();
ToName = DatMsg.Tables[0].Rows[j].ItemArray.GetValue(0).ToString();
AlertList.Add(DatMsg.Tables[0].Rows[j].ItemArray.GetValue(4).ToString() + "<br/>");
TotMsg = (j + 1).ToString();
}
string to = ToEmail;
string from = "helpdesk@mydomain.com";
string subject = "Alert In Time : You Have "+TotMsg+ " Alerts";
string msgBody = "Dear " + ToName + ",<br/><br/>";
msgBody += "<b>You Have " + TotMsg + " Alerts</b><br/><br/>";
msgBody += string.Join("<br/>", AlertList);
msgBody += "<br/><br/>Regards<br/>Sent by Alert Service<br/>(Please do not reply to this email.)";
MailMessage msg = new MailMessage(from, to, subject, msgBody);
msg.IsBodyHtml = true;
SmtpClient clnt = new SmtpClient("outlook.mydomain.local", 25);
clnt.EnableSsl = false;
clnt.Credentials = new System.Net.NetworkCredential("helpdesk@mydomain.com", "password");
clnt.Send(msg);
}
catch (Exception ex)
{
error.ExceptionMessage = ex.ToString();//gets the exception message to a separate class
speechSynthesizerObj = new SpeechSynthesizer();
speechSynthesizerObj.SpeakAsync(ex.Message);//Speaks the Error
}
}
}
}
答案 0 :(得分:1)
我想您当前的代码只会向列表中的最后一个用户发送邮件吗?
您必须包含在for循环中生成和发送邮件的代码。
for (int j = 0; j < DatMsg.Tables[0].Rows.Count; j++)
{
ToEmail = DatMsg.Tables[0].Rows[j].ItemArray.GetValue(1).ToString();
ToName = DatMsg.Tables[0].Rows[j].ItemArray.GetValue(0).ToString();
AlertList.Add(DatMsg.Tables[0].Rows[j].ItemArray.GetValue(4).ToString() + "<br/>");
TotMsg = (j + 1).ToString();
string to = ToEmail;
string from = "helpdesk@mydomain.com";
string subject = "Alert In Time : You Have "+TotMsg+ " Alerts";
string msgBody = "Dear " + ToName + ",<br/><br/>";
msgBody += "<b>You Have " + TotMsg + " Alerts</b><br/><br/>";
msgBody += string.Join("<br/>", AlertList);
msgBody += "<br/><br/>Regards<br/>Sent by Alert Service<br/>(Please do not reply to this email.)";
MailMessage msg = new MailMessage(from, to, subject, msgBody);
msg.IsBodyHtml = true;
SmtpClient clnt = new SmtpClient("outlook.mydomain.local", 25);
clnt.EnableSsl = false;
clnt.Credentials = new System.Net.NetworkCredential("helpdesk@mydomain.com", "password");
clnt.Send(msg);
}
答案 1 :(得分:0)
我找到了解决方案。我对代码做了一些更改,现在它按照我希望的方式工作。以下是我的代码。感谢大家的帮助。
using System;
using System.Data;
using System.Timers;
using System.Net.Mail;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Collections.Generic;
namespace Alerts
{
public partial class frmAlerts : Form
{
SpeechSynthesizer speechSynthesizerObj;
Common ComMsg = new Common();
DataSet DatMsg = new DataSet();
DataSet DatNames = new DataSet();
AlertException error = new AlertException();
List<string> AlertList = new List<string>();
string ToName;
string ToEmail;
string TotMsg;
string _Name;
public frmAlerts()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
}
private void frmAlerts_Load(object sender, EventArgs e)
{
try
{
System.Timers.Timer timer = new System.Timers.Timer(20 * 60 * 1000);
timer.Elapsed += new ElapsedEventHandler(SendAlerts);
timer.Start();
}
catch (Exception ex)
{
MessageBox.Show("Error in application Load: " + ex.Message);
SendtoAdmin();
}
}
public void SendAlerts(object source, ElapsedEventArgs e)
{
try
{
DatNames = ComMsg.ReturnDataSet("SELECT Name FROM RptAlertRecipient ORDER BY Name DESC");
for (int i = 0; i < DatNames.Tables[0].Rows.Count; i++)
{
_Name = DatNames.Tables[0].Rows[i].ItemArray.GetValue(0).ToString();
DatMsg = ComMsg.ReturnDataSet("SELECT RptAlertRecipient.Name, RptAlertRecipient.Email, RptAlerts.Factory, RptAlerts.AlertTime, RptAlerts.Description " +
"FROM RptAlerts " +
"INNER JOIN RptAlertTypes ON RptAlerts.AlertTypeID = RptAlertTypes.ID " +
"INNER JOIN RptAlertType_RecipientMapping ON RptAlertTypes.ID = RptAlertType_RecipientMapping.AlertTypeID " +
"INNER JOIN RptAlertRecipient ON RptAlertType_RecipientMapping.AlertRecipientID = RptAlertRecipient.ID " +
"WHERE RptAlertRecipient.Name ='" + _Name + "'" +
"ORDER BY RptAlertRecipient.Name DESC");
for (int j = 0; j < DatMsg.Tables[0].Rows.Count; j++)
{
ToEmail = DatMsg.Tables[0].Rows[j].ItemArray.GetValue(1).ToString();
ToName = DatMsg.Tables[0].Rows[j].ItemArray.GetValue(0).ToString();
AlertList.Add(DatMsg.Tables[0].Rows[j].ItemArray.GetValue(4).ToString() + "<br/>");
TotMsg = (j + 1).ToString();
}
string to = ToEmail;
string from = "helpdesk@mydomain.com";
string subject = "Alert In Time : You Have " + TotMsg + " Alerts";
string msgBody = "Dear " + ToName + ",<br/><br/>";
msgBody += "<b>You Have " + TotMsg + " Alerts</b><br/><br/>";
msgBody += string.Join("<br/>", AlertList);
msgBody += "<br/><br/>Regards<br/>Sent by Alert Service<br/>(Please do not reply to this email.)";
MailMessage msg = new MailMessage(from, to, subject, msgBody);
msg.IsBodyHtml = true;
SmtpClient clnt = new SmtpClient("outlook.mydomain.local", 25);
clnt.EnableSsl = false;
clnt.Credentials = new System.Net.NetworkCredential("helpdesk@mydomain.com", "password");
clnt.Send(msg);
AlertList.Clear();
}
}
catch (Exception ex)
{
error.ExceptionMessage = ex.ToString();
speechSynthesizerObj = new SpeechSynthesizer();
speechSynthesizerObj.SpeakAsync(ex.Message);//Speaks the Error
SendtoAdmin();
}
}
#region SendMails
protected void SendtoAdmin()
{
//Send mail to Admin
string to = "admin@mydomain.com";
string from = "helpdesk@mydomain.com";
string subject = "System Failure";
string msgBody = "Dear Admin,<br/><br/>System Failure in Alert System.<br/>Please Attend Immediately.<br/>"+ error.ExceptionMessage + "<br/><br/>Regards<br/>Sent By Alert System";
MailMessage msg = new MailMessage(from, to, subject, msgBody);
msg.IsBodyHtml = true;
SmtpClient clnt = new SmtpClient("outlook.mydomain.local", 25);
clnt.EnableSsl = false;
clnt.Credentials = new System.Net.NetworkCredential("helpdesk@mydomain.com", "sl@ithd");
clnt.Send(msg);
}
#endregion
}
}