示例#1
0
        private void Execute_Click(object sender, EventArgs e)
        {
            string messageid = MessageID.Text.ToString(); // Fetching Message ID

            Status.Text = "Retrieving users";
            AdminAPI adminresource = new AdminAPI();                    // Retrieving users
            int      size          = AdminAPI.usersize;                 // How many users are there?

            guser[] userlist = new guser[size];                         // Creating array to accomodate the users
            userlist    = AdminAPI.fillusers(AdminAPI.users, userlist); // Adding information to the array
            Status.Text = "Users retrieved, retrieving email information";
            int i = 0;

            foreach (var item in userlist)
            {
                GmailAPI mailresource = new GmailAPI(item, messageid); // Fetching immutable ID for each user
            }

            Status.Text = "Email information retrieved. Performing deletion";

            if (userlist != null)
            {
                foreach (var listitem in userlist)
                {
                    if (listitem.immid != null)
                    {
                        GmailAPI delete = new GmailAPI();
                        delete.deleteemail(listitem); // Deleting email for each user
                    }
                    else
                    {
                    }
                    i++;
                }
            }

            Status.Text = "Email deleted. You can now close this application";
        }
        // Create argumented constructor
        public GmailAPI(guser user, string rfc822msgid)
        {
            // Retrieve certificate for service account
            var certificate = new X509Certificate2("service_account.p12", "notasecret", X509KeyStorageFlags.Exportable);

            // create credential using service account
            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceaccount)
            {
                User   = user.email, // user to impersonate
                Scopes = gmscopes
            }.FromCertificate(certificate));

            // Create impersonated service
            var service = new GmailService(new BaseClientService.Initializer()
            {
                ApplicationName       = ApplicationName,
                HttpClientInitializer = credential
            });

            // Create request for immutable ID based on RFC 822 ID
            var request = service.Users.Messages.List(user.email);

            request.Q = ("rfc822msgid:" + rfc822msgid);

            // Execute requet
            var response = request.Execute();

            // Verify if there are usable results
            if (response.Messages != null)
            {
                user.immid = response.Messages[0].Id; // Populate the user's immutable ID field with the information
            }
            else
            {
                user.immid = null;  // else, keep the immutable ID as null
            }
        }
        public void deleteemail(guser user)
        {
            // Retrieve certificate for service account
            var certificate = new X509Certificate2("service_account.p12", "notasecret", X509KeyStorageFlags.Exportable);

            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceaccount)
            {
                User   = user.email,
                Scopes = gmscopes
            }.FromCertificate(certificate));

            // Create impersonated service
            var service = new GmailService(new BaseClientService.Initializer()
            {
                ApplicationName       = ApplicationName,
                HttpClientInitializer = credential
            });

            // Create request for message deletion task
            var request = service.Users.Messages.Delete(user.email, user.immid);

            // Execute request
            var response = request.Execute();
        }