调用foxmail发送邮件

因为foxmail没有api调用接口,工作中又需要使用foxmail发送邮件(主要是附件)。在网上没有发现这方面的程序。特写一下自己写的一个示例

示例如下:

  需要添加 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
            GetClassName(htmlEditor, sb, 256);

            //密送(下一个)
            IntPtr bccEditer = GetWindow(htmlEditor, 2);
            GetClassName(bccEditer, sb, 256);
            SendMessage(bccEditer, WM_SETTEXT, IntPtr.Zero, bcc);

            //抄送(下一个)
            IntPtr ccEditer = GetWindow(bccEditer, 2);
            GetClassName(ccEditer, sb, 256);
            SendMessage(ccEditer, WM_SETTEXT, IntPtr.Zero,cc);

            //收件人(下一个)
            IntPtr toEditer = GetWindow(ccEditer, 2);
            GetClassName(toEditer, sb, 256);
            // GetWindowText(toIntPtr, sbtext, 256);
            SendMessage(toEditer, WM_SETTEXT, IntPtr.Zero, to);


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


            return isOk;
        }

    }

posted on 2019-08-14 17:52  liyiqi12  阅读(2695)  评论(1编辑  收藏  举报