internal void SaveMailJob(MailSend mailJob)
        {
            IDataConnection connection = DatabaseAPI.GetConnection();
            try
            {
                connection.open();

                if (MailJobExistsById(mailJob.ID.ToString()))
                { // update
                    string query = string.Format("update mail_send set invoice_number='{0}', status={1}, message_type={2} where id={3}",
                        mailJob.Invoice.InvoiceNo, (int)mailJob.Status, (int)mailJob.MessageType, mailJob.ID);
                    connection.runNonQuery(query);
                }
                else
                { // insert
                    string query = string.Format("insert into mail_send (invoice_number, status, message_type) values ('{0}', {1}, {2}); SELECT LAST_INSERT_ID();",
                        mailJob.Invoice.InvoiceNo, (int)mailJob.Status, (int)mailJob.MessageType);
                    mailJob.ID = int.Parse(connection.runScalar(query).ToString());
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
            finally
            {
                if (connection != null && connection.State == ConnectionState.Open)
                    connection.close();
            }
        }
        internal void SaveMailJob(MailSend mailJob)
        {
            bool add = false;

            DBMailEntry mail = _dbcontext.MailEntry.Where(x => x.EntryId == mailJob.ID).FirstOrDefault();
            if (mail == default(DBMailEntry))
            {
                add = true;
                mail = new DBMailEntry();
                mail.DateCreated = DateTime.Now;
            }

            mail.Invoice = _dbcontext.Invoice.Where(x => x.InvoiceId == mailJob.Invoice.Id).FirstOrDefault();
            mail.MessageType = (int)mailJob.MessageType;
            mail.Status = (int)mailJob.Status;
            if(add)
                _dbcontext.MailEntry.Add(mail);
            _dbcontext.SaveChanges();
        }
        public static List<MailSend> GetMailSendsByInvoiceAndStatus(string invoiceID, Enums.MailStatus status)
        {
            List<MailSend> mailers = new List<MailSend>();

            try {

                using (IDataReader reader = API.EmailingAPI.GetMailJobByInvoiceAndStatus(invoiceID, (int)status)) {
                    while (reader.Read()) {
                        MailSend mailsend = new MailSend();
                        mailsend.ID = int.Parse(reader["id"].ToString());
                        mailsend.Invoice = new Invoice(reader["invoice_number"].ToString());
                        mailsend.MessageType = ((Enums.MessageType)int.Parse(reader["message_type"].ToString()));
                        mailsend.Status = ((Enums.MailStatus)int.Parse(reader["status"].ToString()));

                        mailers.Add(mailsend);
                    }
                }

            } catch (Exception exc) {
                // Trouble connecting to database
                Console.WriteLine("Problem connecting: {0}", exc.Message);
            }
            return mailers;
        }
 public static void SaveMailJob(MailSend mailJob)
 {
     Actions.SaveMailJob(mailJob);
 }
 void sendParcelSentEmailNotification(Invoice invoice)
 {
     IMailSender email = new MailSend(invoice, Enums.MessageType.PARCEL_SENT_NOTIFICATION);
     email.SaveJob();
 }
        private string GetTemplatedEmail(MailSend mail)
        {
            string templatePath = string.Format("{0}{1}.htm", GetTemplatesPath(), mail.Invoice.Provider.CarrierId);
            string template = "";

            try {
                template = File.ReadAllBytes(templatePath).ToString();
            }
            catch(Exception exc) {
                Console.WriteLine("Problem occured: ", exc.Message);
            }
            return template;
        }
        private void SendEmails(object sender, System.Timers.ElapsedEventArgs args)
        {
            if (Connection != null) {

                if (Connection.open()) {
                    List<int> ids = new List<int>();

                    // Get a list of all emails past the window time
                    DateTime cutoff = DateTime.Now.AddHours(-GetSendWindowHours());

                    string sql = string.Format("select id from mail_send where status={0} and date_created < '{1}';",
                        (int)BusinessLayer.Enums.MailStatus.WAITING, cutoff.ToString("yyyy-MM-dd HH:mm:ss"));
                    using (DbDataReader reader = Connection.getDataReader(sql)) {

                        while (reader.Read()) {
                            ids.Add(int.Parse(reader["id"].ToString()));
                        }
                    }

                    // Create the mailsends from the ids
                    foreach (int id in ids) {
                        MailSend mail = new MailSend(id);
                        SendEmail(mail);

                        // Update mailsend item as sent
                        mail.Status = BusinessLayer.Enums.MailStatus.SENT;
                        mail.save();
                    }

                } else {
                    Console.WriteLine("Cannot connect to database.");
                }

            }
        }
        private void SendEmail(MailSend mail)
        {
            string from = GetFromAddress();
            //string to = mail.Invoice.InvoiceCustomer.EmailAddress;
            string to = "*****@*****.**";

            MailMessage msg = new MailMessage(from, to);
            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Host = GetSMTP();
            msg.Subject = "this is a test email.";
            msg.Body = GetTemplatedEmail(mail);
            client.Send(msg);
        }