Thursday, March 5, 2015

How to open default mail client in WPF applications ?

In Your WPF application, You want to open default mail client application when a button is pressed.
your can reference code behind

Option 1: only work on windows 7
Refer a friend

 private void ttbOpenMail_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
              
               OpenEmailClientByShellExecute();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

[DllImport("shell32.dll")]
        public static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation,
            string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

        private void OpenEmailClientByShellExecute()
        {
            ShellExecute(IntPtr.Zero, "open", "mailto:username?subject=Read%20This&body=message%20contents", "", "", 4/* sw_shownoactivate */);
        }


Option 2: using Microsoft.Office.Interop
  Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();

                Microsoft.Office.Interop.Outlook.MailItem message =

                (Microsoft.Office.Interop.Outlook.MailItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

                message.Subject = "Title Mail!";

                message.Recipients.Add(address);

                message.Body = "Hello. This is body area, write your mail here.";

                //int attachmentLocation = 1;

                //message.Attachments.Add(fileName, Outlook.OlAttachmentType.olByValue,attachmentLocation, displayName);

                message.Display(true);

No comments: