///////////////////////////////////////////////////////////////////// // set screen ///////////////////////////////////////////////////////////////////// private void SetScreen ( TestParam Param ) { if (TypeComboBox.SelectedIndex != Param.Type) { TypeComboBox.SelectedIndex = Param.Type; } HostTextBox.Text = Param.Host; PortTextBox.Text = Param.Port.ToString(); UserNameTextBox.Text = Param.UserName; if (Param.Type != 0) { UserPasswordTextBox.Text = Param.UserPassword; UserPasswordTextBox.Enabled = true; RefreshTokenTextBox.Text = string.Empty; RefreshTokenTextBox.Enabled = false; BrowseButton.Enabled = false; } else { UserPasswordTextBox.Text = string.Empty; UserPasswordTextBox.Enabled = false; RefreshTokenTextBox.Text = Param.RefreshToken; RefreshTokenTextBox.Enabled = true; BrowseButton.Enabled = true; } TimeoutTextBox.Text = Param.Timeout.ToString(); FromNameTextBox.Text = Param.FromName; FromAddressTextBox.Text = Param.FromAddress; ToNameTextBox.Text = Param.ToName; ToAddressTextBox.Text = Param.ToAddress; return; }
///////////////////////////////////////////////////////////////////// // initialization ///////////////////////////////////////////////////////////////////// private void OnLoad(object sender, EventArgs e) { #if DEBUG // current directory string CurDir = Environment.CurrentDirectory; string WorkDir = CurDir.Replace("bin\\Debug", "Work"); if (WorkDir != CurDir && Directory.Exists(WorkDir)) { Environment.CurrentDirectory = WorkDir; } #endif Text = "Test Uzi Secure Smtp Client Ver 2.0 2019/06/20"; TypeComboBox.Items.Add("Secure (SSL/TLS) OAuth2"); TypeComboBox.Items.Add("Secure (SSL/TLS) Plain Text"); TypeComboBox.Items.Add("Unsecure Plain Text"); TestParamArray = TestParam.LoadTestParam(TestParamFileName); if (TestParamArray == null) { NextButton.Enabled = false; PreviousButton.Enabled = false; SetScreen(new TestParam()); } else { NextButton.Enabled = true; PreviousButton.Enabled = true; SetScreen(TestParamArray[0]); } return; }
///////////////////////////////////////////////////////////////////// // On test (Unused) ///////////////////////////////////////////////////////////////////// private void OnSendEmailAlternate(object sender, EventArgs e) { TestParam Param = ReadScreen(); //ExampleUnsecurePlainText(Param.Host, Param.UserName, Param.UserPassword, Param.FromName, Param.FromAddress, Param.ToName, Param.ToAddress, Subject, PlainTextView); //ExampleSecureHtml(Param.Host, Param.UserName, Param.UserPassword, Param.FromName, Param.FromAddress, Param.ToName, Param.ToAddress, Subject, HtmlView); ExampleGMail(Param.Host, Param.UserName, Param.RefreshToken, Param.FromName, Param.FromAddress, Param.ToName, Param.ToAddress, Subject, HtmlView); return; }
///////////////////////////////////////////////////////////////////// // Read user parameters ///////////////////////////////////////////////////////////////////// private TestParam ReadScreen() { TestParam Param = new TestParam(); Param.Type = TypeComboBox.SelectedIndex; Param.Host = HostTextBox.Text.Trim(); int.TryParse(PortTextBox.Text.Trim(), out Param.Port); Param.UserName = UserNameTextBox.Text.Trim(); Param.UserPassword = UserPasswordTextBox.Text.Trim(); Param.RefreshToken = RefreshTokenTextBox.Text.Trim(); int.TryParse(TimeoutTextBox.Text.Trim(), out Param.Timeout); Param.FromName = FromNameTextBox.Text.Trim(); Param.FromAddress = FromAddressTextBox.Text.Trim(); Param.ToName = ToNameTextBox.Text.Trim(); Param.ToAddress = ToAddressTextBox.Text.Trim(); if (TestParamArray == null) { TestParamArray = new TestParam[1]; TestParamArray[0] = Param; } else { int Index; for (Index = 0; Index < TestParamArray.Length && Param.CompareTo(TestParamArray[Index]) != 0; Index++) { ; } if (Index == 0) { return(Param); } List <TestParam> TestParamList = new List <TestParam>(TestParamArray); if (Index < TestParamArray.Length) { TestParamList.RemoveAt(Index); } TestParamList.Insert(0, Param); if (TestParamList.Count > 20) { TestParamList.RemoveRange(20, TestParamList.Count - 20); } TestParamArray = TestParamList.ToArray(); } TestParam.SaveTestParam(TestParamArray, TestParamFileName); NextButton.Enabled = true; PreviousButton.Enabled = true; ScrollIndex = 0; return(Param); }
///////////////////////////////////////////////////////////////////// // Send Mail prototype. // This method should be used as a prototype for your email // sending method. ///////////////////////////////////////////////////////////////////// private void OnSendEmail(object sender, EventArgs e) { try { // read screen parameters TestParam Param = ReadScreen(); // create one of three possible connection classes // the SecureSmtpClient is re-useable. If you send more than one email, // you can reuse the class. It is of real benefit for gmail. if (Connection != null && Param.CompareTo(LastParam) != 0) { Connection = null; } if (Connection == null) { switch (Param.Type) { case 0: Connection = new SecureSmtpClient(Param.Host, Param.UserName, new SecureSmtpOAuth2(Param.RefreshToken)); break; case 1: Connection = new SecureSmtpClient(ConnectMethod.Secure, Param.Host, Param.UserName, Param.UserPassword); break; case 2: Connection = new SecureSmtpClient(ConnectMethod.Unsecure, Param.Host, Param.UserName, Param.UserPassword); break; } Connection.PortNo = Param.Port; Connection.Timeout = Param.Timeout; LastParam = Param; } // create mail message object SecureSmtpMessage Message = new SecureSmtpMessage(); // Set subject Message.Subject = Subject; // Set mail from address and display name. Message.From = new MailAddress(Param.FromAddress, Param.FromName); // Add minimum one or more recipients. Message.To.Add(new MailAddress(Param.ToAddress, Param.ToName)); // create mixed multipart boundary SecureSmtpMultipart Mixed = new SecureSmtpMultipart(MultipartType.Mixed); Message.RootPart = Mixed; // create alternative boundary SecureSmtpMultipart Alternative = new SecureSmtpMultipart(MultipartType.Alternative); Mixed.AddPart(Alternative); // Add plain text mail body contents. SecureSmtpContent PlainTextContent = new SecureSmtpContent(ContentType.Plain, PlainTextView); Alternative.AddPart(PlainTextContent); // create related boundary SecureSmtpMultipart Related = new SecureSmtpMultipart(MultipartType.Related); Alternative.AddPart(Related); // add html mail body content SecureSmtpContent HtmlContent = new SecureSmtpContent(ContentType.Html, HtmlView); Related.AddPart(HtmlContent); // add inline image attachment. // NOTE image id is set to IMAGE001 this id must match the html image id in HtmlView text. SecureSmtpAttachment ImageAttachment = new SecureSmtpAttachment(AttachmentType.Inline, "EmailImage.png", "IMAGE001"); ImageAttachment.MediaType = "image/png"; Related.AddPart(ImageAttachment); // add file attachment to the email. // The recipient of the email will be able to save it as a file. SecureSmtpAttachment PdfAttachment = new SecureSmtpAttachment(AttachmentType.Attachment, "rfc2045.pdf"); Mixed.AddPart(PdfAttachment); // send mail Connection.SendMail(Message); MessageBox.Show("Email was successfully sent"); } // catch exceptions catch (Exception Exp) { MessageBox.Show(Exp.Message); } return; }