Beispiel #1
0
 public void Send()
 {
     MailSender ms = new MailSender();
     ms.Send(new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), "Test Send 1", "this is a test", null);
 }
Beispiel #2
0
        /// <summary>
        /// Send the current config file as a zipped file by email
        /// </summary>
        /// <returns>DialogResult for Retry, Cancel or OK</returns>
        private DialogResult SendBackup()
        {
            // any password?
            string password = tbPassword.Text.Trim();

            // get the config/authenticator data. Remove any machine/user encryption
            StringBuilder dataxml = new StringBuilder();
            XmlWriterSettings xmlsettings = new XmlWriterSettings();
            xmlsettings.Encoding = System.Text.Encoding.Unicode;
            using (XmlWriter xw = XmlWriter.Create(dataxml, xmlsettings))
            {
                WinAuthConfig config = (WinAuthConfig)CurrentConfig.Clone();
                if (config.Authenticator != null)
                {
                    config.Authenticator.PasswordType = Authenticator.PasswordTypes.Explicit;
                    config.Authenticator.Password = password;
                }
                config.WriteXmlString(xw);
            }

            // create a temp file containing the zipped data file
            string zipfile = Path.Combine(Path.GetTempPath(), Path.GetFileName(CurrentAuthenticatorFile) + ".zip");
            try {
                // send wait cursor
                Cursor.Current = Cursors.WaitCursor;

                using (FileStream zipfs = File.Create(zipfile))
                {
                    using (ZipOutputStream zos = new ZipOutputStream(zipfs))
                    {
                        // add the authenticator file
                        ZipEntry entry = new ZipEntry(Path.GetFileName(CurrentAuthenticatorFile));
                        entry.DateTime = DateTime.Now;
                        zos.PutNextEntry(entry);
                        using (MemoryStream fs = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(dataxml.ToString())))
                        {
                            byte[] buffer = new byte[4096];
                            int count;
                            while ((count = fs.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                zos.Write(buffer, 0, count);
                            }
                        }

                        // close zip file
                        zos.Finish();
                        zos.Close();
                    }
                }

                // now send the file
                MailSender ms = new MailSender();
                if (this.ckSmtpServer.Checked == true)
                {
                    ms.SmtpServer = this.cbSmtpServer.Text;
                    ms.SmtpPort = Convert.ToInt32(cbSmtpPorts.Text);
                    ms.SmtpSSL = ckSmtpSSL.Checked;

                    if (this.tbSmtpUsername.Text.Length != 0)
                    {
                        ms.SmtpUsername = this.tbSmtpUsername.Text;
                        ms.SmtpPasword = this.tbSmtpPassword.Text;
                    }
                }
                ms.Send(new MailAddress(WinAuth.WINAUTHBACKUP_EMAIL), new MailAddress(this.tbEmail.Text), this.tbSubject.Text, string.Empty, new string[] { zipfile });

                // reset cursor
                Cursor.Current = Cursors.Default;

                // confirm dialog
                MessageBox.Show(this, "Your email has been sent.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                // reset cursor
                Cursor.Current = Cursors.Default;
                return MessageBox.Show(this, "An error occured sending your backup: " + ex.Message, this.Text, MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);
            }
            finally
            {
                try {
                    File.Delete(zipfile);
                } catch (Exception ) {}
            }

            return DialogResult.OK;
        }