Send email with wpf

后端 未结 3 1656
迷失自我
迷失自我 2021-02-10 14:38

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

 
    
相关标签:
3条回答
  • 2021-02-10 15:28

    If you are using codebehind, you don't need binding - although it's not very good pattern, it will of course work.

    Why don't you just name your textboxes (urlTextBox, subjectTextBox, etc.) and use these names in the button click event?

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

    Of course this might easily fail, if user inputs invalid values.

    Using bindings is another way to go, but in this simple case I consider it overhead.

    0 讨论(0)
  • 2021-02-10 15:33

    I have posted a similar and much simpler answer here

    but for the emphasis

    <Button Content="Send Email" HorizontalAlignment="Left" VerticalAlignment="Top" Height="50">
      <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
          <ei:LaunchUriOrFileAction Path="mailto:example@stackoverflow.com?subject=SubjectExample" />
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </Button>

    0 讨论(0)
  • 2021-02-10 15:36

    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() );              
              }
            }
    
    0 讨论(0)
提交回复
热议问题