How to get the delated mails from gmail

To retrieve deleted emails from Gmail, you can use the Gmail API or IMAP protocol. Here are the steps:

Method 1: Using Gmail API

  1. Enable the Gmail API in the Google Cloud Console.
  2. Create a project and enable the Gmail API.
  3. Create credentials for your project (OAuth client ID).
  4. Install the Google API Client Library for your programming language (e.g., Python, Java, etc.).
  5. Use the library to authenticate with the Gmail API and retrieve deleted emails.

Here's an example in Python using the Google API Client Library:

import os
import base64
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

# Set up credentials
creds = Credentials.get_credentials()
service = build('gmail', 'v1', credentials=creds)

# Retrieve deleted emails
response = service.users().messages().list(userId='me', labelIds=['TRASH']).execute()
messages = response.get('messages', [])

for message in messages:
    message_id = message['id']
    message = service.users().messages().get(userId='me', id=message_id).execute()
    print(message['payload']['headers'][0]['value'])

Method 2: Using IMAP

  1. Enable IMAP access in your Gmail settings.
  2. Connect to your Gmail account using an IMAP client (e.g., Thunderbird, Microsoft Outlook, etc.).
  3. Use the IMAP client to retrieve deleted emails from the "Trash" folder.

Here's an example in Thunderbird:

  1. Open Thunderbird and create a new account.
  2. Enter your Gmail address and password.
  3. Select "Manual setup" and choose "IMAP" as the protocol.
  4. Set the incoming mail server to "imap.gmail.com" and the outgoing mail server to "smtp.gmail.com".
  5. Log in to your account and navigate to the "Trash" folder.
  6. Select the deleted emails you want to retrieve and drag them to a new folder or save them to a file.

Note: Deleted emails are only available for a limited time (usually 30 days) before they are permanently deleted. You should retrieve deleted emails as soon as possible to avoid losing them.

Remember to always follow Gmail's terms of service and usage guidelines when retrieving deleted emails.