2011-01-21 4 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. 흔한 = url; // 2. subject = txtSubject.Text; // 3. body = txtBody.Text;

이 단계를 수행하는 방법에 대해 알고 있습니까?

감사합니다.

건배

+0

Process.Start를()에 대한 URL이 열립니다 사용자 메일 클라이언트에 새로운 미리 채워진 메시지가 있습니다. 너가 원하는게 그거야 ? 또는 버튼을 눌렀을 때 실제로 메일을 보내시겠습니까? – JYL

답변

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 계정을 통해 메일을 보내려는 경우 –

+0

@ B.ClayShannon, Server is webmail.yourwebsite.com –

+1

내 웹 사이트? 나는 특정 웹 사이트를 묶어 전자 메일을 보내고 싶지 않으며 왜 그 일이 논리적 인 일인지 이해하지 못한다. –

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>

+0

이것은 OP가 원하는 것이 아니다. 귀하의 접근 방식은 기본값에서 메일 창을 엽니 다. 메일 클라이언트. OP는 메일 클라이언트가 없어도 애플리케이션 내에서 이메일을 보내려고합니다. – Terry