0
# Messages and Email
1
2
Complete email functionality including sending, receiving, replying, forwarding, and managing email messages. Supports both plain text and HTML content, attachments, and advanced email features.
3
4
## Capabilities
5
6
### Message Creation and Properties
7
8
The Message class represents email messages with comprehensive properties and methods for email operations.
9
10
```python { .api }
11
class Message:
12
def __init__(
13
self,
14
account: Account = None,
15
folder: Folder = None,
16
**kwargs
17
):
18
"""
19
Create a new email message.
20
21
Parameters:
22
- account: Account the message belongs to
23
- folder: Folder to save the message in
24
- **kwargs: Message properties (subject, body, to_recipients, etc.)
25
"""
26
27
# Core properties
28
subject: str
29
body: Body | HTMLBody
30
to_recipients: list[Mailbox]
31
cc_recipients: list[Mailbox]
32
bcc_recipients: list[Mailbox]
33
sender: Mailbox
34
author: Mailbox
35
datetime_sent: EWSDateTime
36
datetime_received: EWSDateTime
37
datetime_created: EWSDateTime
38
is_read: bool
39
importance: str
40
sensitivity: str
41
categories: list[str]
42
43
# Attachments
44
attachments: list[FileAttachment | ItemAttachment]
45
46
# Message threading
47
in_reply_to: str
48
references: str
49
conversation_id: str
50
conversation_topic: str
51
52
# Message status
53
is_draft: bool
54
is_from_me: bool
55
is_resend: bool
56
is_unmodified: bool
57
is_submitted: bool
58
59
# Extended properties
60
internet_message_headers: list
61
mime_content: bytes
62
```
63
64
### Sending Messages
65
66
Send messages with various options for delivery and saving.
67
68
```python { .api }
69
class Message:
70
def send(self, save_copy: bool = True, copy_to_folder: Folder = None):
71
"""
72
Send the message.
73
74
Parameters:
75
- save_copy: Whether to save a copy of the sent message
76
- copy_to_folder: Folder to save the sent copy in
77
"""
78
79
def send_and_save(self, update_fields: list = None):
80
"""
81
Send the message and save it to the Sent Items folder.
82
83
Parameters:
84
- update_fields: List of fields to update before sending
85
"""
86
```
87
88
Usage example:
89
90
```python
91
from exchangelib import Message, Mailbox, HTMLBody
92
93
# Create and send a message
94
message = Message(
95
account=account,
96
subject='Meeting Reminder',
97
body=HTMLBody('<p>Don\'t forget about our meeting tomorrow at 2 PM.</p>'),
98
to_recipients=[
99
Mailbox(email_address='colleague@company.com'),
100
Mailbox(email_address='manager@company.com')
101
],
102
cc_recipients=[Mailbox(email_address='team@company.com')]
103
)
104
105
message.send_and_save()
106
```
107
108
### Message Replies and Forwarding
109
110
Reply to and forward messages with automatic handling of recipients and message threading.
111
112
```python { .api }
113
class Message:
114
def reply(
115
self,
116
subject: str,
117
body: str,
118
to_recipients=None,
119
cc_recipients=None,
120
bcc_recipients=None,
121
author=None
122
):
123
"""
124
Reply to this message and send immediately.
125
126
Parameters:
127
- subject: Reply subject
128
- body: Reply body content
129
- to_recipients: Optional override for reply recipients
130
- cc_recipients: Optional CC recipients
131
- bcc_recipients: Optional BCC recipients
132
- author: Reply author (defaults to account owner)
133
"""
134
135
def reply_all(
136
self,
137
subject: str,
138
body: str,
139
author=None
140
):
141
"""
142
Reply to all recipients of this message and send immediately.
143
144
Parameters:
145
- subject: Reply subject
146
- body: Reply body content
147
- author: Reply author
148
"""
149
```
150
151
Usage example:
152
153
```python
154
# Reply to a message
155
original_message = account.inbox.all().order_by('-datetime_received')[0]
156
157
# Reply sends immediately, no need to call send_and_save()
158
original_message.reply(
159
subject='Re: ' + original_message.subject,
160
body='Thanks for the update!'
161
)
162
163
# Reply all sends immediately
164
original_message.reply_all(
165
subject='Re: ' + original_message.subject,
166
body='Thanks everyone for the update!'
167
)
168
```
169
170
### Message Content Types
171
172
Different content types for message bodies with support for plain text and HTML.
173
174
```python { .api }
175
class Body:
176
def __init__(self, body: str):
177
"""
178
Plain text message body.
179
180
Parameters:
181
- body: Plain text content
182
"""
183
184
body: str
185
body_type: str = 'Text'
186
187
class HTMLBody:
188
def __init__(self, body: str):
189
"""
190
HTML message body.
191
192
Parameters:
193
- body: HTML content
194
"""
195
196
body: str
197
body_type: str = 'HTML'
198
```
199
200
Usage example:
201
202
```python
203
from exchangelib import Body, HTMLBody
204
205
# Plain text message
206
plain_message = Message(
207
account=account,
208
subject='Plain Text Message',
209
body=Body('This is a plain text message.'),
210
to_recipients=[Mailbox(email_address='user@company.com')]
211
)
212
213
# HTML message
214
html_message = Message(
215
account=account,
216
subject='HTML Message',
217
body=HTMLBody('''
218
<html>
219
<body>
220
<h1>Important Update</h1>
221
<p>This is an <strong>HTML</strong> message with formatting.</p>
222
<ul>
223
<li>Point 1</li>
224
<li>Point 2</li>
225
</ul>
226
</body>
227
</html>
228
'''),
229
to_recipients=[Mailbox(email_address='user@company.com')]
230
)
231
```
232
233
### Message Management
234
235
CRUD operations for managing messages in folders.
236
237
```python { .api }
238
class Message:
239
def save(self, update_fields: list = None):
240
"""
241
Save the message to its folder.
242
243
Parameters:
244
- update_fields: List of specific fields to update
245
"""
246
247
def delete(self, delete_type: str = 'MoveToDeletedItems', send_meeting_cancellations: str = None):
248
"""
249
Delete the message.
250
251
Parameters:
252
- delete_type: 'HardDelete', 'SoftDelete', or 'MoveToDeletedItems'
253
- send_meeting_cancellations: How to handle meeting cancellations
254
"""
255
256
def move(self, to_folder: Folder):
257
"""
258
Move the message to another folder.
259
260
Parameters:
261
- to_folder: Destination folder
262
"""
263
264
def copy(self, to_folder: Folder):
265
"""
266
Copy the message to another folder.
267
268
Parameters:
269
- to_folder: Destination folder
270
"""
271
272
def mark_as_read(self):
273
"""Mark the message as read."""
274
275
def mark_as_unread(self):
276
"""Mark the message as unread."""
277
```
278
279
### Bulk Message Operations
280
281
Efficient bulk operations for handling multiple messages at once.
282
283
```python { .api }
284
def bulk_create(account: Account, items: list[Message], chunk_size: int = None) -> list[Message]:
285
"""
286
Create multiple messages in bulk.
287
288
Parameters:
289
- account: Account to create messages in
290
- items: List of Message objects to create
291
- chunk_size: Number of items to process per request
292
293
Returns:
294
List of created Message objects with server-assigned properties
295
"""
296
297
def bulk_update(account: Account, items: list[Message], chunk_size: int = None) -> list[Message]:
298
"""
299
Update multiple messages in bulk.
300
301
Parameters:
302
- account: Account containing the messages
303
- items: List of Message objects to update
304
- chunk_size: Number of items to process per request
305
306
Returns:
307
List of updated Message objects
308
"""
309
310
def bulk_delete(account: Account, items: list[Message], chunk_size: int = None, delete_type: str = 'MoveToDeletedItems') -> None:
311
"""
312
Delete multiple messages in bulk.
313
314
Parameters:
315
- account: Account containing the messages
316
- items: List of Message objects to delete
317
- chunk_size: Number of items to process per request
318
- delete_type: Type of deletion to perform
319
"""
320
```
321
322
Usage example:
323
324
```python
325
# Create multiple messages
326
messages = [
327
Message(
328
account=account,
329
subject=f'Bulk Message {i}',
330
body=Body(f'This is bulk message number {i}'),
331
to_recipients=[Mailbox(email_address='user@company.com')]
332
)
333
for i in range(5)
334
]
335
336
# Create all messages at once
337
created_messages = account.bulk_create(messages)
338
339
# Update multiple messages
340
for msg in created_messages:
341
msg.importance = 'High'
342
343
account.bulk_update(created_messages)
344
```
345
346
### Message Response Items
347
348
Special message types for handling calendar and meeting responses.
349
350
```python { .api }
351
class AcceptItem:
352
def __init__(self, reference_item_id: ItemId, **kwargs):
353
"""Accept a meeting request."""
354
355
class DeclineItem:
356
def __init__(self, reference_item_id: ItemId, **kwargs):
357
"""Decline a meeting request."""
358
359
class TentativelyAcceptItem:
360
def __init__(self, reference_item_id: ItemId, **kwargs):
361
"""Tentatively accept a meeting request."""
362
363
class ForwardItem:
364
def __init__(self, reference_item_id: ItemId, **kwargs):
365
"""Forward an item."""
366
367
class ReplyToItem:
368
def __init__(self, reference_item_id: ItemId, **kwargs):
369
"""Reply to an item."""
370
371
class ReplyAllToItem:
372
def __init__(self, reference_item_id: ItemId, **kwargs):
373
"""Reply all to an item."""
374
```
375
376
### Message Properties and Metadata
377
378
Extended properties and metadata for messages.
379
380
```python { .api }
381
class Message:
382
# Delivery and routing
383
message_id: str
384
internet_message_id: str
385
references: str
386
in_reply_to: str
387
388
# Security and classification
389
sensitivity: str # 'Normal', 'Personal', 'Private', 'Confidential'
390
importance: str # 'Low', 'Normal', 'High'
391
392
# Flags and status
393
flag: Flag
394
categories: list[str]
395
396
# Size and content
397
size: int
398
has_attachments: bool
399
is_associated: bool
400
401
# Cultural info
402
culture: str
403
404
# Extended properties
405
extended_properties: list[ExtendedProperty]
406
```