C# 按我希望的方式去做。下面是我的代码。谢谢大家的帮助 using System; using System.Data; using System.Timers; using System.Net.Mail; using
按我希望的方式去做。下面是我的代码。谢谢大家的帮助C# 按我希望的方式去做。下面是我的代码。谢谢大家的帮助 using System; using System.Data; using System.Timers; using System.Net.Mail; using,c#,sql,email,C#,Sql,Email,按我希望的方式去做。下面是我的代码。谢谢大家的帮助 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
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
}
}
使用系统;
使用系统数据;
使用系统计时器;
使用System.Net.Mail;
使用System.Windows.Forms;
使用系统、语音、合成;
使用System.Collections.Generic;
命名空间警报
{
公共部分类frmAlerts:表单
{
语音合成器;
Common ComMsg=新的Common();
数据集DatMsg=新数据集();
数据集DatNames=新数据集();
AlertException error=新的AlertException();
List AlertList=新列表();
字符串音调名称;
邮件串;
字符串TotMsg;
字符串\u名称;
公共frmAlerts()
{
初始化组件();
this.WindowState=FormWindowState.Minimized;
}
私有void frmAlerts\u加载(对象发送方,事件参数e)
{
尝试
{
System.Timers.Timer Timer=新的System.Timers.Timer(20*60*1000);
timer.appead+=新的ElapsedEventHandler(发送警报);
timer.Start();
}
捕获(例外情况除外)
{
MessageBox.Show(“应用程序加载错误:+ex.Message”);
SendtoAdmin();
}
}
public void SendAlerts(对象源,ElapsedEventArgs e)
{
尝试
{
DatNames=ComMsg.ReturnDataSet(“按名称描述从收件人订单中选择名称”);
对于(int i=0;i ”);
TotMsg=(j+1).ToString();
}
字符串to=ToEmail;
字符串from=”helpdesk@mydomain.com";
string subject=“及时提醒:您有“+TotMsg+”提醒”;
字符串msgBody=“亲爱的”+ToName+,
;
msgBody+=“您有”+TotMsg+“警报
”;
msgBody+=string.Join(“
”,AlertList);
msgBody+=“
问候由警报服务发送的
(请不要回复此电子邮件)。”;
MailMessage msg=新的MailMessage(发件人、收件人、主题、msgBody);
msg.IsBodyHtml=true;
SmtpClient clnt=新的SmtpClient(“outlook.mydomain.local”,25);
clnt.enablesl=false;
clnt.Credentials=新系统.Net.NetworkCredential(“helpdesk@mydomain.com“,”密码“);
clnt.Send(msg);
AlertList.Clear();
}
}
捕获(例外情况除外)
{
error.ExceptionMessage=ex.ToString();
speechSynthesizerObj=新的SpeechSynthesizer();
speechsynthezerobj.SpeakAsync(ex.Message);//说出错误
SendtoAdmin();
}
}
#地区发送邮件
受保护的void SendtoAdmin()
{
//发送邮件给管理员
字符串to=”admin@mydomain.com";
字符串from=”helpdesk@mydomain.com";
字符串subject=“系统故障”;
string msgBody=“尊敬的管理员,
警报系统出现系统故障。
请立即出席。
“+error.ExceptionMessage+”
问候警报系统发送的
”;
MailMessage msg=新的MailMessage(发件人、收件人、主题、msgBody);
msg.IsBodyHtml=true;
SmtpClient clnt=新的SmtpClient(“outlook.mydomain.local”,25);
clnt.enablesl=false;
clnt.Credentials=新系统.Net.NetworkCredential(“helpdesk@mydomain.com", "sl@ithd");
clnt.Send(msg);
}
#端区
}
}
如果删除“WHERE rptalerrecipient.Name='User1'”+
部分,您可以向所有人发送邮件。不过,请重新考虑for循环,因为您只是在覆盖找到的每个记录的值。如果你想发送多封邮件,请在该循环中发送一封电子邮件。你的期望是什么?您想连接所有警报消息、警报计数,并仅向每组发送一封电子邮件(user1、user2、user3)还是向每组发送三封电子邮件?@oerkelens我尝试了rem
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
}
}
}
}
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);
}
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
}
}