JayJay
JayJay

Reputation: 1068

Send email with wpf

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

 <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>

and here in the code behind :

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

my purpose is set the parameters of the email binding the data from the form : // 1. mailto = url; // 2. subject = txtSubject.Text; // 3. body = txtBody.Text;

Do you have any idea how work out this step?

Thanks so much for your attention.

Cheers

Upvotes: 7

Views: 35998

Answers (4)

spacemonki
spacemonki

Reputation: 303

Just adjusting Tims answer for Google accounts as some people were inquiring

public static void CreateTimeoutTestMessage()
        {
            string to = "to address";
            string from = "from address";
            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);

            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("@gmail.com", "secretPassword"),
                EnableSsl = true
            };

            try
            {
                client.Send(message);
                Debug.WriteLine("Sent");
            }
            catch
            {
                throw;
            }


Upvotes: 1

OliverAssad
OliverAssad

Reputation: 1070

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:[email protected]?subject=SubjectExample" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

Upvotes: 4

Pz.
Pz.

Reputation: 1179

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.

Upvotes: 2

Tim Myers
Tim Myers

Reputation: 279

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

Upvotes: 14

Related Questions