مدیریت تنظیمات تعطیلات با Gmail API
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این سند نحوه استفاده از پاسخگوی تعطیلات در API جیمیل را توضیح میدهد.
شما میتوانید از منبع settings برای پیکربندی پاسخ خودکار برای یک حساب کاربری استفاده کنید.
برای اطلاعات بیشتر در مورد نحوه دریافت یا بهروزرسانی تنظیمات پاسخگوی تعطیلات، به منبع settings مراجعه کنید.
پیکربندی پاسخ خودکار
پاسخ خودکار نیاز به یک موضوع و متن پاسخ به صورت HTML یا متن ساده دارد. این موارد با استفاده از شیء VacationSettings تنظیم میشوند. میتوانید پاسخ خودکار را به طور نامحدود فعال کنید یا آن را به یک دوره زمانی خاص محدود کنید. همچنین میتوانید پاسخ خودکار را به مخاطبین شناخته شده یا اعضای دامنه محدود کنید.
نمونههای کد زیر نحوه تنظیم پاسخ خودکار برای یک دوره زمانی ثابت و محدود کردن پاسخها به کاربران در همان دامنه را نشان میدهد:
importcom.google.api.client.googleapis.json.GoogleJsonError;importcom.google.api.client.googleapis.json.GoogleJsonResponseException;importcom.google.api.client.http.HttpRequestInitializer;importcom.google.api.client.http.javanet.NetHttpTransport;importcom.google.api.client.json.gson.GsonFactory;importcom.google.api.services.gmail.Gmail;importcom.google.api.services.gmail.GmailScopes;importcom.google.api.services.gmail.model.VacationSettings;importcom.google.auth.http.HttpCredentialsAdapter;importcom.google.auth.oauth2.GoogleCredentials;importjava.io.IOException;importjava.time.LocalDateTime;importjava.time.ZoneOffset;importjava.time.ZonedDateTime;/* Class to demonstrate the use of Gmail Enable Auto Reply API*/publicclassEnableAutoReply{/** * Enables the auto reply * * @return the reply message and response metadata. * @throws IOException - if service account credentials file not found. */publicstaticVacationSettingsautoReply()throwsIOException{/* Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for your application. */GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault().createScoped(GmailScopes.GMAIL_SETTINGS_BASIC);HttpRequestInitializerrequestInitializer=newHttpCredentialsAdapter(credentials);// Create the gmail API clientGmailservice=newGmail.Builder(newNetHttpTransport(),GsonFactory.getDefaultInstance(),requestInitializer).setApplicationName("Gmail samples").build();try{// Enable auto reply by restricting domain with start time and end timeVacationSettingsvacationSettings=newVacationSettings().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);VacationSettingsresponse=service.users().settings().updateVacation("me",vacationSettings).execute();// Prints the auto-reply response bodySystem.out.println("Enabled auto reply with message : "+response.getResponseBodyHtml());returnresponse;}catch(GoogleJsonResponseExceptione){// TODO(developer) - handle error appropriatelyGoogleJsonErrorerror=e.getDetails();if(error.getCode()==403){System.err.println("Unable to enable auto reply: "+e.getDetails());}else{throwe;}}returnnull;}}
fromdatetimeimportdatetime,timedeltaimportgoogle.authfromgoogleapiclient.discoveryimportbuildfromgoogleapiclient.errorsimportHttpErrorfromnumpyimportlongdefenable_auto_reply():"""Enable auto reply. Returns:Draft object, including reply message and response meta data. Load pre-authorized user credentials from the environment. TODO(developer) - See https://developers.google.com/identity for guides on implementing OAuth2 for the application. """creds,_=google.auth.default()try:# create gmail api clientservice=build("gmail","v1",credentials=creds)epoch=datetime.utcfromtimestamp(0)now=datetime.now()start_time=(now-epoch).total_seconds()*1000end_time=(now+timedelta(days=7)-epoch).total_seconds()*1000vacation_settings={"enableAutoReply":True,"responseBodyHtml":("I am on vacation and will reply when I am ""back in the office. Thanks!"),"restrictToDomain":True,"startTime":long(start_time),"endTime":long(end_time),}# pylint: disable=E1101response=(service.users().settings().updateVacation(userId="me",body=vacation_settings).execute())print(f"Enabled AutoReply with message: {response.get('responseBodyHtml')}")exceptHttpErroraserror:print(f"An error occurred: {error}")response=Nonereturnresponseif__name__=="__main__":enable_auto_reply()
برای غیرفعال کردن پاسخ خودکار، متد settings.updateVacation را فراخوانی کنید و فیلد enableAutoReply را در شیء VacationSettings روی false تنظیم کنید. اگر مقدار endTime را تنظیم کنید، پاسخ خودکار پس از گذشت زمان مشخص شده غیرفعال میشود.
تاریخ آخرین بهروزرسانی 2026-03-16 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2026-03-16 بهوقت ساعت هماهنگ جهانی."],[],[]]