@@ -0,0 +1,3702 @@
+// Copyright 2022 Google LLC%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     https://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0A%0A// %5BSTART gmail_enable_forwarding%5D%0Aimport com.google.api.client.googleapis.json.GoogleJsonResponseException;%0Aimport com.google.api.client.http.HttpRequestInitializer;%0Aimport com.google.api.client.http.javanet.NetHttpTransport;%0Aimport com.google.api.client.json.gson.GsonFactory;%0Aimport com.google.api.services.gmail.Gmail;%0Aimport com.google.api.services.gmail.GmailScopes;%0Aimport com.google.api.services.gmail.model.AutoForwarding;%0Aimport com.google.api.services.gmail.model.ForwardingAddress;%0Aimport com.google.auth.http.HttpCredentialsAdapter;%0Aimport com.google.auth.oauth2.GoogleCredentials;%0Aimport java.io.IOException;%0Aimport java.util.Collections;%0A%0A/* Class to demonstrate the use of Gmail Enable Forwarding API */%0Apublic class EnableForwarding %7B%0A    /**%0A     * Enable the auto-forwarding for an account.%0A     *%0A     * @param forwardingEmail - Email address of the recipient whose email will be forwarded.%0A     * @return forwarding id and metadata, %7B@code null%7D otherwise.%0A     * @throws IOException if service account credentials file not found.%0A     */%0A    public static AutoForwarding enableAutoForwarding(String forwardingEmail) throws IOException%7B%0A        // Load pre-authorized user credentials from the environment.%0A        // TODO(developer) - See https://developers.google.com/identity for%0A        // guides on implementing OAuth2 for your application.%0A        GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singletonList(GmailScopes.GMAIL_SETTINGS_SHARING));%0A        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(%0A                credentials);%0A%0A        // Create the gmail API client%0A        Gmail service = new Gmail.Builder(new NetHttpTransport(),%0A                GsonFactory.getDefaultInstance(),%0A                requestInitializer)%0A                .setApplicationName(%22Gmail samples%22)%0A                .build();%0A%0A        try%7B%0A            // Enable auto-forwarding and move forwarded messages to the trash%0A            ForwardingAddress address = new ForwardingAddress()%0A                    .setForwardingEmail(forwardingEmail);%0A            ForwardingAddress createAddressResult = service.users().settings().forwardingAddresses()%0A                    .create(%22me%22, address).execute();%0A            if (createAddressResult.getVerificationStatus().equals(%22accepted%22)) %7B%0A                AutoForwarding autoForwarding = new AutoForwarding()%0A                        .setEnabled(true)%0A                        .setEmailAddress(address.getForwardingEmail())%0A                        .setDisposition(%22trash%22);%0A                autoForwarding = service.users().settings().updateAutoForwarding(%22me%22, autoForwarding).execute();%0A                System.out.println(autoForwarding.toPrettyString());%0A                return autoForwarding;%0A            %7D%0A        %7D catch (GoogleJsonResponseException e) %7B%0A            // TODO(developer) - handle error appropriately%0A            System.err.println(%22Unable to enable forwarding : %22 + e.getDetails());%0A            throw e;%0A        %7D%0A        return null;%0A    %7D%0A%7D%0A// %5BEND gmail_enable_forwarding%5D

Enable Forwarding gmail snippet (#175)