-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (72 loc) · 2.91 KB
/
Copy pathmain.cpp
File metadata and controls
90 lines (72 loc) · 2.91 KB
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
#include <napi.h>
#include <algorithm>
#ifdef _WIN32
#include <windows.h>
#include <mapi.h>
#endif
Napi::Value Send(const Napi::CallbackInfo& info) {
#ifndef _WIN32
return Napi::Boolean::New(env,false);
#endif
HINSTANCE hMAPI = ::LoadLibrary("mapi32.dll");
auto lpfnMAPISendMail = (LPMAPISENDMAIL) ::GetProcAddress(hMAPI, "MAPISendMail");
Napi::Env env = info.Env();
if (info.Length() < 3) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString() || !info[1].IsString() || !info[2].IsString()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}
std::string recipient = info[0].As<Napi::String>().Utf8Value();
std::string subject = info[1].As<Napi::String>().Utf8Value();
std::string body = info[2].As<Napi::String>().Utf8Value();
std::string attachment = "";
if(info.Length() > 3){
attachment = info[3].As<Napi::String>().Utf8Value();
}
char szTo[MAX_PATH] = {0};
strcat_s(szTo, (LPCSTR) recipient.c_str());
MapiRecipDesc recipients[1] = {0};
recipients[0].ulRecipClass = MAPI_TO;
recipients[0].lpszAddress = szTo;
char szSubject[MAX_PATH] = {0};
strcat_s(szSubject, (LPCSTR) subject.c_str());
char szText[MAX_PATH] = {0};
strcat_s(szText, (LPCSTR) body.c_str());
MapiMessage MAPImsg = {0};
if (!attachment.empty()) {
MapiFileDesc fileDesc;
ZeroMemory(&fileDesc, sizeof(fileDesc));
fileDesc.nPosition = (ULONG) 0xFFFFFFFF;
auto pos = attachment.find_last_of('/') + 1;
auto fileName = attachment.substr(pos, attachment.length() - pos);
auto path = attachment;
std::replace(path.begin(), path.end(), '/', '\\');
fileDesc.lpszPathName = (LPTSTR) path.c_str();
fileDesc.lpszFileName = (LPTSTR) fileName.c_str();
MAPImsg.nFileCount = 1;
MAPImsg.lpFiles = &fileDesc;
MAPImsg.lpszSubject = szSubject;
MAPImsg.lpRecips = recipients;
MAPImsg.nRecipCount = 1;
MAPImsg.lpszNoteText = szText;
ULONG nSent = lpfnMAPISendMail(0, 0,
&MAPImsg, MAPI_LOGON_UI | MAPI_DIALOG | MAPI_NEW_SESSION, 0);
return Napi::Boolean::New(env,(nSent == SUCCESS_SUCCESS || nSent == MAPI_E_USER_ABORT));
}
MAPImsg.lpszSubject = szSubject;
MAPImsg.lpRecips = recipients;
MAPImsg.nRecipCount = 1;
MAPImsg.lpszNoteText = szText;
ULONG nSent = lpfnMAPISendMail(0, 0,
&MAPImsg, MAPI_LOGON_UI | MAPI_DIALOG | MAPI_NEW_SESSION, 0);
return Napi::Boolean::New(env,(nSent == SUCCESS_SUCCESS || nSent == MAPI_E_USER_ABORT));
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "send"), Napi::Function::New(env, Send));
return exports;
}
NODE_API_MODULE(addon, Init)