发送电​​子邮件使用WPF [英] Send email with wpf

查看:139
本文介绍了发送电​​子邮件使用WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想在一个WPF应用程序发送电子邮件,但我会被卡住; 我表明我的XAML code

Hi i am trying to send email in a wpf app but i get stuck; i show my xaml code

 <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>

和这里的背后,code:

and here in the code behind :

 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.邮寄地址=网址;                 // 2。受= txtSubject.Text;                 // 3,身体= txtBody.Text;

my purpose is set the parameters of the email binding the data from the form : // 1. mailto = url; // 2. subject = txtSubject.Text; // 3. body = txtBody.Text;

你有什么想法这一步是如何工作的呢?

Do you have any idea how work out this step?

非常感谢您的关注。

干杯

推荐答案

您可以直接使用System.Net.MailMessage类发送邮件。

You can send mail directly using the System.Net.MailMessage class. Look at the following example from the MSDN documentation for this class:

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() );              
          }
        }

这篇关于发送电​​子邮件使用WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆