通过控制台应用程序自动发送邮件

时间:2015-06-26 07:13:32

标签: c# email console

为公司员工创建一个用于发送生日祝福邮件的控制台应用程序,这里只有一个员工的发送邮件代码我尝试但是无法从数据库中获得多个电子邮件ID

这里代码我试试:

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string connStr = ConfigurationManager.ConnectionStrings["EmpDBConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(connStr);
                con.Open();
                SqlCommand cmd;
                cmd = new SqlCommand("SELECT empemail,EmpDOB,empname,Photo,BdayMessage,CC FROM EmpDetails where datepart(day,EmpDOB)= datepart(day,sysdatetime()) and datepart(month,EmpDOB)=datepart(month,sysdatetime())", con);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    ArrayList list_emails = new ArrayList();
                    int i = 0;
                    string email, Bday;
                    int diff;
                    while (reader.Read())
                    {
                        email = reader.GetValue(i).ToString();
                        list_emails.Add(email); //Add email to a arraylist
                        i = i + 1 - 1;
                    }
                        string todaydate = DateTime.Now.ToString("dd/MM");
                        Bday = Convert.ToDateTime(reader["EmpDOB"]).ToString("dd/MM");         //date converted 
                        diff = DateTime.Compare(DateTime.Today, Convert.ToDateTime(Bday));     //compare cuurent date and date of birthday from database of emplyees with only day and month

                        //string Bday = Convert.ToDateTime(reader["EmpDOB"]).ToString("dd/MM");

                        if (diff == 0)
                        {
                            //string Email = reader["EmpEmail"].ToString();
                            string Name = reader["EmpName"].ToString();
                            string Photo = reader["Photo"].ToString();
                            string cc = reader["CC"].ToString();
                            string Bdaymessage = reader["BdayMessage"].ToString();
                            string body = "<br><b><i><font Size=4 face=Comic Sans MS color= #FF2B9E> Dear</br><br><font Size=4 face=Comic Sans MS color=#FF2B9E>" + Name + "</i></b>" + "<br>" + "<br><img src=" + Photo + "></body></html></body></html>" + "<br><b><font Size=4 face=Comic Sans MS color=#FF2B9E><i>" + Bdaymessage + "</i></b></br> " + "<br><br><br>";
                            AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, System.Net.Mime.MediaTypeNames.Text.Html);

                            LinkedResource inline = new LinkedResource(Photo, MediaTypeNames.Image.Jpeg);
                            inline.ContentId = Guid.NewGuid().ToString();
                            av.LinkedResources.Add(inline);

                            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

                            msg.AlternateViews.Add(av);
                            //msg.To.Add(email);
                            msg.CC.Add(cc);
                            msg.Subject = " WIsH YoU MaNy MaNY HaPPy Birthday";
                            msg.From = new System.Net.Mail.MailAddress("example@gmail.com");
                            SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
                            NetworkCredential NetCrd = new NetworkCredential("eample@gmail.com", "password");
                            mailClient.UseDefaultCredentials = false;
                            mailClient.Credentials = NetCrd;
                            mailClient.EnableSsl = true;
                            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                            mailClient.Send(msg);
                            reader.Close();
                            con.Close();
                        }
                        else
                        {
                            Console.Write("its a earlier date");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write("There is No Birthday" + ex);
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您正在从while循环中获取具有DataReader生日的数据库的多个用户。您只需要在循环内移动电子邮件发送代码。

答案 1 :(得分:0)

我建议您以excel格式提取人口报告(姓名,生日,雇用日期,电子邮件),并让wishing application处理一切

它至少需要两件 excel文件,其中包含愿望详细信息(日期,姓名,电子邮件)和配置文件( application.properties ),就是这样,你很好。

此外,本地还有各种options to run应用程序(命令行,前台,后台,泊坞窗,Windows Scheduler,Unix cron等)云。

应用程序为highly configurable,您可以配置各种详细信息,例如:

  • 工作簿加载选项
  • 带有愿望的图像选项。
  • SMTP配置
  • 其他应用程序级别的配置,例如何时发送愿望,迟来的愿望,日志记录等。

    免责声明:我是该应用程序的所有者