예제 #1
0
        private static GoogleMailSettingsService CreateGoogleMailService()
        {
            // Get your service account email from Google Developer's Console
            // Read more: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
            const string SERVICE_ACCT_EMAIL = "<YOUR SERVICE KEY>@developer.gserviceaccount.com";
            //Generate your .p12 key in the Google Developer Console and associate it with your project.
            var certificate = new X509Certificate2("Key.p12", "notasecret", X509KeyStorageFlags.Exportable);

            var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(SERVICE_ACCT_EMAIL)
            {
                User   = "******", // A user with administrator access.
                Scopes = new[] { "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" }
            }.FromCertificate(certificate);

            var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);

            if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
            {
                throw new InvalidOperationException("Access token failed.");
            }

            var requestFactory = new GDataRequestFactory(null);

            requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);

            // Replace the name of your domain and the Google Developer project you created...
            GoogleMailSettingsService service = new GoogleMailSettingsService("vuwall.com", "signatures");

            service.RequestFactory = requestFactory;

            return(service);
        }
예제 #2
0
 private static void SendToGoogle(User user, string signature, GoogleMailSettingsService service)
 {
     try
     {
         Console.Write("Updating user: "******"...");
         service.UpdateSignature(user.AccountName, signature);
         Console.WriteLine("Success.");
     }
     catch (GDataRequestException gdre)
     {
         Console.WriteLine("Could not update user: "******" - Reason: " + gdre.ResponseString);
     }
 }
예제 #3
0
        private static void RunSample(GoogleMailSettingsService service)
        {
            try
            {
                // Create a new Label for the user testUserName
                service.CreateLabel(testUserName, "Test-Label");

                // Create a filter for emails from [email protected]
                // for the user testUserName and applies the new label "Test-Label"
                service.CreateFilter(testUserName, "test@" + domain, "", "", "", "", "", "Test-Label", "true", "");

                // Create a new Send As for the user testUserName
                service.CreateSendAs(testUserName, "Test email", testUserName + "@" + domain, "", "");

                // Updates the forwarding rule to forward emails to
                // [email protected] for the user testUserName amd keeps the email.
                service.UpdateForwarding(testUserName, "true", "test@" + domain, "KEEP");

                // Deactivate POP for the user testUserName
                service.UpdatePop(testUserName, "false", null, null);

                // Activate IMAP for the user testUserName
                service.UpdateImap(testUserName, "true");

                // Activate vacation autoresponse for the user testUserName
                service.UpdateVacation(testUserName, "true", "vacation", "vacation text...", "true");

                // Update the signature for the user testUserName
                service.UpdateSignature(testUserName, "Signature text...");

                // Update the language settings to French (fr) for the user testUserName
                service.UpdateLanguage(testUserName, "fr");

                // Update general settings for the user testUserName
                service.UpdateGeneralSettings(testUserName, "50", "false", "false", "false", "false");
            }
            catch (AppsException a)
            {
                Console.WriteLine("A Google Apps error occurred.");
                Console.WriteLine();
                Console.WriteLine("Error code: {0}", a.ErrorCode);
                Console.WriteLine("Invalid input: {0}", a.InvalidInput);
                Console.WriteLine("Reason: {0}", a.Reason);
            }
        }
        /// <summary>
        /// This console application demonstrates all the Google Apps
        /// Mail Settings API calls.
        /// </summary>
        /// <param name="args">Command-line arguments: args[0] is
        /// the domain, args[1] is the admin email address, args[2]
        /// is the admin password, and args[3] is the username to be modified
        /// by all actions.
        ///
        /// Example: GoogleMailSettingsDemo example.com [email protected] my_password user_name</param>
        public static void Main(string[] args)
        {
            if (args.Length != 4)
            {
                Console.WriteLine("Syntax: AppsDemo <domain> <admin_email> <admin_password> <test_username>");
            }
            else
            {
                domain        = args[0];
                adminEmail    = args[1];
                adminPassword = args[2];
                testUserName  = args[3];

                GoogleMailSettingsService service = new GoogleMailSettingsService(domain, "apps-demo");
                service.setUserCredentials(adminEmail, adminPassword);

                RunSample(service);
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            bool   show_help         = false;
            string domain            = "";
            string pathRazorTemplate = "";
            string pathSecretFile    = "client_secret.json";
            string user = "";

            var p = new OptionSet()
            {
                { "d|domain=", "the domain where you'll change the signatures.",
                  v => domain = v },
                { "t|template=",
                  "the file path of the razor template that will used.",
                  v => pathRazorTemplate = v },
                { "s|secret=", "the file with the secret from Google console.",
                  v => pathSecretFile = v },
                { "u|user="******"an specific user to apply the signature.",
                  v => user = v },
                { "a|all", "apply the signature to every user form the domain.",
                  v => { if (v != null)
                         {
                             user = "";
                         }
                  } },
                { "h|help", "show this message and exit",
                  v => show_help = v != null },
            };

            List <string> extra;

            try
            {
                extra = p.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("Error: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try --help for more information.");
                return;
            }

            if (show_help)
            {
                ShowHelp(p);
                return;
            }


            UserCredential credential = GetCredentials(pathSecretFile);

            var service = new DirectoryService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            IList <User> users = new List <User>();

            if (!string.IsNullOrWhiteSpace(user))
            {
                users.Add(service.Users.Get(user).Execute());
            }
            else
            {
                UsersResource.ListRequest request = service.Users.List();
                request.Domain     = domain;
                request.Projection = UsersResource.ListRequest.ProjectionEnum.Full;
                users = request.Execute().UsersValue;
            }

            OAuth2Parameters parameter = new OAuth2Parameters()
            {
                AccessToken = credential.Token.AccessToken
            };

            var requestFactory = new GOAuth2RequestFactory("apps", ApplicationName, parameter);
            var serviceGmail   = new GoogleMailSettingsService("revolute.academy", ApplicationName);

            serviceGmail.RequestFactory = requestFactory;

            Console.WriteLine("Users:");
            if (users != null)
            {
                foreach (var userItem in users)
                {
                    if (userItem.IsMailboxSetup.HasValue && userItem.IsMailboxSetup.Value)
                    {
                        var     signature = Render.Execute(pathRazorTemplate, userItem);
                        var     userName  = GetUser(userItem.PrimaryEmail);
                        dynamic obj       = userItem.Organizations;

                        serviceGmail.UpdateSignature(userName, signature);
                        Console.WriteLine("New signature for {0}", userName);
                    }
                }
            }
            else
            {
                Console.WriteLine("No users found.");
            }

            Console.WriteLine("Signatures updated");
        }
        private static void RunSample(GoogleMailSettingsService service)
        {
            try
            {
                // Create a new Label for the user testUserName
                service.CreateLabel(testUserName, "Test-Label");

                // Retrieve all labels for the user testUserName
                AppsExtendedFeed labels = service.RetrieveLabels(testUserName);
                Console.WriteLine(String.Format("First label: {0}",
                                                ((AppsExtendedEntry)labels.Entries[0]).getPropertyValueByName("label")));

                // Create a filter for emails from [email protected]
                // for the user testUserName and applies the new label "Test-Label"
                service.CreateFilter(testUserName, "test@" + domain, "", "", "", "", "", "Test-Label", "true", "");

                // Create a filter for emails having "important" in the subject
                // for the user testUserName to never send them to Spam and star them
                service.CreateFilter(testUserName, "", "", "important", "", "", "", "", "", "", "true", "true", "", "");

                // Create a new Send As for the user testUserName
                service.CreateSendAs(testUserName, "Test email", testUserName + "@" + domain, "", "");

                // Retrieve all send-as for user testUserName
                AppsExtendedFeed sendas = service.RetrieveSendAs(testUserName);
                Console.WriteLine(String.Format("First send-as: {0}",
                                                ((AppsExtendedEntry)sendas.Entries[0]).getPropertyValueByName("name")));

                // Updates the forwarding rule to forward emails to
                // test@domain for the user testUserName and keeps the email.
                service.UpdateForwarding(testUserName, "true", "test@" + domain, "KEEP");

                // Disable web clip for the user testUserName.
                service.UpdateWebclip(testUserName, "false");

                // Retrieve forwarding settings for user testUserName
                AppsExtendedEntry forwarding = service.RetrieveForwarding(testUserName);
                Console.WriteLine(String.Format("Forwarding to: {0}",
                                                forwarding.getPropertyValueByName("forwardTo")));

                // Deactivate POP for the user testUserName
                service.UpdatePop(testUserName, "false", null, null);

                // Retrieve POP settings for user testUserName
                AppsExtendedEntry pop = service.RetrievePop(testUserName);
                Console.WriteLine(String.Format("POP enabled: {0}",
                                                pop.getPropertyValueByName("enable")));

                // Activate IMAP for the user testUserName
                service.UpdateImap(testUserName, "true");

                // Retrieve IMAP settings for user testUserName
                AppsExtendedEntry imap = service.RetrieveImap(testUserName);
                Console.WriteLine(String.Format("IMAP enabled: {0}",
                                                imap.getPropertyValueByName("enable")));

                // Activate vacation autoresponse for the user testUserName
                service.UpdateVacation(testUserName, "true", "vacation", "vacation text...", "false", "true", "2012-01-15", "2012-01-22");

                // Retrieve vacation responder settings for user testUserName
                AppsExtendedEntry vacation = service.RetrieveVacation(testUserName);
                Console.WriteLine(String.Format("Vacation responder message: {0}",
                                                vacation.getPropertyValueByName("message")));

                // Update the signature for the user testUserName
                service.UpdateSignature(testUserName, "Signature text...");

                // Retrieve signature for user testUserName
                AppsExtendedEntry signature = service.RetrieveSignature(testUserName);
                Console.WriteLine(String.Format("Signature: {0}",
                                                signature.getPropertyValueByName("signature")));

                // Update the language settings to French (fr) for the user testUserName
                service.UpdateLanguage(testUserName, "fr");

                // Update general settings for the user testUserName
                service.UpdateGeneralSettings(testUserName, "50", "false", "false", "false", "false");

                // Create a new Delegate for the user testUserName
                service.CreateDelegate(testUserName, adminEmail);

                // Retrieve all delegates for the user testUserName
                AppsExtendedFeed delegates = service.RetrieveDelegates(testUserName);
                Console.WriteLine(String.Format("First delegate: {0}",
                                                ((AppsExtendedEntry)delegates.Entries[0]).getPropertyValueByName("delegationId")));

                // Delete the Delegate for the user testUserName
                service.DeleteDelegate(testUserName, adminEmail);
            }
            catch (AppsException a)
            {
                Console.WriteLine("A Google Apps error occurred.");
                Console.WriteLine();
                Console.WriteLine("Error code: {0}", a.ErrorCode);
                Console.WriteLine("Invalid input: {0}", a.InvalidInput);
                Console.WriteLine("Reason: {0}", a.Reason);
            }
        }
예제 #7
0
 public void Init()
 {
     service = new GoogleMailSettingsService("example.com", "apps-example.com");
 }