forked from microsoftgraph/aspnet-snippets-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailController.cs
228 lines (194 loc) · 9.8 KB
/
MailController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the source repository root for complete license information.
*/
using Microsoft.Graph;
using Microsoft_Graph_ASPNET_Snippets.Helpers;
using Microsoft_Graph_ASPNET_Snippets.Models;
using Resources;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace Microsoft_Graph_ASPNET_Snippets.Controllers
{
[Authorize]
public class MailController : Controller
{
MailService mailService = new MailService();
public ActionResult Index()
{
return View("Mail");
}
// Get messages in all the current user's mail folders.
public async Task<ActionResult> GetMyMessages()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the messages.
results.Items = await mailService.GetMyMessages(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
// Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Get messages in the current user's inbox.
public async Task<ActionResult> GetMyInboxMessages()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the messages.
results.Items = await mailService.GetMyInboxMessages(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
// Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Get messages with attachments in the current user's inbox.
public async Task<ActionResult> GetMyInboxMessagesThatHaveAttachments()
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get messages in the Inbox folder that have file attachments.
results.Items = await mailService.GetMyInboxMessagesThatHaveAttachments(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
// Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Send an email message.
// This snippet sends a message to the current user on behalf of the current user.
public async Task<ActionResult> SendMessage()
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Send the message.
results.Items = await mailService.SendMessage(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
// Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Send an email message with a file attachment.
// This snippet sends a message to the current user on behalf of the current user.
public async Task<ActionResult> SendMessageWithAttachment()
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Send the message.
results.Items = await mailService.SendMessageWithAttachment(graphClient);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
// Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Get a specified message.
public async Task<ActionResult> GetMessage(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Get the message.
results.Items = await mailService.GetMessage(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
// Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Reply to a specified message.
public async Task<ActionResult> ReplyToMessage(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
results.Items = await mailService.ReplyToMessage(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Move a specified message. This creates a new copy of the message in the destination folder.
// This snippet moves the message to the Drafts folder.
public async Task<ActionResult> MoveMessage(string id)
{
ResultsViewModel results = new ResultsViewModel();
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Move the message.
results.Items = await mailService.MoveMessage(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
// Delete a specified message.
public async Task<ActionResult> DeleteMessage(string id)
{
ResultsViewModel results = new ResultsViewModel(false);
try
{
// Initialize the GraphServiceClient.
GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();
// Delete the message.
results.Items = await mailService.DeleteMessage(graphClient, id);
}
catch (ServiceException se)
{
if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
// Personal accounts that aren't enabled for the Outlook REST API get a "MailboxNotEnabledForRESTAPI" or "MailboxNotSupportedForRESTAPI" error.
return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
}
return View("Mail", results);
}
}
}