Quản lý chế độ cài đặt kỳ nghỉ
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể sử dụng phần Cài đặt để định cấu hình tính năng trả lời tự động theo lịch cho một tài khoản.
Để biết thông tin về cách nhận hoặc cập nhật, hãy xem Nội dung tham khảo về chế độ cài đặt.
Định cấu hình tính năng trả lời tự động
Tính năng trả lời tự động yêu cầu phải có tiêu đề và nội dung phản hồi, ở dạng HTML hoặc văn bản thuần tuý. Bạn có thể bật chế độ này vô thời hạn hoặc giới hạn trong một khoảng thời gian xác định. Bạn cũng có thể giới hạn tính năng trả lời tự động cho những người liên hệ đã biết hoặc hội viên của miền.
Ví dụ về cách thiết lập tính năng trả lời tự động trong một khoảng thời gian cố định, chỉ cho phép người dùng trong cùng miền trả lời:
Để tắt tính năng trả lời tự động, hãy cập nhật tài nguyên và đặt enableAutoReply
thành false
. Nếu bạn thiết lập endTime
, tính năng trả lời tự động sẽ tự động tắt sau khi thời gian đã chỉ định trôi qua.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-29 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-29 UTC."],[],[],null,["# Managing Vacation Settings\n\nYou can use [Settings](/workspace/gmail/api/v1/reference/users/settings) to\nconfigure scheduled [auto-reply](https://support.google.com/mail/answer/25922) for an account.\n\nFor information on how to\n[get](/workspace/gmail/api/v1/reference/users/settings/getVacation) or\n[update](/workspace/gmail/api/v1/reference/users/settings/updateVacation),\nsee the [Settings reference](/workspace/gmail/api/v1/reference/users/settings).\n\nConfiguring auto-reply\n----------------------\n\nAuto-reply requires a response subject and body, either HTML or plain text. It\ncan be enabled indefinitely, or limited to a defined period of time. You can\nalso restrict auto-reply to known contacts or domain members.\n\nExample of setting an auto-reply for a fixed period of time, restricting replies\nto users in the same domain: \n\n### Java\n\ngmail/snippets/src/main/java/EnableAutoReply.java \n[View on GitHub](https://github.com/googleworkspace/java-samples/blob/main/gmail/snippets/src/main/java/EnableAutoReply.java) \n\n```java\nimport com.google.api.client.googleapis.json.GoogleJsonError;\nimport com.google.api.client.googleapis.json.GoogleJsonResponseException;\nimport com.google.api.client.http.HttpRequestInitializer;\nimport com.google.api.client.http.javanet.NetHttpTransport;\nimport com.google.api.client.json.gson.GsonFactory;\nimport com.google.api.services.gmail.Gmail;\nimport com.google.api.services.gmail.GmailScopes;\nimport com.google.api.services.gmail.model.VacationSettings;\nimport com.google.auth.http.HttpCredentialsAdapter;\nimport com.google.auth.oauth2.GoogleCredentials;\nimport java.io.IOException;\nimport java.time.LocalDateTime;\nimport java.time.ZoneOffset;\nimport java.time.ZonedDateTime;\n\n/* Class to demonstrate the use of Gmail Enable Auto Reply API*/\npublic class EnableAutoReply {\n /**\n * Enables the auto reply\n *\n * @return the reply message and response metadata.\n * @throws IOException - if service account credentials file not found.\n */\n public static VacationSettings autoReply() throws IOException {\n /* Load pre-authorized user credentials from the environment.\n TODO(developer) - See https://developers.google.com/identity for\n guides on implementing OAuth2 for your application. */\n GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()\n .createScoped(GmailScopes.GMAIL_SETTINGS_BASIC);\n HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);\n\n // Create the gmail API client\n Gmail service = new Gmail.Builder(new NetHttpTransport(),\n GsonFactory.getDefaultInstance(),\n requestInitializer)\n .setApplicationName(\"Gmail samples\")\n .build();\n\n try {\n // Enable auto reply by restricting domain with start time and end time\n VacationSettings vacationSettings = new VacationSettings()\n .setEnableAutoReply(true)\n .setResponseBodyHtml(\n \"I am on vacation and will reply when I am back in the office. Thanks!\")\n .setRestrictToDomain(true)\n .setStartTime(LocalDateTime.now()\n .toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000)\n .setEndTime(LocalDateTime.now().plusDays(7)\n .toEpochSecond(ZoneOffset.from(ZonedDateTime.now())) * 1000);\n\n VacationSettings response = service.users().settings()\n .updateVacation(\"me\", vacationSettings).execute();\n // Prints the auto-reply response body\n System.out.println(\"Enabled auto reply with message : \" + response.getResponseBodyHtml());\n return response;\n } catch (GoogleJsonResponseException e) {\n // TODO(developer) - handle error appropriately\n GoogleJsonError error = e.getDetails();\n if (error.getCode() == 403) {\n System.err.println(\"Unable to enable auto reply: \" + e.getDetails());\n } else {\n throw e;\n }\n }\n return null;\n }\n}\n```\n\n### Python\n\ngmail/snippet/settings snippets/enable_auto_reply.py \n[View on GitHub](https://github.com/googleworkspace/python-samples/blob/main/gmail/snippet/settings snippets/enable_auto_reply.py) \n\n```python\nfrom datetime import datetime, timedelta\n\nimport google.auth\nfrom googleapiclient.discovery import build\nfrom googleapiclient.errors import HttpError\nfrom numpy import long\n\n\ndef enable_auto_reply():\n \"\"\"Enable auto reply.\n Returns:Draft object, including reply message and response meta data.\n\n Load pre-authorized user credentials from the environment.\n TODO(developer) - See https://developers.google.com/identity\n for guides on implementing OAuth2 for the application.\n \"\"\"\n creds, _ = google.auth.default()\n\n try:\n # create gmail api client\n service = build(\"gmail\", \"v1\", credentials=creds)\n\n epoch = datetime.utcfromtimestamp(0)\n now = datetime.now()\n start_time = (now - epoch).total_seconds() * 1000\n end_time = (now + timedelta(days=7) - epoch).total_seconds() * 1000\n vacation_settings = {\n \"enableAutoReply\": True,\n \"responseBodyHtml\": (\n \"I am on vacation and will reply when I am \"\n \"back in the office. Thanks!\"\n ),\n \"restrictToDomain\": True,\n \"startTime\": long(start_time),\n \"endTime\": long(end_time),\n }\n\n # pylint: disable=E1101\n response = (\n service.users()\n .settings()\n .updateVacation(userId=\"me\", body=vacation_settings)\n .execute()\n )\n print(f\"Enabled AutoReply with message: {response.get('responseBodyHtml')}\")\n\n except HttpError as error:\n print(f\"An error occurred: {error}\")\n response = None\n\n return response\n\n\nif __name__ == \"__main__\":\n enable_auto_reply()\n```\n\nTo disable auto-reply, update the resource and set `enableAutoReply` to\n`false`. If an `endTime` is configured, auto-reply will automatically disable\nonce the specified time has passed."]]