private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // google form for Curry


            // outlook

            Microsoft.Office.Interop.Outlook.Application otApp = new Outlook.Application();                // create outlook object
            Outlook.MailItem  otMsg   = (Outlook.MailItem)otApp.CreateItem(Outlook.OlItemType.olMailItem); // Create mail object
            Outlook.Recipient otRecip = (Outlook.Recipient)otMsg.Recipients.Add(EmailUrl);
            otRecip.Resolve();                                                                             // validate recipient address
            otMsg.Subject = "Test Subject";
            otMsg.Body    = "Text Message";
            String sSource = AppDomain.CurrentDomain.BaseDirectory + "Test.txt";
        }
Example #2
0
        private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
        {
            switch (e.Button.Text)
            {
            case "Send":
                Outlook._Application outlookApp = null;
                Outlook._MailItem    newMail    = null;
                Outlook.Recipient    recipient  = null;
                Outlook.Recipients   recipients = null;
                Type olType = Type.GetTypeFromProgID("Outlook.Application", false);
                if (olType != null)
                {
                    try
                    {
                        outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook._Application;
                    }
                    catch { }
                    try
                    {
                        if (outlookApp == null)
                        {
                            outlookApp = Activator.CreateInstance(olType) as Outlook._Application;
                        }
                    }
                    catch { }
                    if (outlookApp != null)
                    {
                        if (tbtnMode.Pushed)
                        {
                            securityManager1.ConnectTo(outlookApp);
                            securityManager1.DisableOOMWarnings = true;
                        }
                        try
                        {
                            newMail = outlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
                            if (newMail != null)
                            {
                                try
                                {
                                    recipients = newMail.Recipients;
                                    if (recipients != null)
                                    {
                                        try
                                        {
                                            if (txbTO.Text != string.Empty)
                                            {
                                                recipient = recipients.Add(txbTO.Text);
                                                if (recipient != null)
                                                {
                                                    try
                                                    {
                                                        recipient.Type = (int)Outlook.OlMailRecipientType.olTo;
                                                        recipient.Resolve();
                                                    }
                                                    finally { Marshal.ReleaseComObject(recipient); }
                                                }
                                            }
                                            if (txbCC.Text != string.Empty)
                                            {
                                                recipient = recipients.Add(txbCC.Text);
                                                if (recipient != null)
                                                {
                                                    try
                                                    {
                                                        recipient.Type = (int)Outlook.OlMailRecipientType.olCC;
                                                        recipient.Resolve();
                                                    }
                                                    finally { Marshal.ReleaseComObject(recipient); }
                                                }
                                            }
                                            if (txbBCC.Text != string.Empty)
                                            {
                                                recipient = recipients.Add(txbBCC.Text);
                                                if (recipient != null)
                                                {
                                                    try
                                                    {
                                                        recipient.Type = (int)Outlook.OlMailRecipientType.olBCC;
                                                        recipient.Resolve();
                                                    }
                                                    finally { Marshal.ReleaseComObject(recipient); }
                                                }
                                            }
                                            newMail.Subject = txbSubject.Text;
                                            newMail.Body    = txbMail.Text;
                                            newMail.Send();
                                        }
                                        finally { Marshal.ReleaseComObject(recipients); }
                                    }

                                    Marshal.ReleaseComObject(newMail);
                                    newMail = null;

                                    MessageBox.Show("The message has been sent successfully.", "Send Mail", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                catch (Exception err)
                                {
                                    if (newMail != null)
                                    {
                                        Marshal.ReleaseComObject(newMail);
                                    }
                                    MessageBox.Show(err.Message, err.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                        }
                        finally
                        {
                            if (tbtnMode.Pushed)
                            {
                                securityManager1.DisableOOMWarnings = false;
                                securityManager1.Disconnect(outlookApp);
                            }
                            if (outlookApp != null)
                            {
                                Marshal.ReleaseComObject(outlookApp);
                            }
                        }
                    }
                }
                break;

            case "Security":
                if (tbtnMode.Pushed)
                {
                    tbtnMode.ImageIndex = 2;
                }
                else
                {
                    tbtnMode.ImageIndex = 1;
                }
                break;
            }
        }