private void FormWebMailMessageEdit_Load(object sender, EventArgs e)
        {
            _webMailMode = WebMailMode.View;
            if (_emailMessage == null)
            {
                _webMailMode = WebMailMode.Compose;
            }
            string error = "";

            if (_patCur == null)
            {
                error += "Cannot send Web Mail to an invalid patient. ";
            }
            else
            {
                _listPatsForPHI = Patients.GetPatientsForPhi(_patCur.PatNum);
                if (_listPatsForPHI.Count == 0)               //Every patient should have at least one guarantor.
                {
                    error += "Patient family not setup properly.  Make sure guarantor is valid. ";
                }
            }
            //Webmail notification email address.  One notification email per database (not clinic specific).
            _emailAddressSender = EmailAddresses.GetOne(PrefC.GetLong(PrefName.EmailNotifyAddressNum));
            if (_emailAddressSender == null ||
                _emailAddressSender.EmailAddressNum == 0 ||
                _emailAddressSender.EmailUsername == "")
            {
                //No valid "Notify" email setup for this practice yet.
                error += "Invalid Web Mail Notify email.  Configure a Web Mail Notify email address in E-mail Setup. ";
            }
            List <Userod> listUsers = Userods.GetUsersWithProviders();

            if (listUsers.Count < 1)
            {
                error += "Cannot send Web Mail until there is at least one User associated to a Provider. ";
            }
            if (error != "")
            {
                MsgBox.Show(this, error);
                DialogResult = DialogResult.Abort;
                return;
            }
            if (_emailMessage != null)
            {
                _patRegarding = Patients.GetLim(_emailMessage.PatNumSubj);
            }
            if (Security.CurUser != null)
            {
                _provUserCur = Providers.GetProv(Security.CurUser.ProvNum);
            }
            List <long> listProvNums = listUsers.Select(x => x.ProvNum).Distinct().ToList();

            _listProviders = Providers.GetProvsByProvNums(listProvNums);
            FillFields();
        }
 ///<summary>When viewing an existing message, the "Send" button text will be "Reply" and _formModeCur will be View.  Pressing the button will
 ///reload this form as a reply message.
 ///When composing a new message or replying to an existing message, the button text will be "Send" and _formModeCur will be
 ///either Compose or Reply.  Pressing the button will cause an attempt to send the secure and insecure message if applicable.</summary>
 private void butSend_Click(object sender, EventArgs e)
 {
     if (_webMailMode == WebMailMode.View)
     {
         _webMailMode = WebMailMode.Reply;
         FillFields();
         return;
     }
     if (!Security.IsAuthorized(Permissions.WebMailSend))
     {
         return;
     }
     VerifyInputs();
     if (!VerifyOutputs())
     {
         return;
     }
     if (!VerifyFromProvider())
     {
         return;
     }
     butSend.Enabled = false;
     //Insert the message. The patient will not see this as an actual email.
     //Rather, they must login to the patient portal (secured) and view the message that way.
     //This is how we get around sending the patient a secure message, which would be a hassle for all involved.
     _secureMessage                = new EmailMessage();
     _secureMessage.FromAddress    = textFrom.Text;
     _secureMessage.ToAddress      = textTo.Text;
     _secureMessage.PatNum         = _patCur.PatNum;
     _secureMessage.SentOrReceived = EmailSentOrReceived.WebMailSent;            //this is secure so mark as webmail sent
     _secureMessage.ProvNumWebMail = _provCur.ProvNum;
     _secureMessage.Subject        = textSubject.Text;
     _secureMessage.BodyText       = textBody.Text;
     _secureMessage.MsgDateTime    = DateTime.Now;
     _secureMessage.PatNumSubj     = GetPatNumSubj();
     if (_allowSendNotificationMessage)
     {
         _insecureMessage                = new EmailMessage();
         _insecureMessage.FromAddress    = _emailAddressSender.SenderAddress;
         _insecureMessage.ToAddress      = _patCur.Email;
         _insecureMessage.PatNum         = _patCur.PatNum;
         _insecureMessage.Subject        = SubjectInsecure;
         _insecureMessage.BodyText       = BodyTextInsecure;
         _insecureMessage.SentOrReceived = EmailSentOrReceived.Sent;               //this is not secure so just mark as regular sent
         //Send an insecure notification email to the patient.
         _insecureMessage.MsgDateTime = DateTime.Now;
         _insecureMessage.PatNumSubj  = GetPatNumSubj();
         try {
             EmailMessages.SendEmailUnsecure(_insecureMessage, _emailAddressSender);
             //Insert the notification email into the emailmessage table so we have a record that it was sent.
             EmailMessages.Insert(_insecureMessage);
         }
         catch (Exception ex) {
             MessageBox.Show(this, "An error occurred sending the message. Please try again later or contact support.");
             Logger.openlog.LogMB(this, System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message, Logger.Severity.ERROR);
             butSend.Enabled = true;
             return;
         }
     }
     _secureMessage.Attachments = _listAttachments;
     EmailMessages.Insert(_secureMessage);
     SecurityLogs.MakeLogEntry(Permissions.WebMailSend, 0, Lan.g(this, "Web Mail sent"));
     MsgBox.Show(this, "Message Sent");
     DialogResult = DialogResult.OK;
 }