protected static void SendMail(string body, string subject, string attachmentFile) { if (attachmentFile != null) { IntPtr pidl = IntPtr.Zero; try { pidl = NativeMethods.ILCreateFromPath(attachmentFile); if (pidl != IntPtr.Zero) { NativeMethods.SHOpenFolderAndSelectItems(pidl, 0, IntPtr.Zero, 0); } } finally { if (pidl != IntPtr.Zero) { NativeMethods.ILFree(pidl); } } } Application outlookApp = null; try { outlookApp = new Application(); } catch (System.Exception ex) { GeneralLog.Write("Unable to start Outlook (exception data follows)"); GeneralLog.Write(ex); } if (outlookApp == null) { var fallbackWindow = new SendMailFallbackWindow { MessageBody = body }; fallbackWindow.Show(); fallbackWindow.Activate(); ProcessStartInfo psi = new ProcessStartInfo(); psi.UseShellExecute = true; psi.FileName = string.Format( CultureInfo.InvariantCulture, "mailto:[email protected]?subject={0}&body={1}", Uri.EscapeDataString(subject), Uri.EscapeDataString(body)); Process.Start(psi); } else { try { MailItem mail = outlookApp.CreateItem(OlItemType.olMailItem) as MailItem; mail.Subject = subject; mail.Body = body; mail.To = "*****@*****.**"; mail.Display(Modal: false); } catch (System.Exception ex) { GeneralLog.Write("Error composing Outlook e-mail (exception data follows)"); GeneralLog.Write(ex); } } }
protected void SendMail(string body, string subject, string attachmentFile) { if (attachmentFile != null) { IntPtr pidl = IntPtr.Zero; try { pidl = NativeMethods.ILCreateFromPath(attachmentFile); if (pidl != IntPtr.Zero) { NativeMethods.SHOpenFolderAndSelectItems(pidl, 0, IntPtr.Zero, 0); } } finally { if (pidl != IntPtr.Zero) { NativeMethods.ILFree(pidl); } } } Application outlookApp = null; try { outlookApp = new Application(); } catch (System.Exception ex) { Services.Log.WriteAsync(LogVerbosity.Normal, MessageCategory.Error, "Unable to start Outlook: " + ex.Message).DoNotWait(); } if (outlookApp == null) { var fallbackWindow = new SendMailFallbackWindow { MessageBody = body }; fallbackWindow.Show(); fallbackWindow.Activate(); ProcessStartInfo psi = new ProcessStartInfo(); psi.UseShellExecute = true; psi.FileName = string.Format( CultureInfo.InvariantCulture, "mailto:[email protected]?subject={0}&body={1}", Uri.EscapeDataString(subject), Uri.EscapeDataString(body)); Services.ProcessServices.Start(psi); } else { try { MailItem mail = outlookApp.CreateItem(OlItemType.olMailItem) as MailItem; mail.Subject = subject; mail.Body = body; mail.To = "*****@*****.**"; mail.Display(Modal: false); } catch (System.Exception ex) { Services.Log.WriteAsync(LogVerbosity.Normal, MessageCategory.Error, "Error composing Outlook e-mail: " + ex.Message).DoNotWait(); } } }