需要向多个用户发送电子邮件

Lahiru Sandaruwan

我开发了一个简单的警报系统,该系统将读取SQL中的表并相应地发送电子邮件。该SQL表中有要发送的用户名,电子邮件地址和消息。系统将每20分钟读取一次表格,并将根据用户各自的电子邮件地址向用户发送电子邮件。但是目前,系统仅向一个用户发送电子邮件。我想进一步开发该系统,以在一组完成后向多个用户发送电子邮件。我不知道如何执行此操作。有没有人可以帮助我。代码段将更有助于理解。

下面是SQL表模板

Name  | Email              | Factory| AlertTime| Description
User1 | [email protected] | FAC1   | 01:50:00 | UserMessage1
User1 | [email protected] | FAC2   | 01:50:00 | UserMessage2
User1 | [email protected] | FAC3   | 03:00:00 | UserMessage3

User2 | [email protected] | FAC1   | 01:20:00 | UserMessage1
User2 | [email protected] | FAC2   | 01:50:00 | UserMessage2
User2 | [email protected] | FAC3   | 03:00:00 | UserMessage3

User3 | [email protected] | FAC1   |  01:20:00 | UserMessage1
User3 | [email protected] | FAC2   |  01:50:00 | UserMessage2
User3 | [email protected] | FAC3   |  03:00:00 | UserMessage3

下面是我的C#线

    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 = "[email protected]";
                    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("[email protected]", "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
                }
            }

        }
    }
Lahiru Sandaruwan

我找到了解决方案。我对代码进行了一些更改,现在它可以按照我希望的方式运行。下面是我的代码。感谢大家的帮助。

    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 = "[email protected]";
                        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("[email protected]", "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 = "[email protected]";
                string from = "[email protected]";
                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("[email protected]", "sl@ithd");
                clnt.Send(msg);
            }
            #endregion      
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章