Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ef8d99f
git-on-borg files of gmail-api-snippets
sanjuktaghosh7 Jan 14, 2022
f8d22fe
Merge pull request #2 from sanjuktaghosh7/delRel
RajeshGovo Jan 14, 2022
0c9e47a
Merge pull request #3 from RajeshGogo/delRel
RajeshGovo Jan 14, 2022
494c98e
Merge branch 'googleworkspace:master' into master
sanjuktaghosh7 Jan 19, 2022
cc07ac4
Update build.gradle
Manvendra-P-Singh Jan 19, 2022
dd55a6a
Merge pull request #8 from RajeshGogo/Manvendra-P-Singh-patch-1
RajeshGovo Jan 19, 2022
de85a1a
Merge pull request #9 from RajeshGogo/test1
RajeshGovo Jan 19, 2022
352491b
Update build.gradle
RajeshGovo Jan 19, 2022
5b70dce
Merge branch 'googleworkspace:master' into master
RajeshGovo Jan 20, 2022
740f231
Merge branch 'googleworkspace:master' into master
RajeshGovo Jan 25, 2022
60813f8
Merge branch 'googleworkspace:master' into master
Manvendra-P-Singh Feb 1, 2022
5e1358d
Merge branch 'googleworkspace:master' into master
RajeshGovo Feb 3, 2022
ed9ffec
Merge branch 'googleworkspace:master' into master
sanjuktaghosh7 Feb 8, 2022
ddca4b4
Merge branch 'googleworkspace:master' into master
RajeshGovo Feb 9, 2022
460e238
Merge branch 'googleworkspace:master' into master
RajeshGovo Feb 16, 2022
64950d1
Merge branch 'googleworkspace:master' into master
Manvendra-P-Singh Feb 25, 2022
a48179e
Merge branch 'googleworkspace:master' into master
Manvendra-P-Singh Mar 3, 2022
d58a5f8
Merge branch 'googleworkspace:master' into master
Manvendra-P-Singh Mar 8, 2022
3c749b1
Merge branch 'googleworkspace:master' into master
sanjuktaghosh7 Mar 8, 2022
452c7d0
Merge branch 'googleworkspace:master' into master
sanjuktaghosh7 Mar 16, 2022
fb4ed23
Merge branch 'googleworkspace:master' into master
Manvendra-P-Singh Mar 21, 2022
062907f
Merge branch 'googleworkspace:master' into master
Manvendra-P-Singh Apr 7, 2022
357a8cf
Merge branch 'googleworkspace:master' into master
RajeshGovo Apr 30, 2022
ecd9d0f
Merge branch 'googleworkspace:master' into master
sanjuktaghosh7 May 16, 2022
f613f2c
Merge branch 'googleworkspace:master' into master
Manvendra-P-Singh May 26, 2022
f9cbff5
Gmail-snippets
sanjuktaghosh7 May 31, 2022
873181d
Gmail unit testcase
sanjuktaghosh7 May 31, 2022
6a137d9
Merge branch 'googleworkspace:master' into gmail-unit-testcase
sanjuktaghosh7 Jun 1, 2022
ba9efa0
Merge branch 'RajeshGogo:gmail-unit-testcase' into gmail-unit-testcase
sanjuktaghosh7 Jun 1, 2022
8982da8
Merge pull request #97 from sanjuktaghosh7/gmail-unit-testcase
RajeshGovo Jun 1, 2022
f52e0fa
Gmail unit testcase
sanjuktaghosh7 Jun 6, 2022
e45a54d
Gmail snippet
sanjuktaghosh7 Jun 6, 2022
52b1ef0
Gmail snippet
sanjuktaghosh7 Jun 6, 2022
5cac873
Gmail snippet
sanjuktaghosh7 Jun 6, 2022
2dd8387
Gmail snippet
sanjuktaghosh7 Jun 6, 2022
74373a6
Merge pull request #100 from sanjuktaghosh7/gmail-unit-testcase
RajeshGovo Jun 7, 2022
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
14 changes: 5 additions & 9 deletions gmail/snippets/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ repositories {
}

dependencies {
implementation 'com.google.api-client:google-api-client:1.33.0'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.3.0'
implementation 'com.google.apis:google-api-services-gmail:v1-rev20211108-1.32.1'
implementation 'com.google.code.gson:gson:2.4'
implementation 'javax.mail:mail:1.4'
implementation 'org.apache.commons:commons-csv:1.1'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.6.0'
implementation 'com.google.apis:google-api-services-gmail:v1-rev20220404-1.32.1'
implementation 'javax.mail:mail:1.4.7'
implementation 'org.apache.commons:commons-csv:1.9.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.hamcrest:hamcrest-all:1.3'
//testImplementation 'org.mockito:mockito-core:4.2.0'
testImplementation 'org.mockito:mockito-inline:4.3.1'
testImplementation 'org.mockito:mockito-inline:4.5.1'
}
23 changes: 14 additions & 9 deletions gmail/snippets/src/main/java/CreateDraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


// [START gmail_create_draft]
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand All @@ -32,7 +33,6 @@
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Properties;

/* Class to demonstrate the use of Gmail Create Draft API */
Expand All @@ -42,16 +42,16 @@ public class CreateDraft {
*
* @param fromEmailAddress - Email address to appear in the from: header
* @param toEmailAddress - Email address of the recipient
* @return the created draft
* @throws MessagingException
* @throws IOException
* @return the created draft, {@code null} otherwise.
* @throws MessagingException - if a wrongly formatted address is encountered.
* @throws IOException - if service account credentials file not found.
*/
public static Draft createDraftMessage(String fromEmailAddress,
String toEmailAddress)
throws MessagingException, 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.
/* 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(GmailScopes.GMAIL_COMPOSE);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
Expand Down Expand Up @@ -95,9 +95,14 @@ public static Draft createDraftMessage(String fromEmailAddress,
return draft;
} catch (GoogleJsonResponseException e) {
// TODO(developer) - handle error appropriately
System.err.println("Unable to create draft: " + e.getDetails());
throw e;
GoogleJsonError error = e.getDetails();
if (error.getCode() == 403) {
System.err.println("Unable to create draft: " + e.getMessage());
} else {
throw e;
}
}
return null;
}
}
// [END gmail_create_draft]
24 changes: 15 additions & 9 deletions gmail/snippets/src/main/java/CreateDraftWithAttachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


// [START gmail_create_draft_with_attachment]
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand Down Expand Up @@ -45,24 +46,24 @@
/* Class to demonstrate the use of Gmail Create Draft with attachment API */
public class CreateDraftWithAttachment {
/**
* Create a draft email.
* Create a draft email with attachment.
*
* @param fromEmailAddress - Email address to appear in the from: header.
* @param toEmailAddress - Email address of the recipient.
* @param file - Path to the file to be attached.
* @return the created draft
* @return the created draft, {@code null} otherwise.
* @throws MessagingException - if a wrongly formatted address is encountered.
* @throws IOException - if service account credentials file not found.
*/
public static Draft createDraftMessageWithAttachment(String fromEmailAddress,
String toEmailAddress,
File file)
throws MessagingException, 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(
GmailScopes.GMAIL_COMPOSE);
/* 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(GmailScopes.GMAIL_COMPOSE);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

// Create the gmail API client
Expand Down Expand Up @@ -114,9 +115,14 @@ public static Draft createDraftMessageWithAttachment(String fromEmailAddress,
return draft;
} catch (GoogleJsonResponseException e) {
// TODO(developer) - handle error appropriately
System.err.println("Unable to create draft: " + e.getDetails());
throw e;
GoogleJsonError error = e.getDetails();
if (error.getCode() == 403){
System.err.println("Unable to create draft: " + e.getDetails());
} else {
throw e;
}
}
return null;
}
}
// [END gmail_create_draft_with_attachment]
53 changes: 53 additions & 0 deletions gmail/snippets/src/main/java/CreateEmail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2021 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.

// [START gmail_create_email]
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/* Class to demonstrate the use of Gmail Create Email API */
public class CreateEmail {

/**
* Create a MimeMessage using the parameters provided.
*
* @param toEmailAddress email address of the receiver
* @param fromEmailAddress email address of the sender, the mailbox account
* @param subject subject of the email
* @param bodyText body text of the email
* @return the MimeMessage to be used to send email
* @throws MessagingException - if a wrongly formatted address is encountered.
*/
public static MimeMessage createEmail(String toEmailAddress,
String fromEmailAddress,
String subject,
String bodyText)
throws MessagingException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

MimeMessage email = new MimeMessage(session);

email.setFrom(new InternetAddress(fromEmailAddress));
email.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress(toEmailAddress));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
}
// [END gmail_create_email]
28 changes: 13 additions & 15 deletions gmail/snippets/src/main/java/CreateFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


// [START gmail_create_filter]
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand All @@ -33,20 +34,17 @@ public class CreateFilter {
/**
* Create a new filter.
*
* @param realLabelId - ID of the user label to add
* @return the created filter id
* @param labelId - ID of the user label to add
* @return the created filter id, {@code null} otherwise.
* @throws IOException - if service account credentials file not found.
*/
public static String createNewFilter(String realLabelId) throws IOException {
// TODO(developer) - Replace with your email address.
String userEmail = "ci-test01@workspacesamples.dev";

public static String createNewFilter(String labelId) 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(GmailScopes.GMAIL_SETTINGS_BASIC, GmailScopes.GMAIL_LABELS)
.createDelegated(userEmail);
.createScoped(GmailScopes.GMAIL_SETTINGS_BASIC,
GmailScopes.GMAIL_LABELS);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

// Create the gmail API client
Expand All @@ -56,11 +54,6 @@ public static String createNewFilter(String realLabelId) throws IOException {
.setApplicationName("Gmail samples")
.build();

String labelId = "Label_14"; // ID of the user label to add
// [START_EXCLUDE silent]
labelId = realLabelId;
// [END_EXCLUDE]

try {
// Filter the mail from sender and archive them(skip the inbox)
Filter filter = new Filter()
Expand All @@ -76,9 +69,14 @@ public static String createNewFilter(String realLabelId) throws IOException {
return result.getId();
} catch (GoogleJsonResponseException e) {
// TODO(developer) - handle error appropriately
System.err.println("Unable to create filter: " + e.getDetails());
throw e;
GoogleJsonError error = e.getDetails();
if (error.getCode() == 403) {
System.err.println("Unable to create filter: " + e.getDetails());
} else {
throw e;
}
}
return null;
}
}
// [END gmail_create_filter]
45 changes: 45 additions & 0 deletions gmail/snippets/src/main/java/CreateMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.

// [START gmail_create_message]
import com.google.api.services.gmail.model.Message;
import org.apache.commons.codec.binary.Base64;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/* Class to demonstrate the use of Gmail Create Message API */
public class CreateMessage {

/**
* Create a message from an email.
*
* @param emailContent Email to be set to raw of message
* @return a message containing a base64url encoded email
* @throws IOException - if service account credentials file not found.
* @throws MessagingException - if a wrongly formatted address is encountered.
*/
public static Message createMessageWithEmail(MimeMessage emailContent)
throws MessagingException, IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
emailContent.writeTo(buffer);
byte[] bytes = buffer.toByteArray();
String encodedEmail = Base64.encodeBase64URLSafeString(bytes);
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
}
// [END gmail_create_message]
20 changes: 11 additions & 9 deletions gmail/snippets/src/main/java/EnableAutoReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


// [START gmail_enable_auto_reply]
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand All @@ -38,15 +39,11 @@ public class EnableAutoReply {
* @throws IOException - if service account credentials file not found.
*/
public static VacationSettings autoReply() throws IOException{
// TODO(developer) - Replace with your email address.
String userEmail = "ci-test01@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. */
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(userEmail);
.createScoped(GmailScopes.GMAIL_SETTINGS_BASIC);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

// Create the gmail API client
Expand Down Expand Up @@ -74,9 +71,14 @@ public static VacationSettings autoReply() throws IOException{
return response;
} catch (GoogleJsonResponseException e) {
// TODO(developer) - handle error appropriately
System.err.println("Unable to enable auto reply: " + e.getDetails());
throw e;
GoogleJsonError error = e.getDetails();
if (error.getCode() == 403) {
System.err.println("Unable to enable auto reply: " + e.getDetails());
} else {
throw e;
}
}
return null;
}
}
// [END gmail_enable_auto_reply]
17 changes: 9 additions & 8 deletions gmail/snippets/src/main/java/EnableForwarding.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


// [START gmail_enable_forwarding]
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
Expand All @@ -34,18 +35,14 @@ public class EnableForwarding {
*
* @param forwardingEmail - Email address of the recipient whose email will be forwarded.
* @return forwarding id and metadata, {@code null} otherwise.
* @throws IOException if service account credentials file not found.
* @throws IOException - if service account credentials file not found.
*/
public static AutoForwarding enableAutoForwarding(String forwardingEmail) throws IOException{
// TODO(developer) - Replace with your email address.
String userEmail = "ci-test01@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(GmailScopes.GMAIL_SETTINGS_SHARING)
.createDelegated(userEmail);
.createScoped(GmailScopes.GMAIL_SETTINGS_SHARING);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

// Create the gmail API client
Expand All @@ -72,8 +69,12 @@ public static AutoForwarding enableAutoForwarding(String forwardingEmail) throws
}
} catch (GoogleJsonResponseException e) {
// TODO(developer) - handle error appropriately
System.err.println("Unable to enable forwarding : " + e.getDetails());
throw e;
GoogleJsonError error = e.getDetails();
if (error.getCode() == 403) {
System.err.println("Unable to enable forwarding: " + e.getDetails());
} else {
throw e;
}
}
return null;
}
Expand Down
Loading