zoukankan      html  css  js  c++  java
  • 调用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;
            }

        }

  • 相关阅读:
    【bzoj3110】[Zjoi2013]K大数查询 权值线段树套区间线段树
    【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap
    【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流
    【bzoj3527】[Zjoi2014]力 FFT
    【bzoj4259/bzoj4503】残缺的字符串/两个串 FFT
    【bzoj4827】[Hnoi2017]礼物 FFT
    【bzoj2194】快速傅立叶之二 FFT
    【bzoj2179】FFT快速傅立叶 FFT
    【bzoj4327】JSOI2012 玄武密码 AC自动机
    【bzoj3238】[Ahoi2013]差异 后缀数组+单调栈
  • 原文地址:https://www.cnblogs.com/liyiqi12/p/11353658.html
Copyright © 2011-2022 走看看