Example #1
0
        private void AddValue(object sender, EventArgs e)
        {
            var btn = (Button)sender;

            switch (btn.Name)
            {
            case "CmdAddNumber":
            {
                if (!Regex.IsMatch(TxtNumber.Text, @"\d{2}-\d{2}-\d{3}"))
                {
                    return;                                                               //When the user hits enter and bypasses the button.
                }
                LbNumbers.Items.Add(TxtNumber.Text);
                JobNumbers.Add(TxtNumber.Text);
                CmdAddNumber.Enabled = false;
                return;
            }

            case "CmdAddFiles":
            {
                string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                if (!string.IsNullOrEmpty(Properties.Settings.Default.DefaultSearch))
                {
                    baseDir = Properties.Settings.Default.DefaultSearch;
                }

                OpenFileDialog dialog = new OpenFileDialog
                {
                    Title            = "Select files",
                    Multiselect      = true,
                    InitialDirectory = baseDir
                };

                var dr = dialog.ShowDialog();

                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                foreach (string file in dialog.FileNames)
                {
                    LbFiles.Items.Add(Path.GetFileName(file));
                    Files.Add(file);
                }

                return;
            }
            }
        }
Example #2
0
        void SendData(object s, EventArgs e)
        {
            if (Properties.Settings.Default.SendMethod)
            {
                //TCP
            }
            else
            {
                lblStatus.Text = "Creating E-Mail";
                //EMail

                try
                {
                    MailMessage message = new MailMessage();

                    string[] toPeople = Properties.Settings.Default.DestEmail.Split(',');
                    foreach (string dest in toPeople)
                    {
                        message.To.Add(dest);
                    }

                    message.From = new MailAddress(Properties.Settings.Default.EMail);

                    if (TxtNumber.Text == "12-34-567")
                    {
                        message.Subject = "Testing app config";
                        message.Body    = "Alert the person who sent this that the test worked and you received this message.";
                    }
                    else
                    {
                        if (JobNumbers.Count == 0)
                        {
                            if (Regex.IsMatch(TxtNumber.Text, @"\d{2}-\d{2}-\d{3}"))
                            {
                                JobNumbers.Add(TxtNumber.Text);
                            }
                            else
                            {
                                lblStatus.Text = "No job numbers detected.";
                                return;
                            }
                        }

                        if (Files.Count == 0)
                        {
                            var mr = MessageBox.Show("No files were added, continue sending data?", "Confirm", MessageBoxButtons.YesNo);
                            if (mr == DialogResult.No)
                            {
                                lblStatus.Text = "No files detected."; return;
                            }
                        }
                        else
                        {
                            foreach (string file in Files)
                            {
                                Attachment attachment = new Attachment(file);
                                message.Attachments.Add(attachment);
                            }
                        }

                        string msgBodyFormatted = msgBody.Replace("{SENDER}", Properties.Settings.Default.Name)
                                                  .Replace("{JOBNO}", String.Join("\r\n", JobNumbers))
                                                  .Replace("{PURPOSE}", CbPurpose.Text)
                                                  .Replace("{NOTES}", TxtNotes.Text);

                        message.Subject = "Field Data Submission";
                        message.Body    = msgBodyFormatted;
                    }

                    lblStatus.Text = "Preparing Sender";


                    SmtpClient client;
                    switch (Properties.Settings.Default.EMailServerMethod)
                    {
                    case 0:
                    {
                        client = new SmtpClient(EMailServerInfo.GMail.Host)
                        {
                            Port        = EMailServerInfo.GMail.Port,
                            EnableSsl   = EMailServerInfo.GMail.EnableSSL,
                            Credentials = new NetworkCredential(Properties.Settings.Default.EMail, Properties.Settings.Default.Password)
                        };
                        break;
                    }

                    case 1:
                    {
                        client = new SmtpClient(EMailServerInfo.GoDaddy.Host)
                        {
                            Port        = EMailServerInfo.GoDaddy.Port,
                            EnableSsl   = EMailServerInfo.GoDaddy.EnableSSL,
                            Credentials = new NetworkCredential(Properties.Settings.Default.EMail, Properties.Settings.Default.Password)
                        };
                        break;
                    }

                    case 2:
                    {
                        client = new SmtpClient(EMailServerInfo.Office365.Host)
                        {
                            Port        = EMailServerInfo.Office365.Port,
                            EnableSsl   = EMailServerInfo.Office365.EnableSSL,
                            Credentials = new NetworkCredential(Properties.Settings.Default.EMail, Properties.Settings.Default.Password)
                        };
                        break;
                    }

                    case 3:
                    default:
                    {
                        client = new SmtpClient(Properties.Settings.Default.EMailDomain)
                        {
                            Port        = Properties.Settings.Default.EMailPort,
                            EnableSsl   = Properties.Settings.Default.EMailSSL,
                            Credentials = new NetworkCredential(Properties.Settings.Default.EMail, Properties.Settings.Default.Password)
                        };
                        break;
                    }
                    }

                    lblStatus.Text = "Sending E-Mail";
                    client.Send(message);

                    lblStatus.Text = "Message sent.";


                    message.Dispose();
                    client.Dispose();
                } catch (Exception ex)
                {
                    lblStatus.Text = "Message failed.";
#if DEBUG
                    MessageBox.Show(ex.Message);
#endif
                }
                finally
                {
                }
            }
        }