Skip to content

Commit 6691736

Browse files
sanjuktaghosh7RajeshGovoManvendra-P-Singh
authored
Enable Auto Reply gmail snippet (googleworkspace#162)
* Enable Auto Reply gmail snippet. Co-authored-by: Rajesh Mudaliyar <[email protected]> Co-authored-by: Manvendra-P-Singh <[email protected]>
1 parent f7ca693 commit 6691736

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
// [START gmail_enable_auto_reply]
17+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
18+
import com.google.api.client.http.HttpRequestInitializer;
19+
import com.google.api.client.http.javanet.NetHttpTransport;
20+
import com.google.api.client.json.gson.GsonFactory;
21+
import com.google.api.services.gmail.Gmail;
22+
import com.google.api.services.gmail.GmailScopes;
23+
import com.google.api.services.gmail.model.VacationSettings;
24+
import com.google.auth.http.HttpCredentialsAdapter;
25+
import com.google.auth.oauth2.GoogleCredentials;
26+
import java.io.IOException;
27+
import java.time.LocalDateTime;
28+
import java.time.ZoneOffset;
29+
import java.time.ZonedDateTime;
30+
import java.util.Collections;
31+
32+
/* Class to demonstrate the use of Gmail Enable Auto Reply API*/
33+
public class EnableAutoReply {
34+
/**
35+
* Enables the auto reply
36+
*
37+
* @throws IOException
38+
*/
39+
public static void autoReply() throws IOException{
40+
// Load pre-authorized user credentials from the environment.
41+
// TODO(developer) - See https://developers.google.com/identity for
42+
// guides on implementing OAuth2 for your application.
43+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC));
44+
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
45+
credentials);
46+
47+
// Create the gmail API client
48+
Gmail service = new Gmail.Builder(new NetHttpTransport(),
49+
GsonFactory.getDefaultInstance(),
50+
requestInitializer)
51+
.setApplicationName("Gmail samples")
52+
.build();
53+
54+
try {
55+
// Enable auto reply by restricting domain with start time and end time
56+
VacationSettings vacationSettings = new VacationSettings()
57+
.setEnableAutoReply(true)
58+
.setResponseBodyHtml("I am on vacation and will reply when I am back in the office. Thanks!")
59+
.setRestrictToDomain(true)
60+
.setStartTime(LocalDateTime.now().toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000)
61+
.setEndTime(LocalDateTime.now().plusDays(7).toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000);
62+
63+
VacationSettings response = service.users().settings().updateVacation("me", vacationSettings).execute();
64+
// Prints the auto-reply response body
65+
System.out.println("Enabled auto reply with message : "+response.getResponseBodyHtml());
66+
} catch (GoogleJsonResponseException e) {
67+
// TODO(developer) - handle error appropriately
68+
System.err.println("Unable to enable auto reply: " + e.getDetails());
69+
throw e;
70+
}
71+
}
72+
}
73+
// [END gmail_enable_auto_reply]

0 commit comments

Comments
 (0)