2020-01-18 11:38:54 +01:00
|
|
|
// Copyright (C) 2019-2020 Jakub Melka
|
2019-12-22 18:03:19 +01:00
|
|
|
//
|
2020-12-20 19:03:58 +01:00
|
|
|
// This file is part of Pdf4Qt.
|
2019-12-22 18:03:19 +01:00
|
|
|
//
|
2020-12-20 19:03:58 +01:00
|
|
|
// Pdf4Qt is free software: you can redistribute it and/or modify
|
2019-12-22 18:03:19 +01:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2020-12-20 19:03:58 +01:00
|
|
|
// Pdf4Qt is distributed in the hope that it will be useful,
|
2019-12-22 18:03:19 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2020-12-20 19:03:58 +01:00
|
|
|
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-22 18:03:19 +01:00
|
|
|
|
|
|
|
#include "pdfsendmail.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <MAPI.h>
|
|
|
|
#include <string>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace pdfviewer
|
|
|
|
{
|
|
|
|
|
|
|
|
bool PDFSendMail::sendMail(QWidget* parent, QString subject, QString fileName)
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
QFileInfo fileInfo(fileName);
|
|
|
|
std::wstring subjectString = subject.toStdWString();
|
|
|
|
std::wstring fileNameString = fileInfo.fileName().toStdWString();
|
|
|
|
std::wstring filePathString = QDir::toNativeSeparators(fileInfo.absoluteFilePath()).toStdWString();
|
|
|
|
|
|
|
|
|
|
|
|
HMODULE mapiLib = ::LoadLibrary(L"MAPI32.DLL");
|
|
|
|
if (!mapiLib)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPMAPISENDMAILW SendMail;
|
|
|
|
SendMail = (LPMAPISENDMAILW)GetProcAddress(mapiLib, "MAPISendMailW");
|
|
|
|
if (!SendMail)
|
|
|
|
{
|
|
|
|
::FreeLibrary(mapiLib);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jakub Melka: now, we have function for mail sending, we can use it!
|
|
|
|
// First, we must fill the info structures.
|
|
|
|
MapiFileDescW fileDesc;
|
|
|
|
::ZeroMemory(&fileDesc, sizeof(fileDesc));
|
|
|
|
|
|
|
|
fileDesc.nPosition = ULONG(-1);
|
|
|
|
fileDesc.lpszFileName = const_cast<PWSTR>(fileNameString.c_str());
|
|
|
|
fileDesc.lpszPathName = const_cast<PWSTR>(filePathString.c_str());
|
|
|
|
|
|
|
|
MapiMessageW message;
|
|
|
|
::ZeroMemory(&message, sizeof(message));
|
|
|
|
|
|
|
|
message.lpszSubject = const_cast<PWSTR>(subjectString.c_str());
|
|
|
|
message.nFileCount = 1;
|
|
|
|
message.lpFiles = &fileDesc;
|
|
|
|
|
|
|
|
const int returnCode = SendMail(0, parent->winId(), &message, MAPI_LOGON_UI | MAPI_DIALOG, 0);
|
|
|
|
|
|
|
|
::FreeLibrary(mapiLib);
|
|
|
|
|
|
|
|
switch (returnCode)
|
|
|
|
{
|
|
|
|
case SUCCESS_SUCCESS:
|
|
|
|
case MAPI_USER_ABORT:
|
|
|
|
case MAPI_E_LOGIN_FAILURE:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
static_assert(false, "Implement this for another OS!");
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pdfviewer
|