コード例 #1
0
ファイル: Account.cs プロジェクト: rbstarbuck/energy-emailer
 public Account(Account toCopy)
 {
     EmailAddress = toCopy.EmailAddress;
     LoginId = toCopy.LoginId;
     DisplayName = toCopy.DisplayName;
     SmtpHostname = toCopy.SmtpHostname;
     PortNumber = toCopy.PortNumber;
     SslIsEnabled = toCopy.SslIsEnabled;
 }
コード例 #2
0
ファイル: Emailer.cs プロジェクト: rbstarbuck/energy-emailer
        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;
            }
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: rbstarbuck/energy-emailer
        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;
            }
        }
コード例 #4
0
ファイル: MainForm.cs プロジェクト: rbstarbuck/energy-emailer
        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();
            }
        }
コード例 #5
0
ファイル: MainForm.cs プロジェクト: rbstarbuck/energy-emailer
        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();
            }
        }