Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions gmail/snippets/src/main/java/EnableAutoReply.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Google LLC
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,13 +34,19 @@ public class EnableAutoReply {
/**
* Enables the auto reply
*
* @throws IOException
* @return the reply message and response metadata.
* @throws IOException - if service account credentials file not found.
*/
public static void autoReply() throws IOException{
// Load pre-authorized user credentials from the environment.
// TODO(developer) - See https://developers.google.com/identity for
// guides on implementing OAuth2 for your application.
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC));
public static VacationSettings autoReply() throws IOException{
// TODO(developer) - Replace with your email address.
String USER_EMAIL_ADDRESS = "gduser1@workspacesamples.dev";

/* Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity for
guides on implementing OAuth2 for your application. */
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped(Collections.singleton(GmailScopes.GMAIL_SETTINGS_BASIC))
.createDelegated(USER_EMAIL_ADDRESS);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
credentials);

Expand All @@ -57,12 +63,16 @@ public static void autoReply() throws IOException{
.setEnableAutoReply(true)
.setResponseBodyHtml("I am on vacation and will reply when I am back in the office. Thanks!")
.setRestrictToDomain(true)
.setStartTime(LocalDateTime.now().toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000)
.setEndTime(LocalDateTime.now().plusDays(7).toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000);
.setStartTime(LocalDateTime.now()
.toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000)
.setEndTime(LocalDateTime.now().plusDays(7)
.toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000);

VacationSettings response = service.users().settings().updateVacation("me", vacationSettings).execute();
VacationSettings response = service.users().settings()
.updateVacation("me", vacationSettings).execute();
// Prints the auto-reply response body
System.out.println("Enabled auto reply with message : "+response.getResponseBodyHtml());
return response;
} catch (GoogleJsonResponseException e) {
// TODO(developer) - handle error appropriately
System.err.println("Unable to enable auto reply: " + e.getDetails());
Expand Down
28 changes: 28 additions & 0 deletions gmail/snippets/src/test/java/TestEnableAutoReply.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


import com.google.api.services.gmail.model.VacationSettings;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertNotNull;

public class TestEnableAutoReply extends EnableAutoReply {

@Test
public void testAutoReply() throws IOException {
VacationSettings settings = EnableAutoReply.autoReply();
assertNotNull(settings);
}
}