// Test GraphService.SendEmail method.
// Sends an email to the test account from the test account.
// Success: Retrieved message body matches sent message body.
public async Task SendEmail()
{
// Arrange
string testBody = Guid.NewGuid().ToString();
GraphService graphService = new GraphService();
List<Recipient> recipientList = new List<Recipient>();
recipientList.Add(new Recipient
{
EmailAddress = new UserInfo
{
Address = userName
}
});
Message message = new Message
{
Body = new ItemBody
{
Content = "<html><body>" + testBody + "</body></html>",
ContentType = "HTML"
},
Subject = "Test email from ASP.NET 4.6 Connect sample",
ToRecipients = recipientList
};
MessageRequest email = new MessageRequest
{
Message = message,
SaveToSentItems = true
};
try
{
// Act
string response = await graphService.SendEmail(accessToken, email);
}
// Test action will throw an exception when it tries to return resource string.
// So catch the exception when the operation is complete, and then retrieve the email.
catch (Exception e)
{
if (e.HResult == -2147024894)
{
var mailBody = await GetLastEmail(accessToken);
// Assert
Assert.IsTrue(mailBody == testBody, mailBody);
return;
}
else
{
Assert.Fail(e.InnerException.ToString());
}
}
Assert.Fail();
}