📄 maillibrary.cpp
字号:
// MailLibrary.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <jni.h>
#include <atlbase.h>
#include "..\JNIMailBridge.h"
#import <cdo.dll>
using namespace MAPI;
JNIEXPORT void JNICALL Java_JNIMailBridge_sendMail
(JNIEnv *env, jobject obj,
jstring _profile, jstring _to, jstring _subject, jstring _body)
{
const char *profile = env->GetStringUTFChars(_profile, 0);
const char *to = env->GetStringUTFChars(_to, 0);
const char *subject = env->GetStringUTFChars(_subject, 0);
const char *body = env->GetStringUTFChars(_body, 0);
// Create a MAPI.Session object
_SessionPtr pSession("MAPI.Session");
// Log on with a specific profile.
// If not specified a logon box would pop up.
pSession->Logon(profile);
// Get a pointer to the Outbox
FolderPtr pOutbox = pSession->Outbox;
// Get a pointer to a collection of messages
MessagesPtr pMessages = pOutbox->Messages;
// Start by creating a new message via Add()
MessagePtr pMessage = pMessages->Add();
// Set the subject, body (Text) of the message
pMessage->Subject = subject;
pMessage->Text = body;
// Get a pointer to a collection of recipients
RecipientsPtr pRecipients = pMessage->Recipients;
// Create a new recipient and Add() it to the list of recipients
RecipientPtr pRec = pRecipients->Add();
// Set the parameters on the recipient:
// address (Name),
// Type (1 is "to")
pRec->Name = to;
pRec->Type = (long)1; // 1=to, 2=cc, 3=bcc
// Resolve the recipient
pRec->Resolve();
// and send it off!
// false = do not save a copy of the message
// false = do not show a dialog
pMessage->Send(false, false);
// finally, log off
pSession->Logoff();
// Free the strings so the Java VM can reclaim the memory.
env->ReleaseStringUTFChars(_profile, profile);
env->ReleaseStringUTFChars(_to, to);
env->ReleaseStringUTFChars(_subject, subject);
env->ReleaseStringUTFChars(_body, body);
}
JNIEXPORT void JNICALL Java_JNIMailBridge_getFolderContents
(JNIEnv *env, jobject obj,
jstring _profile, jstring _folderName, jstring _searchName)
{
jclass mapiSupportClass;
jmethodID mAddMessage, mClearMessages;
const char *folderName = env->GetStringUTFChars(_folderName, 0);
const char *searchName = env->GetStringUTFChars(_searchName, 0);
const char *profile = env->GetStringUTFChars(_profile, 0);
mapiSupportClass = env->GetObjectClass(obj);
if(mapiSupportClass == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't obtain class handle from object passed in");
return;
}
_SessionPtr pSession("MAPI.Session");
// Log on with a specific profile.
// If not specified a logon box would pop up.
pSession->Logon(profile);
InfoStoresPtr pInfoStores;
InfoStorePtr pInfoStore;
FolderPtr pTopFolder;
FoldersPtr pPSTFolders;
long l;
pInfoStores = pSession->GetInfoStores();
if(pInfoStores == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Handle to info stores is invalid");
return;
}
// First we search for the correct collection of folders.
for(l=1; l <= (long)(pInfoStores->GetCount()); l++) {
pInfoStore = pInfoStores->GetItem(l);
pTopFolder = pInfoStore->GetRootFolder();
_bstr_t fName = folderName;
_bstr_t compName = (_bstr_t)pTopFolder->GetName();
if(fName == compName) {
break;
}
}
pPSTFolders = pTopFolder->GetFolders();
if(pPSTFolders == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't create global reference to Java class");
return;
}
// Second we need a handle to the correct folder,
// so search for folderName.
for(l=1; l <= (long)(pPSTFolders->GetCount()); l++) {
FolderPtr tempFolder = pPSTFolders->GetItem(l);
_bstr_t pstName = tempFolder->GetName();
_bstr_t compSearchName = searchName;
if(pstName == compSearchName) {
break;
}
}
// Get a handle to the first message (after getting
// a handle to the folder, then the folder's
// message collection)
FolderPtr pFoundFolder = pPSTFolders->GetItem(l);
if(pFoundFolder == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Folder requested was not found");
return;
}
MessagesPtr pMessages = pFoundFolder->Messages;
if(pMessages == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't obtain handle to message collection");
return;
}
MessagePtr pMessage = pMessages->GetFirst();
if(pMessage == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't obtain handle to first message in collection");
return;
}
mAddMessage = env->GetMethodID(mapiSupportClass,
"addMessage",
"(Ljava/lang/String;Ljava/lang/String;"
"Ljava/lang/String;Ljava/lang/String;)V");
mClearMessages = env->GetMethodID(mapiSupportClass,
"clearMessageList",
"(Ljava/lang/String;)V");
if(mAddMessage == NULL || mClearMessages == NULL) {
printf("Can't obtain handle to class\n");
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't obtain handle to addMessage"
" or clearMessageList Java method");
return;
}
// Call the clearMessageList method to reset the
// message collection
env->CallVoidMethod(obj, mClearMessages, _searchName);
// Loop through all messages in the folder, using the
// addMessage method to store each message
while(pMessage != NULL) {
_bstr_t subject, sender, text, sent;
subject = pMessage->GetSubject();
sender = pMessage->GetSender();
text = pMessage->GetText();
jstring jsSubject, jsSender, jsText;
jsSubject = env->NewStringUTF((char *)subject);
jsSender = env->NewStringUTF((char *)sender);
jsText = env->NewStringUTF((char *)text);
env->CallVoidMethod(obj, mAddMessage, _searchName,
jsSender, jsSubject, jsText);
pMessage = NULL;
pMessage = pMessages->GetNext();
}
pFoundFolder = NULL;
pMessages = NULL;
pMessage = NULL;
// Release the strings
env->ReleaseStringUTFChars(_searchName, searchName);
env->ReleaseStringUTFChars(_folderName, folderName);
env->ReleaseStringUTFChars(_profile, profile);
}
JNIEXPORT void JNICALL Java_JNIMailBridge_getFolderList
(JNIEnv *env, jobject obj, jstring _profile, jstring _topFolder)
{
const char *folderName = env->GetStringUTFChars(_topFolder, 0);
const char *profile = env->GetStringUTFChars(_profile, 0);
_SessionPtr pSession("MAPI.Session");
// Log on with a specific profile.
// If this isn't specified a logon box would pop up.
pSession->Logon(profile);
InfoStoresPtr pInfoStores;
InfoStorePtr pInfoStore;
FolderPtr pTopFolder;
FoldersPtr pPSTFolders;
long l;
pInfoStores = pSession->GetInfoStores();
if(pInfoStores == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't obtain handle to InfoStores");
return;
}
// Search for the specific folder name
for(l=1; l <= (long)(pInfoStores->GetCount()); l++) {
pInfoStore = pInfoStores->GetItem(l);
pTopFolder = pInfoStore->GetRootFolder();
_bstr_t fName = folderName;
_bstr_t compName = (_bstr_t)pTopFolder->GetName();
if(fName == compName) {
// We've found it, exit the loop
break;
}
}
if(pTopFolder == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't obtain handle to top folder");
return;
}
pPSTFolders = pTopFolder->GetFolders();
if(pPSTFolders == NULL) {
env->ThrowNew(env->FindClass("java/lang/Exception"),
"Can't obtain handle to PST folders");
return;
}
jclass cls = env->GetObjectClass(obj);
jmethodID clearFolderID =
env->GetMethodID(cls, "clearFolderList", "()V");
jmethodID addFolderID =
env->GetMethodID(cls, "addFolder", "(Ljava/lang/String;)V");
// First reset the list of folders
env->CallVoidMethod(obj, clearFolderID);
// Loop over all available folders
for(l=1; l <= (long)(pPSTFolders->GetCount()); l++) {
FolderPtr tempFolder = pPSTFolders->GetItem(l);
_bstr_t pstName = tempFolder->GetName();
// Add folder. Remember that the string must be transformed
// into a Java string using NewStringUTF.
env->CallVoidMethod(obj, addFolderID,
env->NewStringUTF((char *)pstName));
}
env->ReleaseStringUTFChars(_topFolder, folderName);
env->ReleaseStringUTFChars(_profile, profile);
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
// Initialize the COM system so we can create COM objects later
CoInitialize(NULL);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -