A TypeScript application for fetching emails from Microsoft Outlook using Microsoft Graph API.
- Node.js (v14 or higher)
- npm (Node Package Manager)
- Microsoft Azure Account with registered application
- Required Microsoft Graph API permissions:
- User.Read.All (application permission)
- Mail.Read or Mail.ReadBasic.All (application permission)
- Clone the repository:
git clone [your-repository-url]
cd [your-repository-name]
- Install dependencies:
npm install
- Create
.env
file in the root directory with your Azure credentials:
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret
TENANT_ID=your_tenant_id
Alter-oferty-mailowe/
├── src/ # Source files
│ ├── config/ # Configuration files
│ │ └── environment.ts # Environment configuration
│ ├── interfaces/ # TypeScript interfaces
│ │ ├── email.ts
│ │ ├── credentials.ts
│ │ └── graph.ts
│ ├── services/ # Business logic services
│ │ ├── graphClient.ts # Microsoft Graph client setup
│ │ └── mailService.ts # Email fetching logic
│ ├── utils/ # Utility functions
│ │ ├── logger.ts
│ │ └── fileSystem.ts
│ └── index.ts # Application entry point
├── dist/ # Compiled JavaScript files
├── tests/ # Test files
├── data/ # Output directory for emails
├── .env # Environment variables
├── .env.example # Example environment file
├── .gitignore
├── package.json
├── package-lock.json
├── tsconfig.json
└── README.md
Make sure your Azure Application has the required API permissions in Azure Portal:
- Go to Azure Portal
- Navigate to Azure Active Directory → App registrations
- Select your application
- Go to "API permissions"
- Add necessary permissions (User.Read.All and Mail.Read)
- Grant admin consent
- Build the project:
npm run build
- Run the application:
npm start
The application will:
- List all available users in your organization
- Fetch emails from the first user's inbox
- Save emails as JSON files in the
data
directory
mail-fetcher.ts
- Main application filedata/
- Directory where emails are saved.env
- Environment variables configurationtsconfig.json
- TypeScript configurationpackage.json
- Project dependencies and scripts
- @microsoft/microsoft-graph-client
- @azure/identity
- dotenv
- TypeScript (dev dependency)
- Never commit your
.env
file to version control - Keep your Azure credentials secure
- Follow the principle of least privilege when assigning permissions