added samples
[windows-sources.git] / sdk / samples / WPFSamples / StickyNotes / csharp / email.cs
blobd1bfd263e7a8c4959b269e930c16b8d2411f2193
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ComponentModel;
5 using System.Net;
6 using System.Net.Mail;
7 using System.Windows;
8 using System.Windows.Controls;
9 using System.Windows.Media;
11 namespace StickyNotes
13 class Email
16 public void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
18 // Get the unique identifier for this asynchronous operation.
19 String token = (string)e.UserState;
22 if (e.Error != null)
24 client.SendAsyncCancel();
25 CreateDialog(e.Error.ToString());
26 mailSent = false;
29 else
31 CreateDialog("Message Sent");
32 mailSent = true;
33 dia.Close();
35 message.Dispose();
39 private static void CreateDialog(string msg)
41 Window w = new Window();
42 StackPanel sp = new StackPanel();
43 sp.Background = Brushes.Transparent;
44 TextBlock tb = new TextBlock();
45 tb.Background = sp.Background;
46 tb.TextWrapping = TextWrapping.Wrap;
47 tb.Text = msg;
48 sp.Children.Add(tb);
50 w.Content = sp;
51 w.WindowStyle = WindowStyle.ToolWindow;
52 w.Background = Window2.ChangeBackgroundColor(Colors.Wheat);
53 w.Height = 250;
54 w.Width = 600;
55 w.ShowDialog();
58 public Email(string server, string login, int port, bool sslCheck, string toAddr, string fromAddr, string password, string bodyMessage,EmailDialog emailDia, string subject)
60 dia = emailDia;
61 try
63 NetworkCredential nw = new NetworkCredential(login, password);
64 // Command line argument must be the SMTP host.
65 client = new SmtpClient(server);
66 client.Credentials = nw;
67 client.EnableSsl = (sslCheck == true) ? true : false;
68 // Specify the e-mail sender.
69 // Create a mailing address that includes a UTF8 character
70 // in the display name.
72 MailAddress from = new MailAddress(fromAddr, "", System.Text.Encoding.UTF8);
73 client.Port = port;
74 // Set destinations for the e-mail message.
75 MailAddress to = new MailAddress(toAddr);
76 // Specify the message content.
77 message = new MailMessage(from, to);
78 message.Body = bodyMessage;
80 message.Subject = subject;
81 message.SubjectEncoding = System.Text.Encoding.UTF8;
83 // Set the method that is called back when the send operation ends.
84 client.SendCompleted += new
85 SendCompletedEventHandler(SendCompletedCallback);
86 // The userState can be any object that allows your callback
87 // method to identify this send operation.
88 // For this example, the userToken is a string constant.
89 string userState = "Sticky note message";
90 client.SendAsync(message, userState);
92 catch (Exception e)
94 CreateDialog(e.Message);
98 public bool MailSent
102 return mailSent;
106 private SmtpClient client = null;
107 private MailMessage message = null;
108 private bool mailSent = false;
109 private EmailDialog dia = null;