使用wpf发送电子邮件

Jay*_*Jay 4 .net c# wpf

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

 <Grid>
    <Button     Style="{DynamicResource ShowcaseRedBtn}"  CommandParameter="test@ygmail.com" Tag="Send Email" Content="Button" Height="23" HorizontalAlignment="Left" Margin="351,186,0,0" Name="button1" VerticalAlignment="Top" Width="140" Click="button1_Click" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="92,70,0,0" Name="txtSubject" VerticalAlignment="Top" Width="234" />
    <TextBox AcceptsReturn="True" AcceptsTab="True"   Height="159" HorizontalAlignment="Left" Margin="92,121,0,0" Name="txtBody" VerticalAlignment="Top" Width="234" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

在这里的代码背后:

 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

非常感谢您的关注.

干杯

小智 11

您可以使用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() );              
          }
        }
Run Code Online (Sandbox Code Playgroud)

  • 你会以"服务器"的身份传递什么?例如,如果您想通过您的Gmail帐户发送? (4认同)