Send e-mail to call foxmail

Because there is no foxmail api call interface, work and need to send mail using foxmail (mainly accessories). Online found no procedures in this regard. Close-up look at an example to write their own

Examples are as follows:

  We need to add using System.Runtime.InteropServices;

    public class FoxmailHelper
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "GetWindow")]
        private extern static IntPtr GetWindow(IntPtr hWnd, uint nCmd);

        [DllImport("user32.dll")]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);


        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);

        const int WM_SETTEXT = 0x0C;

        /// <summary>
        ///
        /// </summary>
        /// <param name="to">收件人</param>
        /// <param name="cc">抄送</param>
        /// <param name="bcc">密送</param>
        /// <param name="subject">主题</param>
        /// <param name="listAttrach">附件</param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool Send(string to, string cc, string bcc, string subject, List<string> listAttrach, string content = "")
        {
           System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();

            string foxMailFilePath ="";

            //找到foxmail的文件路径
            foreach (System.Diagnostics.Process p in processes)
            {
                if (p.ProcessName.Equals("Foxmail", StringComparison.OrdinalIgnoreCase))
                {
                    foxMailFilePath = p.MainModule.FileName;
                    break;
                }
            }
            if (string.IsNullOrEmpty(foxMailFilePath))
            {
                throw new Exception("FoxMail未启动或未安装!");
            }
          
            string attach = "";
            string subjectText = "";

            if (listAttrach != null)
            {
                listAttrach.ForEach(s =>
                {
                    attach += $"{s} ";
                    subjectText += $"{System.IO.Path.GetFileNameWithoutExtension(s)},";
                });
                attach = attach.Trim(' ');
                subjectText = subjectText.Trim(',');
            }

            ProcessStartInfo processStartInfo = new ProcessStartInfo(foxMailFilePath)
            {
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true
            };
            if (!string.IsNullOrEmpty(attach))
            {
                processStartInfo.Arguments = attach;
            }
            else
            {
                processStartInfo.Arguments = $"mailto:{to}?cc={cc}&bcc={bcc}&subject={subject}&body={content}";
            }
            bool isOk = false;
            Process process = new Process() { StartInfo = processStartInfo };
            isOk = process.Start();
            System.Threading.Thread.Sleep(1000);


            StringBuilder sb = new StringBuilder();
            StringBuilder sbtext = new StringBuilder();

            IntPtr foxForm = FindWindow(null, $"{subjectText} - 写邮件");         //主窗体 TFoxComposeForm.UnicodeClass
            GetClassName(foxForm, sb, 256);
 
            IntPtr foxFrame = GetWindow(foxForm, 5);               //TFoxComposeFrame.UnicodeClass
            GetClassName(foxFrame, sb, 256);
 
            IntPtr layoutManager = GetWindow(foxFrame, 5);               //TLayoutManager
            GetClassName(layoutManager, sb, 256);
   
            //取得第一个子控件
            IntPtr htmlEditor = GetWindow(layoutManager, 5);                       // TFoxHTMLEditor
            The GetClassName (HTMLEditor, SB, 256);

            // Bcc (next)
            IntPtr = bccEditer the GetWindow (HTMLEditor, 2);
            the GetClassName (bccEditer, SB, 256);
            the SendMessage (bccEditer, the WM_SETTEXT, IntPtr.Zero, BCC);

            / / cc (next)
            IntPtr = ccEditer the GetWindow (bccEditer, 2);
            the GetClassName (ccEditer, SB, 256);
            the SendMessage (ccEditer, the WM_SETTEXT, IntPtr.Zero, CC);

            // To (next)
            IntPtr toEditer GetWindow = (ccEditer, 2);
            GetClassName (toEditer, SB, 256);
            // the GetWindowText (toIntPtr, sbtext, 256);
            SendMessage (toEditer, WM_SETTEXT, IntPtr.Zero, to);


            // theme
            IntPtr subjectEditer = FindWindowEx(layoutManager, IntPtr.Zero, "TFMEdit.UnicodeClass", null);
            GetClassName(subjectEditer, sb, 256);
            SendMessage(subjectEditer, WM_SETTEXT, IntPtr.Zero, subject);


            return isOk;
        }

    }

Guess you like

Origin www.cnblogs.com/liyiqi12/p/11353658.html