1 #include "WindowsIncludes.h"
2 #include "SendFileTo.h"
3 #include <shlwapi.h>
4 #include <tchar.h>
5 #include <stdio.h>
6 #include <direct.h>
7 
SendMail(HWND hWndParent,const char * strAttachmentFilePath,const char * strAttachmentFileName,const char * strSubject,const char * strBody,const char * strRecipient)8 bool CSendFileTo::SendMail(HWND hWndParent, const char *strAttachmentFilePath, const char *strAttachmentFileName, const char *strSubject, const char *strBody, const char *strRecipient)
9 {
10 //	if (strAttachmentFileName==0)
11 //		return false;
12 
13 //	if (!hWndParent || !::IsWindow(hWndParent))
14 //		return false;
15 
16 	HINSTANCE hMAPI = ::LoadLibraryA(_T("MAPI32.DLL"));
17 	if (!hMAPI)
18 		return false;
19 
20 	ULONG (PASCAL *SendMail)(ULONG, ULONG_PTR, MapiMessage*, FLAGS, ULONG);
21 	(FARPROC&)SendMail = GetProcAddress(hMAPI, _T("MAPISendMail"));
22 
23 	if (!SendMail)
24 		return false;
25 
26 //	TCHAR szFileName[_MAX_PATH];
27 //	TCHAR szPath[_MAX_PATH];
28 	TCHAR szName[_MAX_PATH];
29 	TCHAR szSubject[_MAX_PATH];
30 	TCHAR szBody[_MAX_PATH];
31 	TCHAR szAddress[_MAX_PATH];
32 	TCHAR szSupport[_MAX_PATH];
33 	//strcpy(szFileName, (LPCTSTR)strAttachmentFileName);
34 	//strcpy(szPath, (LPCTSTR)strAttachmentFilePath);
35 	if (strAttachmentFileName)
36 		strcpy(szName, (LPCTSTR)strAttachmentFileName);
37 	strcpy(szSubject, (LPCTSTR)strSubject);
38 	strcpy(szBody, (LPCTSTR)strBody);
39 	sprintf(szAddress, "SMTP:%s", strRecipient);
40 	//strcpy(szSupport, _T("Support"));
41 
42 	char fullPath[_MAX_PATH];
43 	if (strAttachmentFileName && strAttachmentFilePath)
44 	{
45 		if (strlen(strAttachmentFilePath)<3 ||
46 			strAttachmentFilePath[1]!=':' ||
47 			(strAttachmentFilePath[2]!='\\' &&
48 			strAttachmentFilePath[2]!='/'))
49 		{
50 			// Make relative paths absolute
51 			getcwd(fullPath, _MAX_PATH);
52 			strcat(fullPath, "/");
53 			strcat(fullPath, strAttachmentFilePath);
54 		}
55 		else
56 			strcpy(fullPath, strAttachmentFilePath);
57 
58 
59 		// All slashes have to be \\ and not /
60 		int len=(unsigned int)strlen(fullPath);
61 		int i;
62 		for (i=0; i < len; i++)
63 		{
64 			if (fullPath[i]=='/')
65 				fullPath[i]='\\';
66 		}
67 	}
68 
69 
70 	MapiFileDesc fileDesc;
71 	if (strAttachmentFileName && strAttachmentFilePath)
72 	{
73 		ZeroMemory(&fileDesc, sizeof(fileDesc));
74 		fileDesc.nPosition = (ULONG)-1;
75 		fileDesc.lpszPathName = fullPath;
76 		fileDesc.lpszFileName = szName;
77 	}
78 
79 	MapiRecipDesc recipDesc;
80 	ZeroMemory(&recipDesc, sizeof(recipDesc));
81 	recipDesc.lpszName = szSupport;
82 	recipDesc.ulRecipClass = MAPI_TO;
83 	recipDesc.lpszName = szAddress+5;
84 	recipDesc.lpszAddress = szAddress;
85 
86 	MapiMessage message;
87 	ZeroMemory(&message, sizeof(message));
88 	message.nRecipCount = 1;
89 	message.lpRecips = &recipDesc;
90 	message.lpszSubject = szSubject;
91 	message.lpszNoteText = szBody;
92 	if (strAttachmentFileName && strAttachmentFilePath)
93 	{
94 		message.nFileCount = 1;
95 		message.lpFiles = &fileDesc;
96 	}
97 
98 	int nError = SendMail(0, (ULONG_PTR)hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
99 
100 	if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
101 		return false;
102 
103 	return true;
104 }