加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用wpf发送电子邮件

发布时间:2020-12-16 00:15:29 所属栏目:百科 来源:网络整理
导读:嗨,我想在一个wpf应用程序发送电子邮件,但我卡住了; 我显示我的xaml代码 Grid Button Style="{DynamicResource ShowcaseRedBtn}" CommandParameter="test@ygmail.com" Tag="Send Email" Content="Button" Height="23" HorizontalAlignment="Left" Margin="35
嗨,我想在一个wpf应用程序发送电子邮件,但我卡住了;
我显示我的xaml代码

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

在这里的代码背后:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读