@@ -46,7 +46,7 @@ function listInboxSnippets() {
46
46
pageToken : pageToken
47
47
} ) ;
48
48
if ( threadList . threads && threadList . threads . length > 0 ) {
49
- threadList . threads . forEach ( function ( thread ) {
49
+ threadList . threads . forEach ( function ( thread ) {
50
50
console . log ( 'Snippet: %s' , thread . snippet ) ;
51
51
} ) ;
52
52
}
@@ -90,8 +90,8 @@ function logRecentHistory() {
90
90
} ) ;
91
91
const history = recordList . history ;
92
92
if ( history && history . length > 0 ) {
93
- history . forEach ( function ( record ) {
94
- record . messages . forEach ( function ( message ) {
93
+ history . forEach ( function ( record ) {
94
+ record . messages . forEach ( function ( message ) {
95
95
if ( changed . indexOf ( message . id ) === - 1 ) {
96
96
changed . push ( message . id ) ;
97
97
}
@@ -101,7 +101,7 @@ function logRecentHistory() {
101
101
pageToken = recordList . nextPageToken ;
102
102
} while ( pageToken ) ;
103
103
104
- changed . forEach ( function ( id ) {
104
+ changed . forEach ( function ( id ) {
105
105
console . log ( 'Message Changed: %s' , id ) ;
106
106
} ) ;
107
107
} catch ( err ) {
@@ -130,3 +130,33 @@ function getRawMessage() {
130
130
}
131
131
}
132
132
// [END gmail_raw]
133
+
134
+ // [START gmail_list_messages]
135
+ /**
136
+ * Lists unread messages in the user's inbox using the advanced Gmail service.
137
+ */
138
+ function listMessages ( ) {
139
+ // The special value 'me' indicates the authenticated user.
140
+ const userId = 'me' ;
141
+
142
+ // Define optional parameters for the request.
143
+ const options = {
144
+ maxResults : 10 , // Limit the number of messages returned.
145
+ q : 'is:unread' , // Search for unread messages.
146
+ } ;
147
+
148
+ try {
149
+ // Call the Gmail.Users.Messages.list method.
150
+ const response = Gmail . Users . Messages . list ( userId , options ) ;
151
+ const messages = response . messages ;
152
+ console . log ( 'Unread Messages:' ) ;
153
+
154
+ for ( const message of messages ) {
155
+ console . log ( `- Message ID: ${ message . id } ` ) ;
156
+ }
157
+ } catch ( err ) {
158
+ // Log any errors to the Apps Script execution log.
159
+ console . log ( `Failed with error: ${ err . message } ` ) ;
160
+ }
161
+ }
162
+ // [END gmail_list_messages]
0 commit comments