From dad3d1184ca883eb7a1e79927b196c3c87a52595 Mon Sep 17 00:00:00 2001 From: Benny Shtemer <75434503+benny779@users.noreply.github.com> Date: Sun, 15 Dec 2024 09:01:14 +0200 Subject: [PATCH 1/7] feat: Add Bcc option --- SimpleSmtpClient/Form1.Designer.cs | 86 +++++++++++++++++++----------- SimpleSmtpClient/Form1.cs | 4 ++ 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/SimpleSmtpClient/Form1.Designer.cs b/SimpleSmtpClient/Form1.Designer.cs index 27718ea..493f785 100644 --- a/SimpleSmtpClient/Form1.Designer.cs +++ b/SimpleSmtpClient/Form1.Designer.cs @@ -30,6 +30,8 @@ private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm)); this.serverGroup = new System.Windows.Forms.GroupBox(); + this.lblSSLVersion = new System.Windows.Forms.Label(); + this.cmbSSLVersion = new System.Windows.Forms.ComboBox(); this.guiUseSsl = new System.Windows.Forms.CheckBox(); this.guiPassword = new System.Windows.Forms.TextBox(); this.lblPassword = new System.Windows.Forms.Label(); @@ -47,11 +49,11 @@ private void InitializeComponent() this.lblSubject = new System.Windows.Forms.Label(); this.guiEmailTo = new System.Windows.Forms.TextBox(); this.lblEmailTo = new System.Windows.Forms.Label(); + this.guiEmailBcc = new System.Windows.Forms.TextBox(); + this.lblEmailBcc = new System.Windows.Forms.Label(); this.guiEmailFrom = new System.Windows.Forms.TextBox(); this.lblEmailFrom = new System.Windows.Forms.Label(); this.guiSendMail = new System.Windows.Forms.Button(); - this.cmbSSLVersion = new System.Windows.Forms.ComboBox(); - this.lblSSLVersion = new System.Windows.Forms.Label(); this.serverGroup.SuspendLayout(); this.emailGroup.SuspendLayout(); this.SuspendLayout(); @@ -77,6 +79,29 @@ private void InitializeComponent() this.serverGroup.TabStop = false; this.serverGroup.Text = "SMTP Configuration"; // + // lblSSLVersion + // + this.lblSSLVersion.AutoSize = true; + this.lblSSLVersion.Location = new System.Drawing.Point(298, 98); + this.lblSSLVersion.Name = "lblSSLVersion"; + this.lblSSLVersion.Size = new System.Drawing.Size(96, 13); + this.lblSSLVersion.TabIndex = 10; + this.lblSSLVersion.Text = "SSL / TLS Version"; + // + // cmbSSLVersion + // + this.cmbSSLVersion.FormattingEnabled = true; + this.cmbSSLVersion.Items.AddRange(new object[] { + "Auto", + "SSL3", + "TLS 1.0", + "TLS 1.1", + "TLS 1.2"}); + this.cmbSSLVersion.Location = new System.Drawing.Point(400, 95); + this.cmbSSLVersion.Name = "cmbSSLVersion"; + this.cmbSSLVersion.Size = new System.Drawing.Size(121, 21); + this.cmbSSLVersion.TabIndex = 7; + // // guiUseSsl // this.guiUseSsl.AutoSize = true; @@ -172,18 +197,20 @@ private void InitializeComponent() this.emailGroup.Controls.Add(this.lblSubject); this.emailGroup.Controls.Add(this.guiEmailTo); this.emailGroup.Controls.Add(this.lblEmailTo); + this.emailGroup.Controls.Add(this.guiEmailBcc); + this.emailGroup.Controls.Add(this.lblEmailBcc); this.emailGroup.Controls.Add(this.guiEmailFrom); this.emailGroup.Controls.Add(this.lblEmailFrom); this.emailGroup.Location = new System.Drawing.Point(12, 170); this.emailGroup.Name = "emailGroup"; - this.emailGroup.Size = new System.Drawing.Size(539, 242); + this.emailGroup.Size = new System.Drawing.Size(539, 280); this.emailGroup.TabIndex = 1; this.emailGroup.TabStop = false; this.emailGroup.Text = "Email Message"; // // guiEmailBody // - this.guiEmailBody.Location = new System.Drawing.Point(76, 142); + this.guiEmailBody.Location = new System.Drawing.Point(76, 180); this.guiEmailBody.Multiline = true; this.guiEmailBody.Name = "guiEmailBody"; this.guiEmailBody.Size = new System.Drawing.Size(446, 77); @@ -192,7 +219,7 @@ private void InitializeComponent() // lblBody // this.lblBody.AutoSize = true; - this.lblBody.Location = new System.Drawing.Point(22, 143); + this.lblBody.Location = new System.Drawing.Point(22, 183); this.lblBody.Name = "lblBody"; this.lblBody.Size = new System.Drawing.Size(31, 13); this.lblBody.TabIndex = 7; @@ -200,7 +227,7 @@ private void InitializeComponent() // // guiEmailSubject // - this.guiEmailSubject.Location = new System.Drawing.Point(76, 104); + this.guiEmailSubject.Location = new System.Drawing.Point(76, 142); this.guiEmailSubject.Name = "guiEmailSubject"; this.guiEmailSubject.Size = new System.Drawing.Size(446, 20); this.guiEmailSubject.TabIndex = 10; @@ -208,7 +235,7 @@ private void InitializeComponent() // lblSubject // this.lblSubject.AutoSize = true; - this.lblSubject.Location = new System.Drawing.Point(22, 107); + this.lblSubject.Location = new System.Drawing.Point(22, 145); this.lblSubject.Name = "lblSubject"; this.lblSubject.Size = new System.Drawing.Size(43, 13); this.lblSubject.TabIndex = 5; @@ -230,6 +257,22 @@ private void InitializeComponent() this.lblEmailTo.TabIndex = 3; this.lblEmailTo.Text = "To"; // + // guiEmailBcc + // + this.guiEmailBcc.Location = new System.Drawing.Point(75, 104); + this.guiEmailBcc.Name = "guiEmailBcc"; + this.guiEmailBcc.Size = new System.Drawing.Size(446, 20); + this.guiEmailBcc.TabIndex = 10; + // + // lblEmailBcc + // + this.lblEmailBcc.AutoSize = true; + this.lblEmailBcc.Location = new System.Drawing.Point(21, 107); + this.lblEmailBcc.Name = "lblEmailBcc"; + this.lblEmailBcc.Size = new System.Drawing.Size(26, 13); + this.lblEmailBcc.TabIndex = 12; + this.lblEmailBcc.Text = "Bcc"; + // // guiEmailFrom // this.guiEmailFrom.Location = new System.Drawing.Point(76, 28); @@ -248,7 +291,7 @@ private void InitializeComponent() // // guiSendMail // - this.guiSendMail.Location = new System.Drawing.Point(446, 427); + this.guiSendMail.Location = new System.Drawing.Point(446, 465); this.guiSendMail.Name = "guiSendMail"; this.guiSendMail.Size = new System.Drawing.Size(104, 31); this.guiSendMail.TabIndex = 12; @@ -256,34 +299,11 @@ private void InitializeComponent() this.guiSendMail.UseVisualStyleBackColor = true; this.guiSendMail.Click += new System.EventHandler(this.guiSendMail_Click); // - // cmbSSLVersion - // - this.cmbSSLVersion.FormattingEnabled = true; - this.cmbSSLVersion.Items.AddRange(new object[] { - "Auto", - "SSL3", - "TLS 1.0", - "TLS 1.1", - "TLS 1.2"}); - this.cmbSSLVersion.Location = new System.Drawing.Point(400, 95); - this.cmbSSLVersion.Name = "cmbSSLVersion"; - this.cmbSSLVersion.Size = new System.Drawing.Size(121, 21); - this.cmbSSLVersion.TabIndex = 7; - // - // lblSSLVersion - // - this.lblSSLVersion.AutoSize = true; - this.lblSSLVersion.Location = new System.Drawing.Point(298, 98); - this.lblSSLVersion.Name = "lblSSLVersion"; - this.lblSSLVersion.Size = new System.Drawing.Size(96, 13); - this.lblSSLVersion.TabIndex = 10; - this.lblSSLVersion.Text = "SSL / TLS Version"; - // // mainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(565, 470); + this.ClientSize = new System.Drawing.Size(565, 508); this.Controls.Add(this.guiSendMail); this.Controls.Add(this.emailGroup); this.Controls.Add(this.serverGroup); @@ -319,6 +339,8 @@ private void InitializeComponent() private System.Windows.Forms.Label lblSubject; private System.Windows.Forms.TextBox guiEmailTo; private System.Windows.Forms.Label lblEmailTo; + private System.Windows.Forms.TextBox guiEmailBcc; + private System.Windows.Forms.Label lblEmailBcc; private System.Windows.Forms.TextBox guiEmailFrom; private System.Windows.Forms.Label lblEmailFrom; private System.Windows.Forms.Button guiSendMail; diff --git a/SimpleSmtpClient/Form1.cs b/SimpleSmtpClient/Form1.cs index f038623..31c0c6b 100644 --- a/SimpleSmtpClient/Form1.cs +++ b/SimpleSmtpClient/Form1.cs @@ -84,6 +84,10 @@ private MailMessage CreateMailMessage() MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(guiEmailFrom.Text); mailMessage.To.Add(guiEmailTo.Text); + if (!string.IsNullOrEmpty(guiEmailBcc.Text)) + { + mailMessage.Bcc.Add(guiEmailBcc.Text); + } mailMessage.Body = guiEmailBody.Text; mailMessage.Subject = guiEmailSubject.Text; return mailMessage; From b71ec75a304ca0dd01bb34e632c8e78b6aff61e3 Mon Sep 17 00:00:00 2001 From: Benny Shtemer <75434503+benny779@users.noreply.github.com> Date: Sun, 15 Dec 2024 09:07:51 +0200 Subject: [PATCH 2/7] fix: Wrong BCC tab index --- SimpleSmtpClient/Form1.Designer.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SimpleSmtpClient/Form1.Designer.cs b/SimpleSmtpClient/Form1.Designer.cs index 493f785..f065a84 100644 --- a/SimpleSmtpClient/Form1.Designer.cs +++ b/SimpleSmtpClient/Form1.Designer.cs @@ -214,7 +214,7 @@ private void InitializeComponent() this.guiEmailBody.Multiline = true; this.guiEmailBody.Name = "guiEmailBody"; this.guiEmailBody.Size = new System.Drawing.Size(446, 77); - this.guiEmailBody.TabIndex = 11; + this.guiEmailBody.TabIndex = 12; // // lblBody // @@ -230,7 +230,7 @@ private void InitializeComponent() this.guiEmailSubject.Location = new System.Drawing.Point(76, 142); this.guiEmailSubject.Name = "guiEmailSubject"; this.guiEmailSubject.Size = new System.Drawing.Size(446, 20); - this.guiEmailSubject.TabIndex = 10; + this.guiEmailSubject.TabIndex = 11; // // lblSubject // @@ -294,7 +294,7 @@ private void InitializeComponent() this.guiSendMail.Location = new System.Drawing.Point(446, 465); this.guiSendMail.Name = "guiSendMail"; this.guiSendMail.Size = new System.Drawing.Size(104, 31); - this.guiSendMail.TabIndex = 12; + this.guiSendMail.TabIndex = 13; this.guiSendMail.Text = "Send Mail"; this.guiSendMail.UseVisualStyleBackColor = true; this.guiSendMail.Click += new System.EventHandler(this.guiSendMail_Click); From ba0c3036dc6878cfe7b6fa586a4f0292946163fb Mon Sep 17 00:00:00 2001 From: Benny Shtemer <75434503+benny779@users.noreply.github.com> Date: Sun, 15 Dec 2024 09:12:36 +0200 Subject: [PATCH 3/7] feat: Toggle SSL Version Dropdown Based on SSL Checkbox --- SimpleSmtpClient/Form1.Designer.cs | 2 ++ SimpleSmtpClient/Form1.cs | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/SimpleSmtpClient/Form1.Designer.cs b/SimpleSmtpClient/Form1.Designer.cs index f065a84..8be055a 100644 --- a/SimpleSmtpClient/Form1.Designer.cs +++ b/SimpleSmtpClient/Form1.Designer.cs @@ -90,6 +90,7 @@ private void InitializeComponent() // // cmbSSLVersion // + this.cmbSSLVersion.Enabled = false; this.cmbSSLVersion.FormattingEnabled = true; this.cmbSSLVersion.Items.AddRange(new object[] { "Auto", @@ -111,6 +112,7 @@ private void InitializeComponent() this.guiUseSsl.TabIndex = 6; this.guiUseSsl.Text = "Use SSL"; this.guiUseSsl.UseVisualStyleBackColor = true; + this.guiUseSsl.CheckedChanged += new System.EventHandler(this.guiUseSsl_CheckedChanged); // // guiPassword // diff --git a/SimpleSmtpClient/Form1.cs b/SimpleSmtpClient/Form1.cs index 31c0c6b..ca86ad9 100644 --- a/SimpleSmtpClient/Form1.cs +++ b/SimpleSmtpClient/Form1.cs @@ -29,6 +29,11 @@ private void guiUseCredentials_CheckedChanged(object sender, EventArgs e) } } + private void guiUseSsl_CheckedChanged(object sender, EventArgs e) + { + cmbSSLVersion.Enabled = guiUseSsl.Checked; + } + private void guiSendMail_Click(object sender, EventArgs e) { try From 6d7ea10890857804dd3401004097feb21bc845f0 Mon Sep 17 00:00:00 2001 From: Benny Shtemer <75434503+benny779@users.noreply.github.com> Date: Sun, 15 Dec 2024 09:21:48 +0200 Subject: [PATCH 4/7] feat: Change port text box to numeric --- SimpleSmtpClient/Form1.Designer.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/SimpleSmtpClient/Form1.Designer.cs b/SimpleSmtpClient/Form1.Designer.cs index 8be055a..00a1e2e 100644 --- a/SimpleSmtpClient/Form1.Designer.cs +++ b/SimpleSmtpClient/Form1.Designer.cs @@ -38,7 +38,7 @@ private void InitializeComponent() this.guiUser = new System.Windows.Forms.TextBox(); this.lblUserName = new System.Windows.Forms.Label(); this.guiUseCredentials = new System.Windows.Forms.CheckBox(); - this.guiPort = new System.Windows.Forms.TextBox(); + this.guiPort = new System.Windows.Forms.NumericUpDown(); this.lblPort = new System.Windows.Forms.Label(); this.guiServerName = new System.Windows.Forms.TextBox(); this.lblServerName = new System.Windows.Forms.Label(); @@ -55,6 +55,7 @@ private void InitializeComponent() this.lblEmailFrom = new System.Windows.Forms.Label(); this.guiSendMail = new System.Windows.Forms.Button(); this.serverGroup.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.guiPort)).BeginInit(); this.emailGroup.SuspendLayout(); this.SuspendLayout(); // @@ -162,9 +163,24 @@ private void InitializeComponent() // guiPort // this.guiPort.Location = new System.Drawing.Point(449, 28); + this.guiPort.Maximum = new decimal(new int[] { + 65535, + 0, + 0, + 0}); + this.guiPort.Minimum = new decimal(new int[] { + 1, + 0, + 0, + 0}); this.guiPort.Name = "guiPort"; this.guiPort.Size = new System.Drawing.Size(73, 20); this.guiPort.TabIndex = 2; + this.guiPort.Value = new decimal(new int[] { + 25, + 0, + 0, + 0}); // // lblPort // @@ -316,6 +332,7 @@ private void InitializeComponent() this.Text = "Simple SMTP Client"; this.serverGroup.ResumeLayout(false); this.serverGroup.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.guiPort)).EndInit(); this.emailGroup.ResumeLayout(false); this.emailGroup.PerformLayout(); this.ResumeLayout(false); @@ -330,7 +347,7 @@ private void InitializeComponent() private System.Windows.Forms.TextBox guiUser; private System.Windows.Forms.Label lblUserName; private System.Windows.Forms.CheckBox guiUseCredentials; - private System.Windows.Forms.TextBox guiPort; + private System.Windows.Forms.NumericUpDown guiPort; private System.Windows.Forms.Label lblPort; private System.Windows.Forms.TextBox guiServerName; private System.Windows.Forms.Label lblServerName; From 454c4ea23198d1228abc78704c2d88f480dcb1c1 Mon Sep 17 00:00:00 2001 From: Benny Shtemer <75434503+benny779@users.noreply.github.com> Date: Sun, 15 Dec 2024 09:26:09 +0200 Subject: [PATCH 5/7] feat: Add html body support --- SimpleSmtpClient/Form1.Designer.cs | 17 +++++++++++++++-- SimpleSmtpClient/Form1.cs | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/SimpleSmtpClient/Form1.Designer.cs b/SimpleSmtpClient/Form1.Designer.cs index 00a1e2e..b34d4cf 100644 --- a/SimpleSmtpClient/Form1.Designer.cs +++ b/SimpleSmtpClient/Form1.Designer.cs @@ -54,6 +54,7 @@ private void InitializeComponent() this.guiEmailFrom = new System.Windows.Forms.TextBox(); this.lblEmailFrom = new System.Windows.Forms.Label(); this.guiSendMail = new System.Windows.Forms.Button(); + this.guiIsBodyHtml = new System.Windows.Forms.CheckBox(); this.serverGroup.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.guiPort)).BeginInit(); this.emailGroup.SuspendLayout(); @@ -209,6 +210,7 @@ private void InitializeComponent() // // emailGroup // + this.emailGroup.Controls.Add(this.guiIsBodyHtml); this.emailGroup.Controls.Add(this.guiEmailBody); this.emailGroup.Controls.Add(this.lblBody); this.emailGroup.Controls.Add(this.guiEmailSubject); @@ -221,7 +223,7 @@ private void InitializeComponent() this.emailGroup.Controls.Add(this.lblEmailFrom); this.emailGroup.Location = new System.Drawing.Point(12, 170); this.emailGroup.Name = "emailGroup"; - this.emailGroup.Size = new System.Drawing.Size(539, 280); + this.emailGroup.Size = new System.Drawing.Size(539, 289); this.emailGroup.TabIndex = 1; this.emailGroup.TabStop = false; this.emailGroup.Text = "Email Message"; @@ -309,7 +311,7 @@ private void InitializeComponent() // // guiSendMail // - this.guiSendMail.Location = new System.Drawing.Point(446, 465); + this.guiSendMail.Location = new System.Drawing.Point(446, 469); this.guiSendMail.Name = "guiSendMail"; this.guiSendMail.Size = new System.Drawing.Size(104, 31); this.guiSendMail.TabIndex = 13; @@ -317,6 +319,16 @@ private void InitializeComponent() this.guiSendMail.UseVisualStyleBackColor = true; this.guiSendMail.Click += new System.EventHandler(this.guiSendMail_Click); // + // guiIsBodyHtml + // + this.guiIsBodyHtml.AutoSize = true; + this.guiIsBodyHtml.Location = new System.Drawing.Point(76, 265); + this.guiIsBodyHtml.Name = "guiIsBodyHtml"; + this.guiIsBodyHtml.Size = new System.Drawing.Size(67, 17); + this.guiIsBodyHtml.TabIndex = 13; + this.guiIsBodyHtml.Text = "Is HTML"; + this.guiIsBodyHtml.UseVisualStyleBackColor = true; + // // mainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -366,6 +378,7 @@ private void InitializeComponent() private System.Windows.Forms.CheckBox guiUseSsl; private System.Windows.Forms.Label lblSSLVersion; private System.Windows.Forms.ComboBox cmbSSLVersion; + private System.Windows.Forms.CheckBox guiIsBodyHtml; } } diff --git a/SimpleSmtpClient/Form1.cs b/SimpleSmtpClient/Form1.cs index ca86ad9..d0b637b 100644 --- a/SimpleSmtpClient/Form1.cs +++ b/SimpleSmtpClient/Form1.cs @@ -94,6 +94,7 @@ private MailMessage CreateMailMessage() mailMessage.Bcc.Add(guiEmailBcc.Text); } mailMessage.Body = guiEmailBody.Text; + mailMessage.IsBodyHtml = guiIsBodyHtml.Checked; mailMessage.Subject = guiEmailSubject.Text; return mailMessage; } From 3bdb5d784f10d5ff96b90b3e04a4cf6344c0dccb Mon Sep 17 00:00:00 2001 From: Benny Shtemer <75434503+benny779@users.noreply.github.com> Date: Sun, 15 Dec 2024 09:43:35 +0200 Subject: [PATCH 6/7] feat: Add input validation before mail sending --- SimpleSmtpClient/Form1.cs | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/SimpleSmtpClient/Form1.cs b/SimpleSmtpClient/Form1.cs index d0b637b..8806a25 100644 --- a/SimpleSmtpClient/Form1.cs +++ b/SimpleSmtpClient/Form1.cs @@ -38,6 +38,9 @@ private void guiSendMail_Click(object sender, EventArgs e) { try { + ValidateSmtpConfiguration(); + ValidateMailMessage(); + SmtpClient client = new SmtpClient(); client.Host = guiServerName.Text; client.Port = Convert.ToInt32(guiPort.Text); @@ -84,6 +87,39 @@ private void guiSendMail_Click(object sender, EventArgs e) } } + private void ValidateSmtpConfiguration() + { + if (string.IsNullOrEmpty(guiServerName.Text)) + { + throw new Exception("Invalid SMTP server."); + } + + if (guiUseCredentials.Checked && + (string.IsNullOrEmpty(guiUser.Text) || string.IsNullOrEmpty(guiPassword.Text))) + { + throw new Exception("Invalid User or Password."); + } + } + + private void ValidateMailMessage() + { + if (!IsValidMailAddress(guiEmailFrom.Text)) + { + throw new Exception("Invalid From address."); + } + + if (!IsValidMailAddressList(guiEmailTo.Text)) + { + throw new Exception("Invalid To address."); + } + + if (!string.IsNullOrEmpty(guiEmailBcc.Text) && + !IsValidMailAddressList(guiEmailBcc.Text)) + { + throw new Exception("Invalid Bcc address."); + } + } + private MailMessage CreateMailMessage() { MailMessage mailMessage = new MailMessage(); @@ -98,5 +134,23 @@ private MailMessage CreateMailMessage() mailMessage.Subject = guiEmailSubject.Text; return mailMessage; } + + private static bool IsValidMailAddressList(string str) + { + return str.Split(',').All(IsValidMailAddress); + } + + private static bool IsValidMailAddress(string str) + { + try + { + _ = new MailAddress(str); + return true; + } + catch + { + return false; + } + } } } From 9ee0e1c32a9ae4fd6a24b0ad1c577cad22b8b185 Mon Sep 17 00:00:00 2001 From: Benny Shtemer <75434503+benny779@users.noreply.github.com> Date: Sun, 15 Dec 2024 09:52:03 +0200 Subject: [PATCH 7/7] style: Improve code style --- SimpleSmtpClient/Form1.Designer.cs | 6 +-- SimpleSmtpClient/Form1.cs | 70 ++++++++++++++++-------------- SimpleSmtpClient/Program.cs | 4 +- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/SimpleSmtpClient/Form1.Designer.cs b/SimpleSmtpClient/Form1.Designer.cs index b34d4cf..3ff4a30 100644 --- a/SimpleSmtpClient/Form1.Designer.cs +++ b/SimpleSmtpClient/Form1.Designer.cs @@ -1,6 +1,6 @@ namespace SimpleSmtpClient { - partial class mainForm + partial class MainForm { /// /// Required designer variable. @@ -28,7 +28,7 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm)); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); this.serverGroup = new System.Windows.Forms.GroupBox(); this.lblSSLVersion = new System.Windows.Forms.Label(); this.cmbSSLVersion = new System.Windows.Forms.ComboBox(); @@ -340,7 +340,7 @@ private void InitializeComponent() this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; - this.Name = "mainForm"; + this.Name = "MainForm"; this.Text = "Simple SMTP Client"; this.serverGroup.ResumeLayout(false); this.serverGroup.PerformLayout(); diff --git a/SimpleSmtpClient/Form1.cs b/SimpleSmtpClient/Form1.cs index 8806a25..e2c220e 100644 --- a/SimpleSmtpClient/Form1.cs +++ b/SimpleSmtpClient/Form1.cs @@ -1,19 +1,14 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; using System.Linq; -using System.Text; -using System.Windows.Forms; -using System.Net.Mail; using System.Net; +using System.Net.Mail; +using System.Windows.Forms; namespace SimpleSmtpClient { - public partial class mainForm : Form + public partial class MainForm : Form { - public mainForm() + public MainForm() { InitializeComponent(); } @@ -22,6 +17,7 @@ private void guiUseCredentials_CheckedChanged(object sender, EventArgs e) { guiUser.ReadOnly = true; guiPassword.ReadOnly = true; + if (guiUseCredentials.Checked) { guiUser.ReadOnly = false; @@ -44,40 +40,22 @@ private void guiSendMail_Click(object sender, EventArgs e) SmtpClient client = new SmtpClient(); client.Host = guiServerName.Text; client.Port = Convert.ToInt32(guiPort.Text); + if (guiUseCredentials.Checked) { client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(guiUser.Text, guiPassword.Text); } + if (guiUseSsl.Checked) { client.EnableSsl = true; - - int sslVer = cmbSSLVersion.SelectedIndex; - if (sslVer == 0 || sslVer == -1) - { - System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault; - } - else if (sslVer == 1) - { - System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; - } - else if (sslVer == 2) - { - System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; - } - else if (sslVer == 3) - { - System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; - } - else if (sslVer == 4) - { - System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; - } - //tls 1.3 not supported by .net framework 4.8 as of now. + ConfigSslOrTls(); } + MailMessage message = CreateMailMessage(); client.Send(message); + MessageBox.Show("Email Sent."); } @@ -86,7 +64,7 @@ private void guiSendMail_Click(object sender, EventArgs e) MessageBox.Show(ex.Message); } } - + private void ValidateSmtpConfiguration() { if (string.IsNullOrEmpty(guiServerName.Text)) @@ -120,6 +98,32 @@ private void ValidateMailMessage() } } + private void ConfigSslOrTls() + { + int sslVer = cmbSSLVersion.SelectedIndex; + + switch (sslVer) + { + case 0: + case -1: + ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault; + break; + case 1: + ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; + break; + case 2: + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; + break; + case 3: + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; + break; + case 4: + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; + break; + } + //tls 1.3 not supported by .net framework 4.8 as of now. + } + private MailMessage CreateMailMessage() { MailMessage mailMessage = new MailMessage(); diff --git a/SimpleSmtpClient/Program.cs b/SimpleSmtpClient/Program.cs index 03bb402..71f0e28 100644 --- a/SimpleSmtpClient/Program.cs +++ b/SimpleSmtpClient/Program.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Windows.Forms; namespace SimpleSmtpClient @@ -15,7 +13,7 @@ static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new mainForm()); + Application.Run(new MainForm()); } } }