@@ -114,15 +114,21 @@ async def get_user_messages():
114114asyncio.run(get_user())
115115```
116116
117- ## 8. Post Request
117+ ## 8. Send Mail with User's delegation
118118
119119This sample sends an email. The request body is constructed using the provided models.
120120Ensure you have the [ right permissions] ( https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http#permissions ) .
121121
122+ Setup includes:
123+ 1 . "App Registration > Authentication > Platform: Mobile and desktop applications" with redirect_uri specified as http://localhost
124+ 2 . "Mail.Send" permissions, potentially requiring admin consent
125+
126+
122127``` py
128+ import asyncio
123129from msgraph import GraphServiceClient
124130
125- from msgraph.generated.me .send_mail.send_mail_post_request_body import SendMailPostRequestBody
131+ from msgraph.generated.users.item .send_mail.send_mail_post_request_body import SendMailPostRequestBody
126132from msgraph.generated.models.body_type import BodyType
127133from msgraph.generated.models.message import Message
128134from msgraph.generated.models.email_address import EmailAddress
@@ -131,46 +137,120 @@ from msgraph.generated.models.item_body import ItemBody
131137from msgraph.generated.models.recipient import Recipient
132138from msgraph.generated.users.item.messages.messages_request_builder import MessagesRequestBuilder
133139
140+ from azure.identity import InteractiveBrowserCredential
134141
135-
136- credential = ClientSecretCredential(
137- ' tenant_id ' ,
138- ' client_id ' ,
139- ' client_secret '
142+ credential = InteractiveBrowserCredential(
143+ client_id,
144+ authority ,
145+ tenant_id ,
146+ redirect_uri
140147)
141148scopes = [' Mail.Send' ]
149+ # alternatively, use "Mail.Send.Shared" for a shared mailbox.
150+
142151client = GraphServiceClient(credentials = credential, scopes = scopes)
143152
144153async def send_mail ():
145- try :
146- sender = EmailAddress()
147- sender.address = ' john.doe@outlook.com'
148- sender.name = ' John Doe'
149-
150- from_recipient = Recipient()
151- from_recipient.email_address = sender
152- recipients = []
153-
154- recipient_email = EmailAddress()
155- recipient_email.address = ' jane.doe@outlook.com'
156- recipient_email.name = ' Jane Doe'
157-
158- to_recipient = Recipient()
159- to_recipient.email_address = recipient_email
160- recipients.append(to_recipient)
161-
162- email_body = ItemBody()
163- email_body.content = ' Dummy content'
164- email_body.content_type = BodyType.Text
165-
166- message = Message()
167- message.subject = ' Test Email'
168- message.from_escaped = from_recipient
169- message.to_recipients = recipients
170- message.body = email_body
171-
172- request_body = SendMailPostRequestBody()
173- request_body.message = message
174- response = await client.me.send_mail.post(request_body)
154+
155+ sender = EmailAddress()
156+ sender.address = ' john.doe@outlook.com'
157+ sender.name = ' John Doe'
158+
159+ from_recipient = Recipient()
160+ from_recipient.email_address = sender
161+ recipients = []
162+
163+ recipient_email = EmailAddress()
164+ recipient_email.address = ' jane.doe@outlook.com'
165+ recipient_email.name = ' Jane Doe'
166+
167+ to_recipient = Recipient()
168+ to_recipient.email_address = recipient_email
169+ recipients.append(to_recipient)
170+
171+ email_body = ItemBody()
172+ email_body.content = ' Dummy content'
173+ email_body.content_type = BodyType.Text
174+
175+ message = Message()
176+ message.subject = ' Test Email'
177+ message.from_escaped = from_recipient
178+ message.to_recipients = recipients
179+ message.body = email_body
180+
181+ request_body = SendMailPostRequestBody()
182+ request_body.message = message
183+ response = await client.me.send_mail.post(request_body)
175184asyncio.run(send_mail())
176185```
186+ ## 8.1 Send Mail with Shared Mailbox and "Public Client" setup
187+ To use a shared mailbox (From) to which user (Sender) has access, the "From" recipient has to be additionally. Resulting in the following code.
188+
189+ Note that you require ** Mail.Send.Shared** permissions to send from shared mailboxes.
190+
191+ ``` py
192+ import asyncio
193+ from msgraph import GraphServiceClient
194+
195+ from msgraph.generated.models.body_type import BodyType
196+ from msgraph.generated.models.message import Message
197+ from msgraph.generated.models.email_address import EmailAddress
198+ from msgraph.generated.models.item_body import ItemBody
199+ from msgraph.generated.models.recipient import Recipient
200+ from msgraph.generated.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
201+
202+ from azure.identity import InteractiveBrowserCredential
203+
204+ # Create a credential object. Used to authenticate requests
205+ credential = InteractiveBrowserCredential(
206+ client_id,
207+ authority, # e.g. https://login.microsoftonline.com/ for public Azure cloud
208+ tenant_id,
209+ redirect_uri # as configured in your App Registration > Authentication > Platofrm: Mobile and desktop applications
210+ )
211+ scopes = [" Mail.Send.Shared" ]
212+
213+ # Create an API client with the credentials and scopes.
214+ client = GraphServiceClient(credentials = credential, scopes = scopes)
215+
216+
217+ async def send_mail ():
218+ sender = EmailAddress()
219+ sender.address = ' john.doe@outlook.com'
220+ sender.name = ' John Doe' # skip to use default
221+
222+ sender_recipient = Recipient()
223+ sender_recipient.email_address = sender
224+
225+ from_mailbox = EmailAddress()
226+ from_mailbox.address = ' your-shared-mailbox@outlook.com'
227+ # skip from_mailbox.name = ... to use the default display name of the shared mailbox
228+
229+ from_recipient = Recipient()
230+ from_recipient.email_address = from_mailbox
231+
232+ recipients = []
233+ recipient_email = EmailAddress()
234+ recipient_email.address = ' jane.doe@outlook.com'
235+ recipient_email.name = ' Jane Doe'
236+
237+ to_recipient = Recipient()
238+ to_recipient.email_address = recipient_email
239+ recipients.append(to_recipient)
240+
241+ email_body = ItemBody()
242+ email_body.content = ' Dummy content'
243+ email_body.content_type = BodyType.Text
244+
245+ message = Message()
246+ message.subject = ' Test Email'
247+ message.sender = sender_recipient
248+ message.from_ = from_recipient
249+ message.to_recipients = recipients
250+ message.body = email_body
251+
252+ request_body = SendMailPostRequestBody()
253+ request_body.message = message
254+ response = await client.me.send_mail.post(request_body)
255+ asyncio.run(send_mail())
256+ ```
0 commit comments