2011-01-21 75 views
2

嗨,我想發送電子郵件在wpf應用程序,但我卡住了; 我展示我的XAML代碼發送電子郵件與wpf

<Grid> 
    <Button  Style="{DynamicResource ShowcaseRedBtn}" CommandParameter="[email protected]" 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> 

這裏在後面的代碼:

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:[email protected]?subject=Software&body=test "); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); 
     } 
    } 

我的目的是設置電子郵件結合從表單中的數據的參數: // 1.郵寄地址=網址; // 2. subject = txtSubject.Text; // 3. body = txtBody.Text;

你知道如何解決這一步嗎?

非常感謝您的關注。

乾杯

+0

一個的Process.Start()與 「電子郵件地址:」 URL將打開用戶郵件客戶端使用新的預填充消息。那是你要的嗎 ?或者當你按下按鈕時你真的想發送郵件嗎? – JYL 2011-01-21 10:58:14

回答

9

您可以使用System.Net.MailMessage類直接發送郵件。請看下面的例子來自MSDN文檔該類:

public static void CreateTimeoutTestMessage(string server) 
     { 
      string to = "[email protected]"; 
      string from = "[email protected]"; 
      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());    
      } 
     } 
+4

你會作爲「服務器」傳遞什麼?例如,如果你想通過你的Gmail賬戶發送? – 2012-07-11 03:00:07

+0

@ B.ClayShannon,服務器是webmail.yourwebsite.com – 2016-04-04 01:03:16

2

如果使用代碼隱藏,你不需要綁定 - 雖然它不是很好的模式,它當然會的工作。

爲什麼你不只是命名你的文本框(urlTextBox,subjectTextBox等),並在按鈕單擊事件中使用這些名稱?

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

當然這可能很容易失敗,如果用戶輸入無效值。

使用綁定是另一種方式,但在這種簡單情況下,我認爲它是開銷。

0

我已經發布了類似的和更簡單的答案here

,但對於注重

<Button Content="Send Email" HorizontalAlignment="Left" VerticalAlignment="Top" Height="50"> 
 
    <i:Interaction.Triggers> 
 
    <i:EventTrigger EventName="Click"> 
 
     <ei:LaunchUriOrFileAction Path="mailto:[email protected]?subject=SubjectExample" /> 
 
    </i:EventTrigger> 
 
    </i:Interaction.Triggers> 
 
</Button>