在WP7应用中发生未处理的异常时,有没有办法发送包含详细信息的电子邮件?
我见过WCF日志记录/电子邮件发送方法,但我没有公开托管服务的地方。
答案 0 :(得分:6)
看看Andy Pennell的“little watson”。它运作得很好。
答案 1 :(得分:1)
您唯一的另一个选择是使用EmailComposeTask。这使得用户可以发送邮件,因为它会为您提供电子邮件地址,但它是目前在没有WCF服务的情况下发送邮件的唯一方式。
示例1:
private void emailAddressChooserTask_Completed(object sender, EmailResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show("Selected email :" + e.Email);
//in-real world application user expect to select it from his contacts and if not found enter manually.
//EmailComposeTask emailComposeTask = new EmailComposeTask();
//emailComposeTask.To = e.Email;
//emailComposeTask.To = saveEmailAddressTask.Email;
//emailComposeTask.Body = "WP7 Emails Demo";
//emailComposeTask.Cc = "testmail2@test.com";
//emailComposeTask.Subject = "Windows Phone 7";
//emailComposeTask.Show();
}
}
示例2:
private void composeMail_Click(object sender, RoutedEventArgs e)
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.To = "chris@example.com";
emailComposeTask.To = saveEmailAddressTask.Email;
emailComposeTask.Body = "WP7 Emails Demo";
emailComposeTask.Cc = "testmail2@test.com";
emailComposeTask.Subject = "Windows Phone 7";
emailComposeTask.Show();
}
Souce:http://www.windowsphonegeek.com/articles/1-how-to-perform-email-tasks-in-a-wp7-app
答案 2 :(得分:1)
我在我的应用程序中使用以下内容,它有点令人费解,但有效。以下代码位于App.xaml.cs中:
/// <summary>
/// Indicates whether the application needs to quit due to a previous exception
/// </summary>
private static bool _appMustQuit = false;
/// <summary>
/// Exception class that will be unhandled causing application to quit
/// </summary>
private class AppQuitException : Exception {}
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException( object sender,
ApplicationUnhandledExceptionEventArgs e )
{
if( ( e.ExceptionObject is AppQuitException ) == false ) {
Debug.WriteLine( "App:Application_UnhandledException - " + e.ToString() );
if( Debugger.IsAttached ) {
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
// Compose error report
StringBuilder report = new StringBuilder( 1024 );
report.AppendFormat( "{0}", LangResources.ErrorReportContent );
report.AppendFormat( "Message: {0}\n", e.ExceptionObject.Message );
if( e.ExceptionObject.InnerException != null ) {
report.AppendFormat( "Inner: {0}\n", e.ExceptionObject.InnerException.Message );
}
report.AppendFormat( "\nStackTrace: {0}\n", e.ExceptionObject.StackTrace );
if( MessageBox.Show( "Unexpected Error", "Error", MessageBoxButton.OKCancel )
== MessageBoxResult.OK ) {
e.Handled = true;
// Email the error report
Tasks.ComposeEmail( "\"Developer\" <your@emailaddress.com>", "MyApp Error Report",
report.ToString() );
_appMustQuit = true;
}
}
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated( object sender, ActivatedEventArgs e )
{
var state = PhoneApplicationService.Current.State;
if( state.ContainsKey( "AppMustQuit" ) ) {
throw new AppQuitException();
} else {
// Restore other tombstoned variables
}
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated( object sender, DeactivatedEventArgs e )
{
if( _appMustQuit ) {
state["AppMustQuit"] = true;
} else {
// Save other variables for tombstoning
}
}
Tasks
是一个静态类,带有Microsoft.Phone.Tasks
命名空间中的一堆辅助函数。
using Microsoft.Phone.Tasks;
namespace MyApp
{
/// <summary>
/// Utility class for performing various phone tasks
/// </summary>
public static class Tasks
{
/// <summary>
/// Composes an email using the specified arguments
/// </summary>
/// <param name="to">The recepient(s) of the email</param>
/// <param name="subject">Email subject</param>
/// <param name="body">Email contents</param>
/// <param name="cc">The recipient(s) on the cc line of the email</param>
public static void ComposeEmail( string to, string subject, string body,
string cc = "" )
{
var task = new EmailComposeTask() {
To = to,
Subject = subject,
Body = body,
Cc = cc,
};
task.Show();
}
}
}
要稍微解释一下代码,使用EmailComposeTask
会导致您的应用被逻辑删除。由于我不想在未处理的异常后继续执行应用程序,因此我将一个布尔值保存到PhoneApplicationService
的{{3}}字典中,以便在用户发送电子邮件后,当应用程序重新唤醒时,我可以查找此布尔值并抛出另一个故意未处理的异常。第二个例外导致应用程序退出。
答案 3 :(得分:0)
如果有人在后台寻找电子邮件(不加载EmailComposeTask),那么MailMessage可能是最佳选择,因此,它还允许EmailComposeTask根本不支持的附件。它允许您使用自己的Gmail或任何其他帐户发送电子邮件,而无需在后台打扰用户。
你可以从NuGet Install-Package MailMessage
获得它试一试,但是我自己还没有使用它,所以你可能需要从authors site购买它只需29.99美元值得为此付出代价。免费版也可以使用,但在发送电子邮件之前它会在应用程序中显示一条弹出消息。