I finally found a solution. to this issue.
Instead of using Window.Navigate, use javascript location.href.
private void TestLink_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.IsRunningOutOfBrowser)
return;
var cmd = String.Format("location.href='mailto:test@test.com?subject=blah&body=something';",
HtmlPage.Window.Eval(cmd);
}
Note: This wont work OOB as HtmlPage is not available, however the default behaviour for the mailto works as it should so in your constructor only bind the NavigateUri if OOB.
In my project I created a control to wrap all this up so I could bind the email, subject and body in xaml.
Here it is..
public class MailToButton : HyperlinkButton
{
public static readonly DependencyProperty EmailProperty = DependencyProperty.Register("Email",
typeof(string),
typeof(
MailToButton
), null);
public object Email
{
get { return GetValue(EmailProperty); }
set { SetValue(EmailProperty, Convert.ToString(value)); }
}
public static readonly DependencyProperty SubjectProperty = DependencyProperty.Register("Subject",
typeof(string),
typeof(
MailToButton
), null);
public object Subject
{
get { return GetValue(SubjectProperty); }
set { SetValue(SubjectProperty, Convert.ToString(value)); }
}
public static readonly DependencyProperty BodyProperty = DependencyProperty.Register("Body",
typeof(string),
typeof(
MailToButton
), null);
public new Uri NavigateUri { get; set; }
public object Body
{
get { return GetValue(BodyProperty); }
set { SetValue(BodyProperty, Convert.ToString(value)); }
}
public MailToButton()
{
Loaded += MailToButton_Loaded;
Click += MailToButtonClicked;
}
private void MailToButton_Loaded(object sender, RoutedEventArgs e)
{
if (Application.Current.IsRunningOutOfBrowser)
base.NavigateUri = new Uri(BuildMailToString());
}
private void MailToButtonClicked(object sender, RoutedEventArgs e)
{
if (Application.Current.IsRunningOutOfBrowser)
return;
if (string.IsNullOrEmpty(Convert.ToString(Email)))
return;
HtmlPage.Window.Eval(BuildMailToString());
}
private string BuildMailToString()
{
var email = HttpUtility.UrlEncode(Convert.ToString(Email));
var subject = HttpUtility.UrlEncode(Convert.ToString(Subject));
var body = HttpUtility.UrlEncode(Convert.ToString(Body));
var sb = new StringBuilder();
sb.AppendFormat("location.href='mailto:{0}?", email);
if (!string.IsNullOrEmpty(subject))
sb.AppendFormat("subject={0}", subject);
if (!string.IsNullOrEmpty(body))
{
if (!string.IsNullOrEmpty(subject))
sb.Append("&");
sb.AppendFormat("body={0}", body);
}
sb.Append("'");
return sb.ToString();
}
}