Beispiel #1
0
        /////////////////////////////////////////////////////////////////////
        // 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);
        }
Beispiel #2
0
        /////////////////////////////////////////////////////////////////////
        // 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;
        }