使用wpf发送电子邮件

嗨,我想在一个wpf应用程序发送电子邮件,但我卡住了; 我展示了我的xaml代码

 

在这里的代码背后:

  private void button1_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; if (btn == null) return; string url = btn.CommandParameter as string; if (String.IsNullOrEmpty(url)) return; try { // here i wish set the parameters of email in this way // 1. mailto = url; // 2. subject = txtSubject.Text; // 3. body = txtBody.Text; Process.Start("mailto:test@gmail.com?subject=Software&body=test "); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } 

我的目的是设置电子邮件的参数绑定来自表单的数据:// 1. mailto = url; // 2. subject = txtSubject.Text; // 3. body = txtBody.Text;

你知道如何解决这一步吗?

非常感谢你的关注。

干杯

您可以使用System.Net.MailMessage类直接发送邮件。 请查看此类的MSDN文档中的以下示例:

 public static void CreateTimeoutTestMessage(string server) { string to = "jane@contoso.com"; string from = "ben@contoso.com"; string subject = "Using the new SMTP client."; string body = @"Using this new feature, you can send an e-mail message from an application very easily."; MailMessage message = new MailMessage(from, to, subject, body); SmtpClient client = new SmtpClient(server); Console.WriteLine("Changing time out from {0} to 100.", client.Timeout); client.Timeout = 100; // Credentials are necessary if the server requires the client // to authenticate before it will send e-mail on the client's behalf. client.Credentials = CredentialCache.DefaultNetworkCredentials; try { client.Send(message); } catch (Exception ex) { Console.WriteLine("Exception caught in CreateTimeoutTestMessage(): {0}", ex.ToString() ); } } 

如果你正在使用代码隐藏,你不需要绑定 – 虽然它不是很好的模式,但它当然会起作用。

为什么不直接命名文本框(urlTextBox,subjectTextBox等)并在按钮单击事件中使用这些名称?

  Process.Start(string.Format("mailto:{0}?subject={1}&body={2}", urlTextBox.Text, subjectTextBox.Text, ... )); 

当然,如果用户输入无效值,这可能很容易失败。

使用绑定是另一种方法,但在这个简单的情况下,我认为它是开销。

我在这里发布了一个类似且更简单的答案

但强调