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; }
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); } }
protected override void ProcessRecord() { adminUser = credentials.UserName; adminPassword = new Dgc.ConvertToUnsecureString(credentials.Password).PlainString; var _domain = dgcGoogleAppsService.GetDomain(adminUser); try { //AppsService service.AppsService = new AppsService(_domain, adminUser, adminPassword); //CalendarService var _calendarService = new CalendarService("Calendar"); _calendarService.setUserCredentials(adminUser, adminPassword); service.CalendarService = _calendarService; //OauthCalendarService if (consumerKey != null) { if (consumerSecret == null) { throw new Exception("-ConsumerSecret can't be null"); } var _oauthCalendarService = new CalendarService("Calendar"); var _oauth = new GDataTypes.Oauth(); _oauth.ConsumerKey = consumerKey; _oauth.ConsumerSecret = consumerSecret; service.Oauth = _oauth; GOAuthRequestFactory _requestFactory = new GOAuthRequestFactory("cl", "GDataCmdLet"); _requestFactory.ConsumerKey = _oauth.ConsumerKey; _requestFactory.ConsumerSecret = _oauth.ConsumerSecret; _oauthCalendarService.RequestFactory = _requestFactory; service.OauthCalendarService = _oauthCalendarService; } //MailSettingsService var _googleMailSettingsService = new GoogleMailSettingsService(_domain, "GMailSettingsService"); _googleMailSettingsService.setUserCredentials(adminUser, adminPassword); service.GoogleMailSettingsService = _googleMailSettingsService; //ProfileService var _dgcGoogleProfileService = new Dgc.GoogleProfileService(); service.ProfileService = _dgcGoogleProfileService.GetAuthToken(adminUser, adminPassword); //ResourceService var _dgcGoogleResourceService = new Dgc.GoogleResourceService(); service.ResourceService = _dgcGoogleResourceService.GetAuthToken(adminUser, adminPassword); //ContactsService var _contactService = new ContactsService("GData"); _contactService.setUserCredentials(adminUser, adminPassword); service.ContactsService = _contactService; WriteObject(service); } catch (AppsException _exception) { WriteObject(_exception, true); } }
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); } }
/// <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); } }
public void Init() { service = new GoogleMailSettingsService("example.com", "apps-example.com"); }
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); } }
public void CreateSendAs(GmailUsers gusersyn, SqlDataReader userNicknames, string sendASFieldName, string replyToFieldName, LogFile log) { //AppsService service = new AppsService(gusersyn.Admin_domain, gusersyn.Admin_user + "@" + gusersyn.Admin_domain, gusersyn.Admin_password); GoogleMailSettingsService gmailSettings = new GoogleMailSettingsService(gusersyn.Admin_domain, gusersyn.Admin_domain); gmailSettings.setUserCredentials(gusersyn.Admin_user + "@" + gusersyn.Admin_domain, gusersyn.Admin_password); try { while (userNicknames.Read()) { try { gmailSettings.CreateSendAs((string)userNicknames[gusersyn.User_StuID], (string)userNicknames[gusersyn.User_Fname] + " " + (string)userNicknames[gusersyn.User_Lname], (string)userNicknames[sendASFieldName], (string)userNicknames[replyToFieldName], "true"); log.addTrn("Created send as alias " + (string)userNicknames[sendASFieldName] + " for userlogin " + (string)userNicknames[gusersyn.User_StuID], "Transaction"); } catch (Exception ex) { log.addTrn("Failed user send as creation " + ex.Message.ToString() + "\n" + ex.StackTrace.ToString(), "Error"); } } } catch (Exception ex) { log.addTrn("Issue creating send as datareader is null " + ex.Message.ToString() + "\n" + ex.StackTrace.ToString(), "Error"); } }
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"); }