Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("waiting" + DateTime.Now.ToLongTimeString());
            int secs = int.Parse(ConfigurationSettings.AppSettings["Wait"].ToString());

            System.Threading.Thread.Sleep(secs * 1000);
            Console.WriteLine("starting" + DateTime.Now.ToLongTimeString());
            MailClass mailClass = new MailClass();
            MailVo    mailVo    = new MailVo();
            string    filepath  = ConfigurationSettings.AppSettings["logPath"].ToString();


            mailVo.Host     = ConfigurationSettings.AppSettings["host"].ToString();
            mailVo.Port     = int.Parse(ConfigurationSettings.AppSettings["port"].ToString());
            mailVo.UserName = ConfigurationSettings.AppSettings["UserName"].ToString();
            mailVo.Password = ConfigurationSettings.AppSettings["Password"].ToString();
            mailVo.From     = ConfigurationSettings.AppSettings["From"].ToString();

            mailVo.Subject = ConfigurationSettings.AppSettings["Subject"].ToString();
            mailVo.Message = ConfigurationSettings.AppSettings["Message"].ToString();
            mailVo.To      = ConfigurationSettings.AppSettings["To"].ToString().Split(';');
            mailVo.Cc      = ConfigurationSettings.AppSettings["Cc"].ToString().Split(';');
            mailVo.Bcc     = ConfigurationSettings.AppSettings["Bcc"].ToString().Split(';');
            mailVo.IsBulk  = int.Parse(ConfigurationSettings.AppSettings["IsBulk"].ToString());
            if (mailVo.IsBulk == 0)
            {
                if (mailClass.SendMail(mailVo))
                {
                    Console.WriteLine("Alert Send Successfully");
                }
                else
                {
                    Console.WriteLine("Alert Not Send");
                }
            }
            else
            {
                if (mailClass.SendBulkMail(mailVo))
                {
                    Console.WriteLine("Alert Send Successfully");
                }
                else
                {
                    Console.WriteLine("Alert Not Send");
                }
            }
            Console.ReadLine();
        }
        public bool SendBulkMail(MailVo mailVo)
        {
            bool   result = false;
            string filepath = ConfigurationSettings.AppSettings["logPath"].ToString();
            string filepath_url = "";
            string attachment_url = ConfigurationSettings.AppSettings["Attachment"].ToString();
            string year = "", day = "", month = "";

            if (attachment_url.Contains("#Date#"))
            {
                year = DateTime.Now.Year.ToString();
                if (DateTime.Now.Month < 10)
                {
                    month = "0" + DateTime.Now.Month.ToString();
                }
                else
                {
                    month = DateTime.Now.Month.ToString();
                }
                if (DateTime.Now.Day < 10)
                {
                    day = "0" + DateTime.Now.Day.ToString();
                }
                else
                {
                    day = DateTime.Now.Day.ToString();
                }
                filepath_url = attachment_url.Replace("#Date#", year + month + day);
            }
            else
            {
                filepath_url = attachment_url;
            }


            try
            {
                SmtpClient        smtpclient        = new SmtpClient(mailVo.Host, mailVo.Port);
                NetworkCredential networkCredential = new NetworkCredential(mailVo.UserName, mailVo.Password);
                smtpclient.Credentials = networkCredential;
                MailMessage mailMessage = new MailMessage();
                MailAddress mailFrom    = new MailAddress(mailVo.From);
                Attachment  attachment  = new Attachment(filepath_url);
                mailMessage.From = mailFrom;
                mailMessage.Attachments.Add(attachment);
                foreach (string strCc in mailVo.Cc)
                {
                    if (strCc != "")
                    {
                        mailMessage.CC.Add(strCc);
                    }
                }
                foreach (string strBcc in mailVo.Bcc)
                {
                    if (strBcc != "")
                    {
                        mailMessage.Bcc.Add(strBcc);
                    }
                }
                foreach (string strTo in mailVo.To)
                {
                    if (strTo != "")
                    {
                        mailMessage.To.Add(strTo);
                        mailMessage.Subject = mailVo.Subject;
                        mailMessage.Body    = mailVo.Message;
                        smtpclient.Send(mailMessage);
                        mailMessage.To.Clear();
                    }
                }


                result = true;
            }
            catch (Exception ex)
            {
                String exMsg = ex.Message.ToString();
                WriteToFile(filepath, ex.ToString());
            }

            return(result);
        }