Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用wpf发送电子邮件_C#_.net_Wpf - Fatal编程技术网

C# 使用wpf发送电子邮件

C# 使用wpf发送电子邮件,c#,.net,wpf,C#,.net,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=

嗨,我试图在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>
我的目的是设置绑定表单数据的电子邮件的参数: // 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() );              
          }
        }

如果您使用的是codebehind,则不需要绑定——尽管它不是很好的模式,但它当然会工作

为什么不把文本框命名为urlTextBox、subjectTextBox等,并在按钮点击事件中使用这些名称呢

        Process.Start(string.Format("mailto:{0}?subject={1}&body={2}", urlTextBox.Text, subjectTextBox.Text, ... ));
当然,如果用户输入无效值,这可能很容易失败


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

< P>我已经发布了一个类似和简单得多的答案< /P> 但是为了强调


进程。从mailto开始:URL将使用新的预填充邮件打开用户邮件客户端。这就是你想要的吗?或者你真的想在按下按钮时发送邮件吗?作为服务器你会传递什么?e、 例如,如果您想通过您的gmail帐户发送?@B.ClayShannon,服务器是webmail.yourwebsite.comMy website?我不想将某个网站与发送电子邮件联系在一起,也不明白为什么这样做是合乎逻辑的……你必须控制发送你所有邮件的服务器——想象一下,如果任何人和每个人都可以将谷歌的电子邮件基础设施用于个人用途。这不是OP想要的:你的方法会从默认的邮件客户端打开一个邮件窗口。OP希望从应用程序内部发送电子邮件,甚至没有邮件客户端。
        Process.Start(string.Format("mailto:{0}?subject={1}&body={2}", urlTextBox.Text, subjectTextBox.Text, ... ));