summaryrefslogtreecommitdiff
path: root/plugins/SendScreenshotPlus/src/CSendEmail.cpp
blob: f8fa02fca44b2209a8c47c8ac4eae36a3c4f086b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (C) 2012-19 Miranda NG team (https://miranda-ng.org),
Copyright (c) 2000-09 Miranda ICQ/IM project,

This file is part of Send Screenshot Plus, a Miranda IM plugin.
Copyright (c) 2010 Ing.U.Horn

Parts of this file based on original sorce code
(c) 2004-2006 Sérgio Vieira Rolanski (portet from Borland C++)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "stdafx.h"

/////////////////////////////////////////////////////////////////////////////////////////

CSendEmail::CSendEmail(HWND Owner, MCONTACT hContact, bool /*bAsync*/)
	: CSend(Owner, hContact, true)
{
	m_EnableItem = SS_DLG_DESCRIPTION | SS_DLG_DELETEAFTERSSEND; // SS_DLG_AUTOSEND | ;
	m_pszSendTyp = LPGENW("Email transfer");
	m_pszFileA = nullptr;
	m_pszFileName = nullptr;
	m_Email = nullptr;
	m_FriendlyName = nullptr;
	m_Subject = nullptr;
}

CSendEmail::~CSendEmail()
{
	mir_free(m_pszFileA);
	mir_free(m_pszFileName);
	mir_free(m_Email);
	mir_free(m_FriendlyName);
	mir_free(m_Subject);
}

/////////////////////////////////////////////////////////////////////////////////////////

int CSendEmail::Send()
{
	if (!m_hContact) return 1;
	mir_free(m_pszFileName);
	m_pszFileName = GetFileNameA(m_pszFile);

	mir_free(m_pszFileA);
	m_pszFileA = mir_u2a(m_pszFile);

	m_Email = mir_u2a(ptrW(Contact_GetInfo(CNF_EMAIL, m_hContact, m_pszProto)));
	m_FriendlyName = mir_u2a(ptrW(Contact_GetInfo(CNF_DISPLAY, m_hContact, m_pszProto)));
	m_Subject = mir_u2a(m_pszFileDesc);

	// SendByEmail(m_pszFileA, "", m_FriendlyName, m_Email, m_Subject);

	// start Send thread
	mir_forkthread(&CSendEmail::SendThreadWrapper, this);
	return 0;
}

void CSendEmail::SendThread()
{
	// This code based on SentTo.exe application.
	// The default mail client for Simple MAPI or MAPI calls is defined by the
	// HKLM\Software\Clients\Mail::(default) registry value.

	MapiFileDesc arrfileDesc[1];

	typedef ULONG(FAR PASCAL *MAPIFUNC)(LHANDLE, ULONG, lpMapiMessage, FLAGS, ULONG);
	MapiMessage Msg;
	MAPIFUNC lpMAPISendMail;

	HINSTANCE hMAPILib = ::LoadLibrary(L"MAPI32.DLL");
	if (hMAPILib == nullptr) {
		// return -1;
		Error(SS_ERR_INIT, m_pszSendTyp);
		Exit(ACKRESULT_FAILED); return;
	}

	lpMAPISendMail = (MAPIFUNC)GetProcAddress(hMAPILib, "MAPISendMail");
	if (lpMAPISendMail == nullptr) {
		::FreeLibrary(hMAPILib);
		// return -2;
		Error(SS_ERR_INIT, m_pszSendTyp);
		Exit(ACKRESULT_FAILED); return;
	}

	memset(&Msg, 0, sizeof(Msg));

	arrfileDesc[0].ulReserved = 0;
	arrfileDesc[0].flFlags = 0;
	arrfileDesc[0].lpFileType = nullptr;
	arrfileDesc[0].nPosition = -1;
	arrfileDesc[0].lpszPathName = m_pszFileA;
	arrfileDesc[0].lpszFileName = nullptr;

	Msg.nFileCount = 1;
	Msg.lpFiles = arrfileDesc;
	Msg.lpszNoteText = "";							// body
	Msg.lpszSubject = m_Subject;					// subject

	Msg.nRecipCount = 1;
	MapiRecipDesc recip;
	recip.ulReserved = 0;
	recip.ulRecipClass = MAPI_TO;

	if (m_FriendlyName && m_FriendlyName[0] != NULL) {
		recip.lpszName = m_FriendlyName;			// friendly name set to contact's name
	}
	else {
		recip.lpszName = m_Email;					// friendly name set to contact's email
	}

	recip.lpszAddress = m_Email;					// email
	recip.ulEIDSize = 0;
	recip.lpEntryID = nullptr;
	Msg.lpRecips = &recip;

	try {
		int res = lpMAPISendMail(NULL, NULL, &Msg, MAPI_LOGON_UI | MAPI_DIALOG, 0);
		::FreeLibrary(hMAPILib);

		wchar_t* err;
		switch (res) {
		case SUCCESS_SUCCESS:
			// The call succeeded and the message was sent.
			Exit(ACKRESULT_SUCCESS); return;
			// No message was sent
		case MAPI_E_AMBIGUOUS_RECIPIENT:
			err = LPGENW("A recipient matched more than one of the recipient descriptor structures and MAPI_DIALOG was not set");
			break;
		case MAPI_E_ATTACHMENT_NOT_FOUND:
			err = LPGENW("The specified attachment was not found");
			break;
		case MAPI_E_ATTACHMENT_OPEN_FAILURE:
			err = LPGENW("The specified attachment could not be opened");
			break;
		case MAPI_E_BAD_RECIPTYPE:
			err = LPGENW("The type of a recipient was not MAPI_TO, MAPI_CC, or MAPI_BCC");
			break;
		case MAPI_E_FAILURE:
			err = LPGENW("One or more unspecified errors occurred");
			break;
		case MAPI_E_INSUFFICIENT_MEMORY:
			err = LPGENW("There was insufficient memory to proceed");
			break;
		case MAPI_E_INVALID_RECIPS:
			err = LPGENW("One or more recipients were invalid or did not resolve to any address");
			break;
		case MAPI_E_LOGIN_FAILURE:
			err = LPGENW("There was no default logon, and the user failed to log on successfully when the logon dialog box was displayed");
			break;
		case MAPI_E_TEXT_TOO_LARGE:
			err = LPGENW("The text in the message was too large");
			break;
		case MAPI_E_TOO_MANY_FILES:
			err = LPGENW("There were too many file attachments");
			break;
		case MAPI_E_TOO_MANY_RECIPIENTS:
			err = LPGENW("There were too many recipients");
			break;
		case MAPI_E_UNKNOWN_RECIPIENT:
			err = LPGENW("A recipient did not appear in the address list");
			break;
		case MAPI_E_USER_ABORT:
			err = LPGENW("The user canceled one of the dialog boxes");
			break;
		default:
			err = LPGENW("Unknown Error");
			break;
		}
		Error(SS_ERR_MAPI, res, err);
		Exit(ACKRESULT_FAILED);
	}
	catch (...) {
		::FreeLibrary(hMAPILib);
		Error(SS_ERR_INIT, m_pszSendTyp);
		Exit(ACKRESULT_FAILED);
		return;
	}
}

void	CSendEmail::SendThreadWrapper(void * Obj)
{
	reinterpret_cast<CSendEmail*>(Obj)->SendThread();
}