public async Task<ActionResult> Send(MyMessage myEvent) { myEvent.FromName = User.Identity.Name; myEvent.FromEmailAddress = User.Identity.Name; await _repo.SendMessage(myEvent); return Redirect("/Mail"); }
public async Task<List<MyMessage>> GetMessages(int pageIndex, int pageSize) { var client = await EnsureClientCreated(); var messageResults = await (from message in client.Me.Messages orderby message.DateTimeSent descending select message) .Skip(pageIndex * pageSize) .Take(pageSize) .ExecuteAsync(); MorePagesAvailable = messageResults.MorePagesAvailable; var messageList = new List<MyMessage>(); foreach (IMessage message in messageResults.CurrentPage) { var myMessage = new MyMessage { Id = message.Id, Subject = message.Subject, DateTimeReceived = message.DateTimeReceived, FromName = message.From.EmailAddress.Name, FromEmailAddress = message.From.EmailAddress.Address, ToName = message.ToRecipients[0].EmailAddress.Name, ToEmailAddress= message.ToRecipients[0].EmailAddress.Address, HasAttachments = message.HasAttachments }; messageList.Add(myMessage); } return messageList; }
public async Task<List<MyMessage>> GetMessages(int pageIndex, int pageSize) { List<MyMessage> messageList = new List<MyMessage>(); try { string restURL = string.Format("{0}/me/messages?$orderby=SentDateTime desc&$skip={1}&$top={2}", GraphResourceUrl, pageIndex * pageSize, pageSize); string responseString = await GetJsonAsync(restURL); if (responseString != null) { MorePagesAvailable = !JObject.Parse(responseString)["@odata.nextLink"].IsNullOrEmpty(); var jsonresult = JObject.Parse(responseString)["value"]; foreach (var item in jsonresult) { var msg = new MyMessage(); msg.Id = item["id"].IsNullOrEmpty() ? string.Empty : item["id"].ToString(); msg.Subject = item["subject"].IsNullOrEmpty() ? string.Empty : item["subject"].ToString(); msg.DateTimeReceived = item["receivedDateTime"].IsNullOrEmpty() ? new DateTime() : DateTime.Parse(item["receivedDateTime"].ToString()); if (!item["from"].IsNullOrEmpty() && !item["from"]["emailAddress"].IsNullOrEmpty()) { msg.FromName = item["from"]["emailAddress"]["name"].IsNullOrEmpty() ? string.Empty : item["from"]["emailAddress"]["name"].ToString(); msg.FromEmailAddress = item["from"]["emailAddress"]["address"].IsNullOrEmpty() ? string.Empty : item["from"]["emailAddress"]["address"].ToString(); } if (!item["toRecipients"].IsNullOrEmpty()) { var to = item["toRecipients"].ToArray(); if (!to[0]["emailAddress"].IsNullOrEmpty()) { msg.ToName = to[0]["emailAddress"]["name"].IsNullOrEmpty() ? string.Empty : to[0]["emailAddress"]["name"].ToString(); msg.ToEmailAddress = to[0]["emailAddress"]["address"].IsNullOrEmpty() ? string.Empty : to[0]["emailAddress"]["address"].ToString(); } } messageList.Add(msg); } } } catch (Exception el) { Debug.WriteLine("GetMessages error: " + el.ToString()); } return messageList; }
public async Task<MyMessage> GetMessage(string id) { var client = await EnsureClientCreated(); var existingMessage = await client.Me.Messages.GetById(id).ExecuteAsync(); var newMessage = new MyMessage { Id = existingMessage.Id, ConversationId = existingMessage.ConversationId, Subject = existingMessage.Subject, DateTimeSent = existingMessage.DateTimeSent, DateTimeReceived = existingMessage.DateTimeReceived, FromName = existingMessage.From.EmailAddress.Name, FromEmailAddress = existingMessage.From.EmailAddress.Address, Body = existingMessage.Body.Content ?? string.Empty, HasAttachments = existingMessage.HasAttachments, ToName = existingMessage.ToRecipients[0].EmailAddress.Name, ToEmailAddress = existingMessage.ToRecipients[0].EmailAddress.Address }; return newMessage; }
public async Task SendMessage(MyMessage myMessage) { var client = await EnsureClientCreated(); var newMessage = new Message {Subject = myMessage.Subject}; var email = new EmailAddress { Name = myMessage.ToName, Address = myMessage.ToEmailAddress }; newMessage.ToRecipients.Add(new Recipient { EmailAddress = email }); newMessage.Body = new ItemBody { ContentType = BodyType.Text, Content = myMessage.Body }; await client.Me.Messages.AddMessageAsync(newMessage); await newMessage.SendAsync(); }
public async Task<MyMessage> GetMessage(string id) { try { var restURL = string.Format("{0}/me/messages/{1}", GraphResourceUrl, id); string responseString = await GetJsonAsync(restURL); if (responseString != null) { var jsonresult = JObject.Parse(responseString); var msg = new MyMessage(); msg.Id = jsonresult["id"].IsNullOrEmpty() ? string.Empty : jsonresult["id"].ToString(); msg.Subject = jsonresult["subject"].IsNullOrEmpty() ? string.Empty : jsonresult["subject"].ToString(); msg.DateTimeReceived = jsonresult["receivedDateTime"].IsNullOrEmpty() ? new DateTime() : DateTime.Parse(jsonresult["receivedDateTime"].ToString()); msg.DateTimeSent = jsonresult["sentDateTime"].IsNullOrEmpty() ? new DateTime() : DateTime.Parse(jsonresult["sentDateTime"].ToString()); if (!jsonresult["from"].IsNullOrEmpty() && !jsonresult["from"]["emailAddress"].IsNullOrEmpty()) { msg.FromName = jsonresult["from"]["emailAddress"]["name"].IsNullOrEmpty() ? string.Empty : jsonresult["from"]["emailAddress"]["name"].ToString(); msg.FromEmailAddress = jsonresult["from"]["emailAddress"]["address"].IsNullOrEmpty() ? string.Empty : jsonresult["from"]["emailAddress"]["address"].ToString(); } if (!jsonresult["toRecipients"].IsNullOrEmpty()) { var to = jsonresult["toRecipients"].ToArray(); if (!to[0]["emailAddress"].IsNullOrEmpty()) { msg.ToName = to[0]["emailAddress"]["name"].IsNullOrEmpty() ? string.Empty : to[0]["emailAddress"]["name"].ToString(); msg.ToEmailAddress = to[0]["emailAddress"]["address"].IsNullOrEmpty() ? string.Empty : to[0]["emailAddress"]["address"].ToString(); } } if (!jsonresult["body"].IsNullOrEmpty()) { msg.Body = jsonresult["body"]["content"].IsNullOrEmpty() ? string.Empty : jsonresult["body"]["content"].ToString(); } return msg; } } catch (Exception el) { Debug.WriteLine("GetMessage error: " + el.ToString()); } return null; }
public async Task SendMessage(MyMessage myMessage) { var restURL = string.Format("{0}/me/Microsoft.Graph.sendMail", GraphResourceUrl); string accessToken = await GetGraphAccessTokenAsync(); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var to = new { EmailAddress = new { Name = myMessage.ToName, Address = myMessage.ToEmailAddress } }; var msg = new { Message = new { Subject = myMessage.Subject, Body = new { ContentType = "TEXT", Content = myMessage.Body }, ToRecipients = new[] { to } }, SaveToSentItems = true }; string postBody = JsonConvert.SerializeObject(msg); using (var response = await client.PostAsync(restURL, new StringContent(postBody, Encoding.UTF8, "application/json"))) { if (response.IsSuccessStatusCode) return; else Debug.WriteLine("SendMessage error: " + response.StatusCode); } } }