Example #1
0
 public Account(Account toCopy)
 {
     EmailAddress = toCopy.EmailAddress;
     LoginId = toCopy.LoginId;
     DisplayName = toCopy.DisplayName;
     SmtpHostname = toCopy.SmtpHostname;
     PortNumber = toCopy.PortNumber;
     SslIsEnabled = toCopy.SslIsEnabled;
 }
Example #2
0
        public static void Send(Account account, ExcelRow entry)
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
                MailMessage mailMsg = new MailMessage();
                MailAddress fromAddress;
                if (account.DisplayName == "")
                    fromAddress = new MailAddress(account.EmailAddress);
                else
                    fromAddress = new MailAddress(account.EmailAddress, account.DisplayName);

                mailMsg.To.Add(entry.EmailAddress);
                mailMsg.From = fromAddress;
                mailMsg.IsBodyHtml = true;

                // Subject and Body
                mailMsg.Subject = String.Format("Room {0}'s weekly energy report card", entry.RoomNumber);
                switch (entry.MessageType)
                {
                    case MESSAGE_TYPE_CONTROL:
                        mailMsg.Body = s_emailBodyControl.Generate(entry.RoomNumber, entry.YourEnergyUse, entry.OtherEnergyUse, entry.BestEnergyUse, entry.Rating);
                        break;
                    case MESSAGE_TYPE_GENERIC:
                        mailMsg.Body = s_emailBodyGeneric.Generate(entry.RoomNumber, entry.YourEnergyUse, entry.OtherEnergyUse, entry.BestEnergyUse, entry.Rating);
                        break;
                    case MESSAGE_TYPE_PERSONALIZED:
                        mailMsg.Body = s_emailBodyPersonalized.Generate(entry.RoomNumber, entry.YourEnergyUse, entry.OtherEnergyUse, entry.BestEnergyUse, entry.Rating);
                        break;
                }

                // Init SmtpClient and send on port 587 in my case. (Usual=port25)
                SmtpClient smtpClient = new SmtpClient(account.SmtpHostname, account.PortNumber);
                smtpClient.EnableSsl = account.SslIsEnabled;
                smtpClient.Credentials = new System.Net.NetworkCredential(account.LoginId, account.Password);

                smtpClient.Send(mailMsg);
            }
            // TODO: add exception handling for email sending
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
Example #3
0
        private void populateFields(Account accountData)
        {
            if (accountData != null)
            {
                textboxEmailAddress.Text = accountData.EmailAddress;
                textBoxUsername.Text = accountData.LoginId;
                textBoxDisplayName.Text = accountData.DisplayName;
                textboxPassword.Text = accountData.Password;
                textboxPortNumber.Text = accountData.PortNumber.ToString();
                textboxSmtpHostname.Text = accountData.SmtpHostname;
                checkboxEnableSSL.Checked = accountData.SslIsEnabled;

                if (accountData.Password == null || accountData.Password == "")
                    checkboxSavePassword.Checked = false;
                else
                    checkboxSavePassword.Checked = true;
            }
            else
            {
                textboxEmailAddress.Text = "";
                textBoxUsername.Text = "";
                textBoxDisplayName.Text = "";
                textboxPassword.Text = "";
                textboxPortNumber.Text = "587";
                textboxSmtpHostname.Text = "";
                checkboxEnableSSL.Checked = true;
                checkboxSavePassword.Checked = false;
            }
        }
Example #4
0
        private void sendAllEmails(Account account, int startingIndex = 0)
        {
            if (m_writeableEntries != null)
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                Dictionary<int, string> undeliverable = new Dictionary<int, string>();

                progressBarSending.Maximum = m_writeableEntries.Count;
                progressBarSending.Value = startingIndex;

                if (!m_willLoadSavedData)
                {
                    using (Stream dataStream = File.Open(@"bin\inprog_data.bin", FileMode.Create))
                    using (Stream accountStream = File.Open(@"bin\inprog_account.bin", FileMode.Create))
                    {
                        binaryFormatter.Serialize(dataStream, m_writeableEntries);
                        binaryFormatter.Serialize(accountStream, account);
                    }
                }

                for (int i = startingIndex; i < m_writeableEntries.Count; ++i)
                {
                    using (Stream stream = File.Open(@"bin\inprog_count.bin", FileMode.Create))
                    {
                        binaryFormatter.Serialize(stream, i);

                        try
                        {
                            Emailer.Send(account, m_writeableEntries[i]);
                        }
                        catch (Exception ex)
                        {
                            undeliverable.Add(i, m_writeableEntries[i].EmailAddress);
                        }

                    }

                    progressBarSending.Increment(1);
                    progressBarSending.Refresh();
                }

                if (undeliverable.Count == 0)
                {
                    MessageBox.Show("All emails sent without errors.", "Finished");
                }
                else
                {
                    string output = "Sending complete with errors. Emails to the following addresses were not sent:\n\n";
                    foreach (KeyValuePair<int, string> entry in undeliverable)
                    {
                        output += String.Format("\"{0}\": row {1}\n", entry.Value, entry.Key);
                    }
                    MessageBox.Show(output, "Finished (Errors)");
                }

                progressBarSending.Value = 0;
                closeWorksheet();
            }
        }
Example #5
0
        private void buttonSaveAccount_Click(object sender, EventArgs e)
        {
            Account newAccount = newAccountFromFields();
            int index = listboxSavedAccounts.SelectedIndex;

            if (areFieldsValidForSaving())
            {
                if (!checkboxSavePassword.Checked)
                    newAccount = new Account(newAccount);

                if (index == 0)
                    m_accounts.Add(newAccount);
                else
                {
                    m_accounts.RemoveAt(index);
                    m_accounts.Insert(index, newAccount);
                    listboxSavedAccounts.SelectedIndex = index;
                }

                saveAccounts();
            }
        }