管理轉寄功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用「設定」為帳戶設定轉寄功能。如要將電子郵件地址做為轉寄地址,該地址必須符合下列其中一項條件:
- 電子郵件地址已通過驗證。詳情請參閱「建立及驗證轉寄地址」。
- 電子郵件地址與寄件者位於相同網域。
- 電子郵件地址屬於寄件者所在網域的子網域。
- 電子郵件地址屬於網域別名,該別名已設定為相同 Google Workspace 帳戶的一部分。
如果轉寄電子郵件地址不符合其中一項規則,透過 API 設定轉寄功能就會失敗。
如要瞭解如何建立、列出、取得或刪除轉寄地址,請參閱 ForwardingAddresses 參考資料。
如要瞭解如何取得或更新轉送設定,請參閱設定參考資料
建立及驗證轉寄地址
您必須先建立轉寄地址,才能使用這項功能。在某些情況下,使用者也必須驗證地址擁有權。
如果 Gmail 要求驗證轉寄地址,系統會傳回狀態為 pending
的地址。系統會自動將驗證郵件傳送到目標電子郵件地址。電子郵件地址擁有者必須完成驗證程序,才能使用該地址。
不需要驗證的轉寄地址驗證狀態為 accepted
。
啟用自動轉寄功能
呼叫 updateAutoForwarding 方法,為帳戶啟用自動轉寄功能。這項呼叫需要已註冊並驗證的轉寄地址,以及對轉寄郵件採取的動作。
舉例來說,如要啟用自動轉寄功能,並將轉寄的郵件移至垃圾桶,請按照下列步驟操作:
如要停用自動轉寄功能,請呼叫 updateAutoForwarding,並將 enabled
屬性設為 false
。
轉寄特定訊息
自動轉寄功能會將所有收到的郵件傳送到目標帳戶。如要選擇性轉寄郵件,請使用篩選器建立規則,根據郵件屬性或內容轉寄郵件。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-01 (世界標準時間)。
[null,null,["上次更新時間:2025-08-01 (世界標準時間)。"],[],[],null,["# Managing Forwarding\n\nYou can use [Settings](/workspace/gmail/api/v1/reference/users/settings) to\nconfigure forwarding for an account. To be used as a forwarding email address,\nan address must fulfill one of the following criteria:\n\n- The email address has been verified. For further information, see [Creating and verifying forwarding addresses](#create)\n- The email address belongs to the same domain as the sender.\n- The email address belongs to a subdomain within the same domain of the sender.\n- The email address belongs to a domain alias configured as part of the same Google Workspace account.\n\nIf the forwarding email address doesn't adhere to one of these rules, setting up\nforwarding using the API fails.\n\nFor information on how to\n[create](/workspace/gmail/api/v1/reference/users/settings/forwardingAddresses/create),\n[list](/workspace/gmail/api/v1/reference/users/settings/forwardingAddresses/list),\n[get](/workspace/gmail/api/v1/reference/users/settings/forwardingAddresses/get),\nor [delete](/workspace/gmail/api/v1/reference/users/settings/forwardingAddresses/delete)\nforwarding addresses,\nsee the [ForwardingAddresses reference](/workspace/gmail/api/v1/reference/users/settings/forwardingAddresses).\n\nFor information on how to [get](/workspace/gmail/api/v1/reference/users/settings/getAutoForwarding) or\n[update](/workspace/gmail/api/v1/reference/users/settings/updateAutoForwarding) forwarding\nsettings, see the [Settings reference](/workspace/gmail/api/v1/reference/users/settings)\n\nCreating and verifying forwarding addresses\n-------------------------------------------\n\nYou must [create](/workspace/gmail/api/v1/reference/users/settings/forwardingAddresses/create)\nforwarding addresses prior to use. In some cases, users must also verify ownership of the\naddress as well.\n\nIf Gmail requires user verification for a forwarding address, the address is returned with the\nstatus `pending`. A verification message is automatically sent to the\ntarget email address. The owner of the email address must complete the verification\nprocess before it can be used.\n\nForwarding addresses that do not require verification have a verification status of `accepted`.\n\nEnabling auto-forwarding\n------------------------\n\nCall the [updateAutoForwarding](/workspace/gmail/api/v1/reference/users/settings/updateAutoForwarding)\nmethod to enable auto-forwarding for an account. The call requires both a registered\nand verified forwarding address as well as an action to take on forwarded messages.\n\nFor example, to enable auto-forwarding and move forwarded messages to the trash: \n\n### Java\n\ngmail/snippets/src/main/java/EnableForwarding.java \n[View on GitHub](https://github.com/googleworkspace/java-samples/blob/main/gmail/snippets/src/main/java/EnableForwarding.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.AutoForwarding;\nimport com.google.api.services.gmail.model.ForwardingAddress;\nimport com.google.auth.http.HttpCredentialsAdapter;\nimport com.google.auth.oauth2.GoogleCredentials;\nimport java.io.IOException;\n\n/* Class to demonstrate the use of Gmail Enable Forwarding API */\npublic class EnableForwarding {\n /**\n * Enable the auto-forwarding for an account.\n *\n * @param forwardingEmail - Email address of the recipient whose email will be forwarded.\n * @return forwarding id and metadata, {@code null} otherwise.\n * @throws IOException - if service account credentials file not found.\n */\n public static AutoForwarding enableAutoForwarding(String forwardingEmail) 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_SHARING);\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-forwarding and move forwarded messages to the trash\n ForwardingAddress address = new ForwardingAddress()\n .setForwardingEmail(forwardingEmail);\n ForwardingAddress createAddressResult = service.users().settings().forwardingAddresses()\n .create(\"me\", address).execute();\n if (createAddressResult.getVerificationStatus().equals(\"accepted\")) {\n AutoForwarding autoForwarding = new AutoForwarding()\n .setEnabled(true)\n .setEmailAddress(address.getForwardingEmail())\n .setDisposition(\"trash\");\n autoForwarding =\n service.users().settings().updateAutoForwarding(\"me\", autoForwarding).execute();\n System.out.println(autoForwarding.toPrettyString());\n return autoForwarding;\n }\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 forwarding: \" + e.getDetails());\n } else {\n throw e;\n }\n }\n return null;\n }\n}\n```\n\n### Python\n\ngmail/snippet/settings snippets/enable_forwarding.py \n[View on GitHub](https://github.com/googleworkspace/python-samples/blob/main/gmail/snippet/settings snippets/enable_forwarding.py) \n\n```python\nimport google.auth\nfrom googleapiclient.discovery import build\nfrom googleapiclient.errors import HttpError\n\n\ndef enable_forwarding():\n \"\"\"Enable email forwarding.\n Returns:Draft object, including forwarding id and result 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 address = {\"forwardingEmail\": \"gduser1@workspacesamples.dev\"}\n\n # pylint: disable=E1101\n result = (\n service.users()\n .settings()\n .forwardingAddresses()\n .create(userId=\"me\", body=address)\n .execute()\n )\n if result.get(\"verificationStatus\") == \"accepted\":\n body = {\n \"emailAddress\": result.get(\"forwardingEmail\"),\n \"enabled\": True,\n \"disposition\": \"trash\",\n }\n # pylint: disable=E1101\n result = (\n service.users()\n .settings()\n .updateAutoForwarding(userId=\"me\", body=body)\n .execute()\n )\n print(f\"Forwarding is enabled : {result}\")\n\n except HttpError as error:\n print(f\"An error occurred: {error}\")\n result = None\n\n return result\n\n\nif __name__ == \"__main__\":\n enable_forwarding()\n```\n\nTo disable autoforwarding, call [updateAutoForwarding](/workspace/gmail/api/v1/reference/users/settings/updateAutoForwarding)\nand set the `enabled` property to `false`.\n\nForwarding specific messages\n----------------------------\n\nAuto-forwarding sends all received messages to the target account. To\nselectively forward messages, use [filters](/workspace/gmail/api/guides/filter_settings)\nto create rules that forward in response to message attributes or content."]]