Ejemplo n.º 1
0
        private void RemoveAttachedFileButton_Click(object sender, EventArgs e)
        {
            for (int x = attachmentListBox.SelectedIndices.Count - 1; x >= 0; x--)
            {
                int element = attachmentListBox.SelectedIndices[x];

                // decrement total file size
                FilesTotalLength -= new FileInfo(AttachmentFilePaths[element]).Length;

                AttachmentFilePaths.RemoveAt(element);
                AttachmentFileNames.RemoveAt(element);
            }

            UpdateTotalFilesLengthInfo();

            UpdateBindings();

            toolStripStatusLabel.Text = string.Empty;
        }
Ejemplo n.º 2
0
        private void AttachFileButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in openFileDialog.FileNames)
                {
                    // check, if list contains current file
                    if (AttachmentFilePaths.Contains(file))
                    {
                        MessageBox.Show($"File '{file}' already added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        continue;
                    }

                    long fileLength = new FileInfo(file).Length;

                    if (FilesTotalLength + fileLength > (long.Parse(fileSizeLimitTextBox.Text) * 1048576))
                    {
                        toolStripStatusLabel.Text = string.Empty;
                        MessageBox.Show($"Attachment(s) total file size exceeds the limit allowed", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        continue;
                    }
                    else
                    {
                        // increment total file size
                        FilesTotalLength += fileLength;
                    }

                    AttachmentFilePaths.Add(file);
                    AttachmentFileNames.Add(Path.GetFileName(file));
                }

                UpdateTotalFilesLengthInfo();

                UpdateBindings();

                toolStripStatusLabel.Text = "File(s) added";
            }
        }
Ejemplo n.º 3
0
        private async Task SendEmailAsync()
        {
            try
            {
                var client = new SmtpClient()
                {
                    Host        = serverHostnameTextBox.Text,
                    Port        = int.Parse(serverPortTextBox.Text),
                    Timeout     = SMTP_CLIENT_TIMEOUT,
                    Credentials = new NetworkCredential(usernameTextBox.Text, passwordTextBox.Text),
                    EnableSsl   = useSslCheckBox.Checked
                };

                using var mail = new MailMessage();

                // validate 'From' email address
                ValidateEmail(fromTextBox.Text);
                mail.From = new MailAddress(fromTextBox.Text);

                // add 'To' recipients
                if (!string.IsNullOrEmpty(toTextBox.Text))
                {
                    string[] toRecipients = toTextBox.Text.Split(';');

                    foreach (string recipient in toRecipients)
                    {
                        // validate email address
                        ValidateEmail(recipient);
                        mail.To.Add(new MailAddress(recipient));
                    }
                }

                // add 'CC' recipients
                if (!string.IsNullOrEmpty(ccTextBox.Text))
                {
                    string[] ccRecipients = ccTextBox.Text.Split(';');

                    foreach (string recipient in ccRecipients)
                    {
                        // validate email address
                        ValidateEmail(recipient);
                        mail.CC.Add(new MailAddress(recipient));
                    }
                }

                mail.Subject      = subjectTextBox.Text;
                mail.BodyEncoding = Encoding.UTF8;
                mail.IsBodyHtml   = htmlBodyCheckBox.Checked;

                if (mail.IsBodyHtml)
                {
                    // replace '\n' chars with "<br/>"
                    var sb = new StringBuilder(messageRichTextBox.Text);
                    sb.Replace("\n", "<br/>");

                    string timestamp = string.Empty;

                    // check, if timestamp is required
                    if (timestampCheckBox.Checked)
                    {
                        timestamp = $"<p><b>{GetUtcTimestamp()}</b></p>";
                    }

                    string htmlBody = "<html>" +
                                      $"<head>" +
                                      $"<title></title>" +
                                      $"</head>" +
                                      $"<body>" +
                                      $"<p>{sb.ToString()}</p>" +
                                      timestamp +
                                      $"</body>" +
                                      $"</html>";

                    mail.Body = htmlBody;
                }
                else
                {
                    string timestamp = string.Empty;

                    // check, if timestamp is required
                    if (timestampCheckBox.Checked)
                    {
                        timestamp = $"\n\n{GetUtcTimestamp()}";
                    }

                    mail.Body = messageRichTextBox.Text + timestamp;
                }

                // add attachements, if required
                if (AttachmentFilePaths.Any())
                {
                    foreach (string file in AttachmentFilePaths)
                    {
                        var attachment = new Attachment(file);

                        if (safeExtensionCheckBox.Checked)
                        {
                            // remove file extension and then append the 'safe' one
                            attachment.Name  = Path.GetFileNameWithoutExtension(file);
                            attachment.Name += SAFE_FILE_EXTENSION;
                        }

                        mail.Attachments.Add(attachment);
                    }
                }

                using (var sendingForm = new SendingForm())
                {
                    sendingForm.sendingLabel.Text = $"Sending email to {mail.To}";
                    toolStripStatusLabel.Text     = $"Sending email to {mail.To}";
                    sendingForm.Show();

                    await Task.Run(() =>
                    {
                        try
                        {
                            _emailSent = false;

                            client.Send(mail);

                            _emailSent = true;
                        }
                        catch (Exception ex)
                        {
                            toolStripStatusLabel.Text = "Exception occured";

#if DEBUG
                            MessageBox.Show($"{ex.Message}\n{ex.InnerException}\n{ex.StackTrace}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
                            MessageBox.Show($"{ex.Message}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif

                            return;
                        }
                    });
                }

                if (_emailSent)
                {
                    toolStripStatusLabel.Text = $"Email successfully sent to {mail.To}";
                    MessageBox.Show($"Email successfully sent to {mail.To}", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                toolStripStatusLabel.Text = "Exception occured";

#if DEBUG
                MessageBox.Show($"{ex.Message}\n{ex.InnerException}\n{ex.StackTrace}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
                MessageBox.Show($"{ex.Message}", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
            }
        }