private void Log(MailLog MailLog, List<UDT.MailLog> MandrillSendLogs)
        {
            UDT.MailLog MandrillSendLog = new UDT.MailLog();

            MandrillSendLog.EmailCategory = MailLog.MailLogBase.EmailCategory;
            MandrillSendLog.GUID = MailLog.MailLogBase.GUID;
            MandrillSendLog.IsCC = MailLog.IsCC;
            MandrillSendLog.SchoolYear = MailLog.MailLogBase.SchoolYear;
            MandrillSendLog.Semester = MailLog.MailLogBase.Semester;
            MandrillSendLog.SenderEmail = MailLog.MailLogBase.SenderEmail;
            MandrillSendLog.SenderName = MailLog.MailLogBase.SenderName;
            MandrillSendLog.TimeStamp = MailLog.TimeStamp;
            MandrillSendLog.UserAccount = MailLog.MailLogBase.UserAccount;
            MandrillSendLog.MailContent = SecurityElement.Escape(MailLog.MailContent);
            MandrillSendLog.MailSubject = SecurityElement.Escape(MailLog.MailSubject);
            MandrillSendLog.RecipientEmailAddress = MailLog.RecipientEmailAddress;
            MandrillSendLog.Status = MailLog.Status;
            MandrillSendLog.Result = MailLog.Result;

            MandrillSendLog.Extension = string.Format("<Extension>" + (string.IsNullOrEmpty(MailLog.StudentID) ? "" :
                                                                                                ("<Student ID=\"" + MailLog.StudentID + "\"></Student>")) +
                                                                                                "<Course ID=\"" + MailLog.CourseID + "\"></Course>" +
                                                                                                "<Section ID=\"" + string.Join(",", MailLog.SectionIDs) + "\"></Section>" +
                                                                                            "</Extension>");

            MandrillSendLogs.Add(MandrillSendLog);
        }
        private void SendSingleMail(MailLog MailLog, IEnumerable<string> email_list, MandrillApi mandrill, List<UDT.MailLog> MandrillSendLogs)
        {
            if (email_list.Count() == 0)
                return;

            try
            {
                EmailMessage message = new EmailMessage();
                message.auto_text = true;
                message.from_email = this.from_email;
                message.from_name = this.from_name;

                List<EmailAddress> EmailAddresss = new List<EmailAddress>();
                foreach (string mail_to in email_list)
                {
                    EmailAddress mt = new EmailAddress();

                    mt.email = mail_to;
                    mt.name = string.Empty;

                    EmailAddresss.Add(mt);
                }
                message.to = EmailAddresss;

                message.track_clicks = true;
                message.track_opens = true;
                message.html = MailLog.MailContent;
                message.important = true;
                message.merge = false;
                message.preserve_recipients = true;
                message.subject = MailLog.MailSubject;

                MailLog.TimeStamp = DateTime.Now;

                List<EmailResult> results = mandrill.SendMessageSync(message);

                //  Log Email Result
                foreach (EmailResult result in results)
                {
                    MailLog.Result = "<Result>" +
                                                       "<NoneException>" +
                                                           "<Status>" + result.Status.ToString() + "</Status>" +
                                                           "<ResultID>" + result.Id + "</ResultID>" +
                                                           "<Email>" + result.Email + "</Email>" +
                                                           "<RejectReason>" + result.RejectReason + "</RejectReason>" +
                                                        "</NoneException>" +
                                                    "</Result>";
                    MailLog.RecipientEmailAddress = result.Email;
                    MailLog.Status = result.Status.ToString();
                    this.Log(MailLog, MandrillSendLogs);
                }
            }
            //  Log Email Error
            catch (Exception ex)
            {
                MandrillException error = (MandrillException)ex;

                MailLog.Result = "<Result>" +
                                                    "<Exception>" +
                                                        "<Status>" + error.Error.status + "</Status>" +
                                                        "<Code>" + error.Error.code + "</Code>" +
                                                        "<Name>" + error.Error.name + "</Name>" +
                                                        "<Message>" + error.Message + "</Message>" +
                                                        "<Source>" + error.Source + "</Source>" +
                                                        "<Email></Email>" +
                                                    "</Exception>" +
                                                "</Result>";
                MailLog.RecipientEmailAddress = string.Empty;
                MailLog.Status = error.Error.status;
                this.Log(MailLog, MandrillSendLogs);
            }
        }
        private void Emailing(MailLog MailLog, List<string> Emails, string StudentName, string CourseName, string AttendNo, string AttendTime, MandrillApi mandrill, List<UDT.MailLog> MandrillSendLogs, string AttendPeriod)
        {
            DateTime time_stamp = DateTime.Now;
            string email_subject = MailLog.MailLogBase.MailSubject;
            string email_body = MailLog.MailLogBase.MailContent;

            try
            {
                //  學年度、學期、開課、學生姓名、缺課次數、缺課時間
                email_subject = email_subject.Replace("[[學年度]]", MailLog.MailLogBase.SchoolYear.ToString()).Replace("[[學期]]", DataItems.SemesterItem.GetSemesterByCode(MailLog.MailLogBase.Semester + "").Name).Replace("[[開課]]", CourseName).Replace("[[學生姓名]]", StudentName).Replace("[[缺課次數]]", AttendNo).Replace("[[缺課時間]]", AttendTime).Replace("[[上課總堂數]]", this.SectionIDs.Count.ToString()).Replace("[[缺課日期]]", AttendPeriod);
                email_body = email_body.Replace("[[學年度]]", MailLog.MailLogBase.SchoolYear.ToString()).Replace("[[學期]]", DataItems.SemesterItem.GetSemesterByCode(MailLog.MailLogBase.Semester + "").Name).Replace("[[開課]]", CourseName).Replace("[[學生姓名]]", StudentName).Replace("[[缺課次數]]", AttendNo).Replace("[[缺課時間]]", AttendTime).Replace("[[上課總堂數]]", this.SectionIDs.Count.ToString()).Replace("[[缺課日期]]", AttendPeriod);
                if (MailLog.IsCC)
                {
                    email_subject += "【副本】";
                }
                MailLog.MailSubject = email_subject;
                MailLog.MailContent = email_body;
                this.SendSingleMail(MailLog, Emails, mandrill, MandrillSendLogs);
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
            }
            finally
            {
                //Access.InsertValues(MandrillSendLogs);
                //MandrillSendLogs.SaveAll();
            }
        }
        /// <summary>
        /// 批次寄送缺課通知
        /// </summary>
        /// <param name="StudentIDs"></param>
        /// <param name="Course"></param>
        /// <param name="dicSections"></param>
        /// <param name="dicAttendNos"></param>
        /// <param name="Subject"></param>
        /// <param name="Content"></param>
        /// <param name="template_name"></param>
        /// <param name="mandrill"></param>
        private void SendEmails(List<string> StudentIDs, dynamic Course, Dictionary<string, Dictionary<int, string>> dicSections, Dictionary<string, string> dicAttendNos, MailLogBase MailLogBase, MandrillApi mandrill, List<UDT.MailLog> MandrillSendLogs)
        {
            if (StudentIDs.Count == 0)
                throw new Exception("請先勾選學生。");

            List<string> ErrorEmails = this.GetErrorEmails(StudentIDs);
            if (ErrorEmails.Count > 0)
            {
                string error_email_alert_message = "下列學生及其電子郵件格式有誤,請先修正:\n\n";
                ErrorEmails.ForEach((x) => error_email_alert_message += (x + "\n"));

                throw new Exception(error_email_alert_message);
            }

            MailLog mail_log = new MailLog(false, MailLogBase);
            foreach (string StudentID in StudentIDs)
            {
                if (!this.dicStudentEmails.ContainsKey(StudentID))
                    continue;

                List<dynamic> emails = dicStudentEmails[StudentID];
                List<string> Emails = new List<string>();
                string StudentName = string.Empty;
                foreach (dynamic o in emails)
                {
                    string student_number = o.學號 + "";
                    StudentName = o.學生姓名 + "";
                    string e0 = o.登入帳號 + "";
                    string e1 = o.電子郵件一 + "";
                    string e2 = o.電子郵件二 + "";
                    string e3 = o.電子郵件三 + "";
                    string e4 = o.電子郵件四 + "";
                    string e5 = o.電子郵件五 + "";

                    if (!string.IsNullOrEmpty(e0))
                        Emails.Add(e0);
                    if (!string.IsNullOrEmpty(e1))
                        Emails.Add(e1);
                    if (!string.IsNullOrEmpty(e2))
                        Emails.Add(e2);
                    if (!string.IsNullOrEmpty(e3))
                        Emails.Add(e3);
                    if (!string.IsNullOrEmpty(e4))
                        Emails.Add(e4);
                    if (!string.IsNullOrEmpty(e5))
                        Emails.Add(e5);
                }
                mail_log = new MailLog(false, MailLogBase);
                mail_log.StudentID = StudentID;
                mail_log.CourseID = Course.CourseID + "";
                List<int> ColumnIndexs = dicSections[StudentID].Keys.ToList();
                ColumnIndexs.ForEach((x) => mail_log.AddSectionID(this.SectionIDs[x-7].ToString()));

                //  2014/06/07 09:00~12:40(補)
                List<string> absence = dicSections[StudentID].Values.ToList();
                string absence_dates = string.Join("、", absence.Select(x => x.Split(new char[]{' '}).ElementAt(0)));
                string absence_times = string.Empty;

                //.ToString("yyyy/MM/dd hh:mm:ss");
                if (absence.Count == 1)
                    absence_times = absence.ElementAt(0).Split(new char[] { ' ' }).ElementAt(1);
                else
                    absence_times = string.Join("、", absence);

                this.Emailing(mail_log, Emails.Distinct().ToList(), StudentName, Course.CourseName + "", dicAttendNos[StudentID], absence_times, mandrill, MandrillSendLogs, absence_dates);

                if (this.validated_cc.Count() > 0)
                {
                    mail_log = new MailLog(true, MailLogBase);
                    mail_log.StudentID = StudentID;
                    mail_log.CourseID = Course.CourseID + "";
                    ColumnIndexs.ForEach((x) => mail_log.AddSectionID(this.SectionIDs[x - 7].ToString()));
                    this.Emailing(mail_log, this.validated_cc.Distinct().ToList(), StudentName, Course.CourseName + "", dicAttendNos[StudentID], absence_times, mandrill, MandrillSendLogs, absence_dates);
                }
            }
        }