C# 编程HyperlinkColumn以发送电子邮件WPF
我正在尝试编写一个C# 编程HyperlinkColumn以发送电子邮件WPF,c#,wpf,email,outlook,C#,Wpf,Email,Outlook,我正在尝试编写一个DataGridHyperlinkColumn,其中包含用户的电子邮件,单击该地址时可通过outlook发送新电子邮件。现在,我只是使用一封测试电子邮件,而不是获取专栏的内容,但这是我目前所拥有的 <DataGridHyperlinkColumn Header="Email" Binding="{Binding Email}"> <DataGridHyperlinkColumn.ElementStyle> <Style>
DataGridHyperlinkColumn
,其中包含用户的电子邮件,单击该地址时可通过outlook发送新电子邮件。现在,我只是使用一封测试电子邮件,而不是获取专栏的内容,但这是我目前所拥有的
<DataGridHyperlinkColumn Header="Email" Binding="{Binding Email}">
<DataGridHyperlinkColumn.ElementStyle>
<Style>
<EventSetter Event="Hyperlink.Click" Handler="OnEmailHyperlinkClick"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
目前,这提供了奇怪的行为。首先,它打开了一个新的谷歌浏览器实例。与Outlook完全无关。然后说,
找不到资源的通讯簿/someone@test.com"
这几乎就好像这个事件实际上是在其他地方处理的,但我敢肯定它不是。以前有人经历过这种情况吗?您的解决方案所做的只是告诉windows尝试使用提供的字符串启动新进程 您需要指定希望它使用outlook 将Microsoft.Office.Interop.Outlook的
引用添加到项目中(确保其版本正确)
然后试着这样做:
public static void OnEmailHyperlinkClick(object sender, RoutedEventArgs e)
{
try
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem msg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
msg.Subject = "My subject";
msg.Body = "My Message Body";
Recipients recipients = (Recipients)msg.Recipients;
Recipient recipient = (Recipient)recipients.Add("someone@test.com");
recipient.Resolve();
msg.Display(); // If you want to have the email displayed for the user to send
// Otherwise
msg.Send();
recipient = null;
recipients = null;
msg = null;
oApp = null;
}
catch (Exception ex)
{
}
}
您的解决方案所做的只是告诉windows尝试使用提供的字符串启动新进程
您需要指定希望它使用outlook
将Microsoft.Office.Interop.Outlook的引用添加到项目中(确保其版本正确)
然后试着这样做:
public static void OnEmailHyperlinkClick(object sender, RoutedEventArgs e)
{
try
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
MailItem msg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
msg.Subject = "My subject";
msg.Body = "My Message Body";
Recipients recipients = (Recipients)msg.Recipients;
Recipient recipient = (Recipient)recipients.Add("someone@test.com");
recipient.Resolve();
msg.Display(); // If you want to have the email displayed for the user to send
// Otherwise
msg.Send();
recipient = null;
recipients = null;
msg = null;
oApp = null;
}
catch (Exception ex)
{
}
}
谢谢你的回答。现在,它确实在Outlook
中打开了一封电子邮件,而不是在Chrome中打开了一个选项卡,但是我仍然收到错误无法找到资源的地址簿/someone@test.com“
。有什么建议吗?是someone@test.com
是否在outlook通讯簿中?试着用一个真实的电子邮件地址代替。谢谢你的回答。现在,它确实在Outlook
中打开了一封电子邮件,而不是在Chrome中打开了一个选项卡,但是我仍然收到错误无法找到资源的地址簿/someone@test.com“
。有什么建议吗?是someone@test.com
是否在outlook通讯簿中?试着用一个真实的电子邮件地址代替它。