Пример #1
0
        public static void authenticate()
        {
            planner.updateStatus("Authenticating Google Calendar Account Info");
            string calendarURL = PlannerSettings.Default.CalendarURL;

            if (calendarURL.Equals(""))
            {
                MessageBox.Show("No Calendar URL specified.", "Invalid Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            postUri = new Uri(calendarURL);
            BackgroundWorker bw = new BackgroundWorker();

            bw.DoWork += delegate(object s, DoWorkEventArgs args){
                lock (threadLock) {
                    service = new CalendarService("A+StudentPlanner");
                    if (PlannerSettings.Default.StoredGoogleAccount == true)
                    {
                        byte[]   salt           = Encoding.Unicode.GetBytes("A+ Student Planner Developed By David Riggleman");
                        string[] encryptedBytes = PlannerSettings.Default.Password.Split('-');
                        byte[]   encodedBytes   = new byte[encryptedBytes.Length];
                        for (int i = 0; i < encryptedBytes.Length; i++)
                        {
                            encodedBytes[i] = Byte.Parse(encryptedBytes[i]);
                        }
                        byte[] decodedBytes = ProtectedData.Unprotect(encodedBytes, salt, DataProtectionScope.CurrentUser);
                        service.setUserCredentials(PlannerSettings.Default.Username, Encoding.Unicode.GetString(decodedBytes));

                        try {
                            EventQuery myQuery    = new EventQuery(postUri.ToString());
                            EventFeed  resultFeed = (EventFeed)service.Query(myQuery);
                            authenticated = true;
                        }
                        catch {
                            Util.displayError("Error authenticating Google Calendar Account. Check settings.", "Authentication Error");
                            authenticated = false;
                        }
                    }
                    else
                    {
                        AuthenticationForm form = new AuthenticationForm(PlannerSettings.Default.Username);
                        form.changeText("Enter the credentials for your google account. \n(No information will be stored).", "Enter Google Account Credentials");
                        authenticated = false;
                        bool exit = false;
                        while (exit == false)
                        {
                            form.ShowDialog();
                            if (form.cancelled == true)
                            {
                                exit = true;
                            }
                            else
                            {
                                try {
                                    service.setUserCredentials(form.username, form.password);
                                    EventQuery myQuery    = new EventQuery(postUri.ToString());
                                    EventFeed  resultFeed = (EventFeed)service.Query(myQuery);
                                    authenticated = true;
                                    exit          = true;
                                }
                                catch {
                                    DialogResult retry = MessageBox.Show("Incorrect username or password. Try Again?", "Authentication Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);
                                    if (retry != DialogResult.Retry)
                                    {
                                        exit = true;
                                    }
                                }
                            }
                        }
                        form.eraseCredentials();
                    }
                }
            };

            bw.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args) {
                if (authenticated == true)
                {
                    planner.updateStatus("Status: Google Authentication Successful");
                    planner.Refresh();
                }
                else
                {
                    planner.updateStatus("Status: Google Authentication Failed");
                    planner.Refresh();
                }
            };

            //start thread
            bw.RunWorkerAsync();
        }
Пример #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            //extract user entered information
            string MyFrom    = txtEmailFrom.Text;
            string MyTo      = txtEmailTo.Text;
            string MySubject = txtEmailSubject.Text;
            string MyMessage = txtEmailMessage.Text;

            //check if to open with default email client
            if (PlannerSettings.Default.UseDefaultClient == true)
            {
                //open in email client with entered fields already populated
                openMailClient(MyTo, MySubject, MyMessage);
            }
            else
            {
                string MySmtpMailServerName = PlannerSettings.Default.SmtpServer;
                //ex: "smtp.cedarville.edu" or "smtp.google.com"

                int MySmtpMailServerPort = PlannerSettings.Default.SmtpPort;
                //25 is the default for SMTP (587 for Google)

                SmtpClient MySmtpClient = new SmtpClient(MySmtpMailServerName, MySmtpMailServerPort);

                if (PlannerSettings.Default.RequireAuthentication == true)
                {
                    //get authentication details from user
                    AuthenticationForm credentials = new AuthenticationForm(PlannerSettings.Default.EmailAddress);
                    credentials.ShowDialog(this);

                    //configure credentials
                    MySmtpClient.EnableSsl             = true;
                    MySmtpClient.Timeout               = 3000;
                    MySmtpClient.UseDefaultCredentials = false;
                    MySmtpClient.Credentials           = new NetworkCredential(credentials.username, credentials.password);
                    credentials.eraseCredentials();
                }

                //indicate message is in process of sending
                parent.updateStatus("Status: Message Sending...");

                //send message using new thread so it won't freeze main thread if there is an error
                String           errorMessage = "";
                BackgroundWorker bw           = new BackgroundWorker();
                bw.DoWork += delegate(object s, DoWorkEventArgs args){
                    try {
                        MySmtpClient.Send(MyFrom, MyTo, MySubject, MyMessage);
                    }
                    catch (Exception ex) {
                        errorMessage = ex.Message;
                    }
                };

                bw.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args) {
                    if (errorMessage.Equals("") == false)
                    {
                        //display any errors with sending the message
                        Util.displayError(errorMessage, "Error");
                        parent.updateStatus("Status: Message Not Sent");
                        parent.Refresh();
                    }
                    else
                    {
                        //update message status if message sent successfully
                        parent.updateStatus("Status: Message Successfully Sent");
                        parent.Refresh();
                    }
                };

                //start sending message
                bw.RunWorkerAsync();
            }
        }