Reputation: 4453
I have a function to send an e-mail:
public async Task<bool> ComposeEmail(string email, string subject)
{
var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
emailMessage.Body = String.Empty;
var emailRecipient = new Email.EmailRecipient(email);
emailMessage.To.Add(emailRecipient);
emailMessage.Subject = subject;
return EmailManager.ShowComposeNewEmailAsync(emailMessage).status == AsyncStatus.Completed;
}
According to the docs, Started
is not the final result.
How can I wait for the result?
I tried using await
but then ShowComposeNewEmailAsync
is returning void, and I can't get the Status.
I am following this link here: which is using async/await
but with this method there is no way to get the result
Upvotes: 0
Views: 911
Reputation: 4453
Here is what I ended up doing eventually.
public async Task<bool> ComposeEmail(string email, string subject)
{
var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
emailMessage.Body = String.Empty;
var emailRecipient = new Email.EmailRecipient(email);
emailMessage.To.Add(emailRecipient);
emailMessage.Subject = subject;
var x = (EmailManager.ShowComposeNewEmailAsync(emailMessage));
await x;
return x.Status == AsyncStatus.Completed;
}
However, the result here is not whether the User has actually sent the email or not. it seems to be if the program was lunched correctly or not.
I am getting result completed even before sending or cancelling the Email.
Upvotes: 0
Reputation: 39082
The API method's signature is:
public static IAsyncAction ShowComposeNewEmailAsync(EmailMessage message);
As you see, the IAsyncAction
is generic result, that only informs the await
that the action is complete, but does not carry any additional information - in this case does not inform you if the user actually sent the e-mail or not.
You can see this is the case if you compare it with Launcher
's LaunchUriAsync
method for example:
public static IAsyncOperation<bool> LaunchUriAsync(Uri uri);
This one returns a bool
that indicates if the launch was successful or not.
This means the built-in API does not support the functionality you require, it only creates mail message and prepares it for the user to send, but you cannot know if she actually sent it.
As an alternative you could present the user with a confirmation dialog, if he has sent the e-mail message or should the mail compose window be shown again.
var messageDialog = new MessageDialog("No internet connection has been found.");
var messageDialog = new MessageDialog( "Have you successfully sent the e-mail?" );
messageDialog.Commands.Add(new UICommand(
"Yes",
new UICommandInvokedHandler(YesHandler)));
messageDialog.Commands.Add(new UICommand(
"Try again",
new UICommandInvokedHandler(TryAgainHandler)));
await messageDialog.ShowAsync();
As a proof you can use the following snippet:
var action = EmailManager.ShowComposeNewEmailAsync(emailMessage);
await action;
Debug.WriteLine(action.Status);
The Status
is always Completed
once the mail app launches.
But it is true it is quite unfortunate a bool
result of the action is not available. I think the reason is that any app can respond to the request, including older Win32 apps.
Upvotes: 1