VC++ 使用ShellExecute函数调用邮箱客户端发送邮件(可以带附件)

 

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

VC++ 使用ShellExecute函数调用邮箱客户端发送邮件(可以带附件)

Foxmail:

int SharedByFoxmail()
{
TCHAR szPath[MAX_PATH] = { };
SHGetSpecialFolderPath(NULL, szPath, CSIDL_SENDTO, FALSE);
CString strClient;
strClient.Format(L"%ls", szPath);
strClient += L"\\Foxmail.lnk";//拿到快捷方式的绝对路径 CString strRecv = “example@.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 = ;
do
{
Sleep();
hWnd = ::FindWindow(L"TFoxComposeForm.UnicodeClass", NULL);
n--;
} while (n && (hWnd == NULL || !::IsWindowVisible(hWnd))); if (hWnd == NULL)
{
return -;
} HWND hChildWnd1 = NULL;
HWND hChildWnd2 = NULL;
HWND hChildWnd31 = NULL;
HWND hChildWnd32 = NULL;
HWND hChildWnd33 = NULL;
if (hWnd != NULL)
{
::SetForegroundWindow(hWnd); hChildWnd1 = ::FindWindowEx(hWnd, , L"TFoxComposeFrame.UnicodeClass", NULL);
hChildWnd2 = ::FindWindowEx(hChildWnd1, , L"TLayoutManager", NULL);
hChildWnd31 = ::FindWindowEx(hChildWnd2, , 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, , (LPARAM)strRecv.GetBuffer());
strRecv.ReleaseBuffer();
}
}
return 0;
}

网易邮箱大师:

int SharedByNetease()
{
TCHAR szPath[MAX_PATH] = { };
SHGetSpecialFolderPath(NULL, szPath, CSIDL_SENDTO, FALSE);
CString strClient;
strClient.Format(L"%ls", szPath);
strClient += L"\\Foxmail.lnk";//拿到快捷方式的绝对路径 CString strRecv = “example@.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 = ;
do
{
Sleep();
hWnd = ::FindWindow(L"MailWriteWindow", NULL);
n--;
} while (n && (hWnd == NULL || !::IsWindowVisible(hWnd))); if (hWnd == NULL)
{
return -;
} ::SetForegroundWindow(hWnd); hWnd = NULL;
n = ;
do
{
Sleep();
hWnd = ::FindWindow(L"MailWriteWindow", NULL);
n--;
} while (hWnd == NULL && n); if (hWnd == NULL)
{
return -;
} ::SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(, ));//模拟鼠标左键按下 Sleep(); SendKeys(strRecv);
return 0;
}
....................... ///////////////////////////////////////////////// void SendKeys(CString msg)
{
USES_CONVERSION;
wchar_t* data = T2W(msg.GetBuffer());
int len = wcslen(data); for (int i = ; i<len; i++)
{
SendUnicode(data[i]);
}
}

OutLook:

int SharedByOutlook()
{
CString strFile= "\"C:\\Users\\Administrator\\Desktop\\123.txt\""
CString strRecv= “example@.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 = ;
do
{
Sleep();
hWnd = ::FindWindow(L"rctrl_renwnd32", NULL);
n--;
} while (n && (hWnd == NULL || !::IsWindowVisible(hWnd))); if (hWnd == NULL)
{
return -;
} ::SetForegroundWindow(hWnd); TCHAR* szBuffer = strRecv.GetBuffer();
EnumChildWindows(hWnd, EnumChildWindowsProc, (LPARAM)szBuffer);
strRecv.ReleaseBuffer(); return ;
} ///////////////////////////////////////
CString CSharedHelper::ReadOutlookPath()
{
HKEY hKEY;
HKEY hKeyRoot = HKEY_LOCAL_MACHINE;
long ret = ::RegOpenKeyEx(hKeyRoot,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\OUTLOOK.EXE",
,
KEY_READ,
&hKEY);
if (ret != ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
{
// AfxMessageBox(L"错误:无法打开有关的hKEY");
return L"";
} TCHAR Vals[MAX_PATH] = { };
DWORD keyType;
DWORD lenIt = MAX_PATH*sizeof(TCHAR);
if (::RegQueryValueEx(hKEY, L"Path", , &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, , (LPARAM)str); return ;
}
} return ;
}
上一篇:如何在IIS7或IIS7.5中导入导出站点及应用程序池. -摘自网络


下一篇:学习 MySQL中导入 导出CSV