zoukankan      html  css  js  c++  java
  • VC++ 使用ShellExecute函数调用邮箱客户端发送邮件(可以带附件)

     

    之前写过一篇博文,通过MAPI实现调用邮箱客户端发送邮件带附件,当时对ShellExecute研究不深,以为ShellExecute不能带附件,因为项目需求原因(MAPI只能调用Foxmail和Outlook邮箱客户端,无法调用网易邮箱大师),不得不回头再次研究ShellExecute函数,最后发现ShellExecute可以完美调用Foxmail、网易邮箱大师、Outlook(单次发送只能添加一个附件)。因为我们安装完Foxmail、网易大师后,系统右键里发送到里有他们的快捷方式,所以就是在这里入手的,它们的快捷方式存放在系统路径下 C:UsersAdministratorAppDataRoamingMicrosoftWindowsSendTo (这是我的电脑上的位置,具体根据当前系统登录用户目录下找)

    Foxmail:

    int SharedByFoxmail()
    {
    TCHAR szPath[MAX_PATH] = { 0 }; SHGetSpecialFolderPath(NULL, szPath, CSIDL_SENDTO, FALSE); CString strClient; strClient.Format(L"%ls", szPath); strClient += L"\Foxmail.lnk";//拿到快捷方式的绝对路径 CString strRecv = “example@163.com”;//收件人 CString FilePath = ""C:\Users\Administrator\Desktop\123.txt" "C:\Users\Administrator\Desktop\image.png"";//多个附件发送时 路径要用双引号括起来,路径之间用空格隔开 ShellExecute(NULL, L"open", strClient,FilePath, L"", SW_SHOW); //填写收件人 HWND hWnd = NULL; int n = 60; do { Sleep(50); hWnd = ::FindWindow(L"TFoxComposeForm.UnicodeClass", NULL); n--; } while (n && (hWnd == NULL || !::IsWindowVisible(hWnd))); if (hWnd == NULL) { return -1; } HWND hChildWnd1 = NULL; HWND hChildWnd2 = NULL; HWND hChildWnd31 = NULL; HWND hChildWnd32 = NULL; HWND hChildWnd33 = NULL; if (hWnd != NULL) { ::SetForegroundWindow(hWnd); hChildWnd1 = ::FindWindowEx(hWnd, 0, L"TFoxComposeFrame.UnicodeClass", NULL); hChildWnd2 = ::FindWindowEx(hChildWnd1, 0, L"TLayoutManager", NULL); hChildWnd31 = ::FindWindowEx(hChildWnd2, 0, L"TFMZRichEdit.UnicodeClass", NULL); //抄送 hChildWnd32 = ::FindWindowEx(hChildWnd2, hChildWnd31, L"TFMZRichEdit.UnicodeClass", NULL); if (hChildWnd32 != NULL) { DWORD SelfThreadId = GetCurrentThreadId();//获取本身的线程ID DWORD ForeThreadId = GetWindowThreadProcessId(hChildWnd32, NULL);//根据窗口句柄获取线程ID AttachThreadInput(ForeThreadId, SelfThreadId, true);//附加线程 // WCHAR* szTest = L"comor_86sssss163.com"; //::SendMessage(hChildWnd32, WM_SETTEXT, 0, (LPARAM)strCC.GetBuffer(0)); //strCC.ReleaseBuffer(); } //收件人 hChildWnd33 = ::FindWindowEx(hChildWnd2, hChildWnd32, L"TFMZRichEdit.UnicodeClass", NULL); if (hChildWnd33 != NULL) { DWORD SelfThreadId = GetCurrentThreadId();//获取本身的线程ID DWORD ForeThreadId = GetWindowThreadProcessId(hChildWnd33, NULL);//根据窗口句柄获取线程ID AttachThreadInput(ForeThreadId, SelfThreadId, true);//附加线程 // WCHAR* szTest = L"comor_86@163.com"; ::SendMessage(hChildWnd33, WM_SETTEXT, 0, (LPARAM)strRecv.GetBuffer(0)); strRecv.ReleaseBuffer(); } }
    return 0;
    }

     网易邮箱大师:

    int SharedByNetease()
    {
    TCHAR szPath[MAX_PATH] = { 0 }; SHGetSpecialFolderPath(NULL, szPath, CSIDL_SENDTO, FALSE); CString strClient; strClient.Format(L"%ls", szPath); strClient += L"\Foxmail.lnk";//拿到快捷方式的绝对路径 CString strRecv = “example@163.com”;//收件人 CString FilePath = ""C:\Users\Administrator\Desktop\123.txt" "C:\Users\Administrator\Desktop\image.png"";//多个附件发送时 路径要用双引号括起来,路径之间用空格隔开 ShellExecute(NULL, L"open", strClient,FilePath, L"", SW_SHOW); //填写收件人 HWND hWnd = NULL; int n = 60; do { Sleep(50); hWnd = ::FindWindow(L"MailWriteWindow", NULL); n--; } while (n && (hWnd == NULL || !::IsWindowVisible(hWnd))); if (hWnd == NULL) { return -1; } ::SetForegroundWindow(hWnd); hWnd = NULL; n = 60; do { Sleep(50); hWnd = ::FindWindow(L"MailWriteWindow", NULL); n--; } while (hWnd == NULL && n); if (hWnd == NULL) { return -1; } ::SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(150, 65));//模拟鼠标左键按下 Sleep(1000); SendKeys(strRecv); return 0;
    } ....................... /////////////////////////////////////////////////
    void SendKeys(CString msg) { USES_CONVERSION; wchar_t* data = T2W(msg.GetBuffer(0)); int len = wcslen(data); for (int i = 0; i<len; i++) { SendUnicode(data[i]); } }

    OutLook:

    int SharedByOutlook()
    {
        CString strFile=     ""C:\Users\Administrator\Desktop\123.txt""
        CString strRecv= “example@163.com”;//收件人
    
        CString strPara;
        strPara = "/a "";
        strPara += strFile;
        strPara +=""";//附件路径用双引号括起来,方式文件名中有空格造成调用失败
    
        //获取outlook安装路径
        CString strPath = ReadOutlookPath();
        if (!strPath.IsEmpty())
        {
            //添加判断文件是否存在
            if (PathFileExists(strPath + "outlook.exe"))
            {
                ShellExecute(NULL,L"open",L"outlook.exe",strPara,strPath,SW_SHOW);
            }
        }
    
        HWND hWnd = NULL;
        int n = 60;
        do
        {
            Sleep(50);
            hWnd = ::FindWindow(L"rctrl_renwnd32", NULL);
            n--;
        } while (n && (hWnd == NULL || !::IsWindowVisible(hWnd)));
    
        if (hWnd == NULL)
        {
            return -1;
        }
    
        ::SetForegroundWindow(hWnd);
    
        TCHAR* szBuffer = strRecv.GetBuffer(0);
        EnumChildWindows(hWnd, EnumChildWindowsProc, (LPARAM)szBuffer);
        strRecv.ReleaseBuffer();
    
        return 0;
    }
    
    
    ///////////////////////////////////////
    CString CSharedHelper::ReadOutlookPath()
    {
    HKEY hKEY;
    HKEY hKeyRoot = HKEY_LOCAL_MACHINE;
    long ret = ::RegOpenKeyEx(hKeyRoot,
    L"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE", 
    0, 
    KEY_READ, 
    &hKEY);
    if (ret != ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
    {
    //    AfxMessageBox(L"错误:无法打开有关的hKEY");
    return L"";
    }
    
    
    TCHAR Vals[MAX_PATH] = { 0 };
    DWORD keyType;
    DWORD lenIt = MAX_PATH*sizeof(TCHAR);
    if (::RegQueryValueEx(hKEY, L"Path", 0, &keyType, (BYTE*)Vals, &lenIt) != ERROR_SUCCESS)
    {
    //    AfxMessageBox(L"错误:RegQueryValueExA错误");
    return L"";
    }
    
    CString str;
    str.Format(L"%ls", Vals);
    return str;
    }
    
    BOOL CALLBACK CSharedHelper::EnumChildWindowsProc(HWND hWnd, LPARAM lParam)
    {
    HWND EditNumHwnd = ::FindWindowEx(hWnd, NULL, _T("RichEdit20WPT"), NULL);
    if (EditNumHwnd != NULL)
    {
    if (::GetWindowLong(EditNumHwnd, GWL_STYLE)&WS_VISIBLE)
    {
    TCHAR* str = (TCHAR*)lParam;
    //::SendMessage(EditNumHwnd, WM_CHAR, WPARAM('a'), 0);//发送一个字消息
    ::SendMessage(EditNumHwnd, WM_SETTEXT, 0, (LPARAM)str);
    
    return 0;
    }
    }
    
    return 1;
    }
  • 相关阅读:
    WPF 关于拖拽打开文件的注意事项
    asp.net core 3.1中对Mongodb BsonDocument的序列化和反序列化支持
    用百度webuploader分片上传大文件
    多线程学习笔记
    web.config数据库连接字符串加密
    Visual Studio 2010 常用快捷方式
    Team Foundation Server 2013 日常使用使用手册(四)分支与合并
    Team Foundation Server 2013 日常使用使用手册(三)上传新工程、创建任务、创建bug、设置预警
    Team Foundation Server 2013 日常使用使用手册(二)修改、签入、撤销、回滚、对比代码变更
    Team Foundation Server 2013 日常使用使用手册(一)-本地连接TFS、查看任务
  • 原文地址:https://www.cnblogs.com/chechen/p/8269000.html
Copyright © 2011-2022 走看看