MySQL Forums
Forum List  »  Triggers

Re: Send email via trigger
Posted by: Zigmund Bulinsh
Date: December 08, 2007 03:25AM

I am not a C lover - i hate it, but there is my source for C++ builder 2007 - for sending MAPI message.

for mysql 5.1 place dll in the lib folder. Then exec

create function _MySqlSendMail returns integer soname 'dllname.dll'

after that you can try to use it..But there is some problems..This is not working and function MAPISendMail itself returns 3 - logon failure.. But this is not the main problem also..(maybe it was outlook or antivirus...), second problem is that MySQL server crashes after first call to this function (it doesnt see any databases after one call, but continue to work..)
I dont know how to call normally in C this function..hate this language..so i wrote this in assembler,,

#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
#include <windows.h>
#include <winbase.h>
#include <mapi.h>
#include <stdlib.h>

#define MAPIDLL "mapi32.dll"
#define SEND_MAIL_FUNC "MAPISendMail"
#define MSG_ARG_SUBJECT 0
#define MSG_BODY 1
#define MSG_FROM_NAME 2
#define MSG_FROM_EMAIL 3
#define MSG_TO_NAME 4
#define MSG_TO_EMAIL 5
#define MSG_ERROR_MAXLEN 31

//---------------------------------------------------------------------------
char *ErrorText;

__declspec( dllexport ) my_bool MySqlSendMail_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
int i;//loop

if (args->arg_count != 6) {
strcpy(message, "MySqlSendMail() must have exactly six arguments");
return 1;
}
for(i = 0; i < 6; i++)
if (args->arg_type != STRING_RESULT) {
strcpy(message, "MySqlSendMail() must have six string arguments");
return 1;
}

initid->maybe_null = 0;
initid->decimals = 0;
initid->max_length = MSG_ERROR_MAXLEN;
initid->const_item = 0;
ErrorText = (char *) calloc(MSG_ERROR_MAXLEN, sizeof(char));

return 0;
}
//---------------------------------------------------------------------------
__declspec( dllexport ) void MySqlSendMail_deinit(UDF_INIT *initid)
{
free(ErrorText);
}
//---------------------------------------------------------------------------
__declspec( dllexport ) char * MySqlSendMail(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
MapiMessage msg;
MapiRecipDesc Sender, Recipient;
void *SendMailProc;
int result;
HMODULE MAPIModule;
char *Subject, *Body, *SenderName, *SenderEmail;
char *RecipientName, *RecipientEmail, *Address;

//params
Subject = args->args[MSG_ARG_SUBJECT];
Body = args->args[MSG_BODY];
SenderName = args->args[MSG_FROM_NAME];
SenderEmail = args->args[MSG_FROM_EMAIL];
RecipientName = args->args[MSG_TO_NAME];
RecipientEmail = args->args[MSG_TO_EMAIL];

ZeroMemory(&msg, sizeof(MapiMessage));

msg.lpFiles = NULL;//no attachments
msg.lpszSubject = Subject;
msg.lpszNoteText = Body;

//filling sender data
if (strlen(SenderEmail) > 0) {
Sender.ulRecipClass = MAPI_ORIG;
if (strlen(SenderName) == 0)
Sender.lpszName = SenderEmail;
else
Sender.lpszName = SenderName;

//*assigning e-mal address
Address = (char *) calloc(strlen(SenderEmail) + 6, sizeof(char));
strcpy(Address, "SMTP:");
Sender.lpszAddress = strcat(Address, SenderEmail);
free(Address);
//email address now is assigned
Sender.ulReserved = 0;
Sender.ulEIDSize = 0;
Sender.lpEntryID = NULL;
msg.lpOriginator = &Sender;
}
//filling recipient data
if (strlen(RecipientEmail) > 0) {
Recipient.ulRecipClass = MAPI_ORIG;
if (strlen(RecipientName) == 0)
Recipient.lpszName = RecipientEmail;
else
Recipient.lpszName = RecipientName;

//*assigning e-mal address
Address = (char *) calloc(strlen(RecipientEmail) + 6, sizeof(char));
strcpy(Address, "SMTP:");
Recipient.lpszAddress = strcat(Address, RecipientEmail);
free(Address);
//email address now is assigned
Recipient.ulReserved = 0;
Recipient.ulEIDSize = 0;
Recipient.lpEntryID = NULL;
msg.nRecipCount = 1;
msg.lpRecips = &Recipient;
} else {
msg.lpRecips = NULL;
}

MAPIModule = LoadLibraryA(MAPIDLL);
if (!MAPIModule)
return -1;
SendMailProc = GetProcAddress(MAPIModule, SEND_MAIL_FUNC);
if (SendMailProc)
asm {
//pascal calling
push 0;//ulReserved
push 0;//flFlags
push offset msg;//lpMessage
push 0//ulUIParam
push 0//lhSession
call SendMailProc
lea ebx, result
mov [ebx], eax
mov [ebx + 4], edx
}
else
result = -2;
FreeLibrary(MAPIModule);

switch (result) {
case -1:
strcpy(ErrorText, "MAPI32.DLL not found.");
break;
case -2:
strcpy(ErrorText, "MAPISendMail export not found.");
case MAPI_E_USER_ABORT:
strcpy(ErrorText, "MAPI_E_USER_ABORT");
break;
case MAPI_E_FAILURE:
strcpy(ErrorText, "MAPI_E_FAILURE");
break;
case MAPI_E_LOGON_FAILURE:
strcpy(ErrorText, "MAPI_E_LOGON_FAILURE");
break;
case MAPI_E_DISK_FULL:
strcpy(ErrorText, "MAPI_E_DISK_FULL");
break;
case MAPI_E_INSUFFICIENT_MEMORY:
strcpy(ErrorText, "MAPI_E_INSUFFICIENT_MEMORY");
break;
case MAPI_E_ACCESS_DENIED:
strcpy(ErrorText, "MAPI_E_ACCESS_DENIED");
break;
case MAPI_E_TOO_MANY_SESSIONS:
strcpy(ErrorText, "MAPI_E_TOO_MANY_SESSIONS");
break;
case MAPI_E_TOO_MANY_FILES:
strcpy(ErrorText, "MAPI_E_TOO_MANY_FILES");
break;
case MAPI_E_TOO_MANY_RECIPIENTS:
strcpy(ErrorText, "MAPI_E_TOO_MANY_RECIPIENTS");
break;
case MAPI_E_ATTACHMENT_NOT_FOUND:
strcpy(ErrorText, "MAPI_E_ATTACHMENT_NOT_FOUND");
break;
case MAPI_E_ATTACHMENT_OPEN_FAILURE:
strcpy(ErrorText, "MAPI_E_ATTACHMENT_OPEN_FAILURE");
break;
case MAPI_E_ATTACHMENT_WRITE_FAILURE:
strcpy(ErrorText, "MAPI_E_ATTACHMENT_WRITE_FAILURE");
break;
case MAPI_E_UNKNOWN_RECIPIENT:
strcpy(ErrorText, "MAPI_E_UNKNOWN_RECIPIENT");
break;
case MAPI_E_BAD_RECIPTYPE:
strcpy(ErrorText, "MAPI_E_BAD_RECIPTYPE");
break;
case MAPI_E_NO_MESSAGES:
strcpy(ErrorText, "MAPI_E_NO_MESSAGES");
break;
case MAPI_E_INVALID_MESSAGE:
strcpy(ErrorText, "MAPI_E_INVALID_MESSAGE");
break;
case MAPI_E_TEXT_TOO_LARGE:
strcpy(ErrorText, "MAPI_E_TEXT_TOO_LARGE");
break;
case MAPI_E_INVALID_SESSION:
strcpy(ErrorText, "MAPI_E_INVALID_SESSION");
break;
case MAPI_E_TYPE_NOT_SUPPORTED:
strcpy(ErrorText, "MAPI_E_TYPE_NOT_SUPPORTED");
break;
case MAPI_E_AMBIGUOUS_RECIPIENT:
strcpy(ErrorText, "MAPI_E_AMBIGUOUS_RECIPIENT");
break;
case MAPI_E_MESSAGE_IN_USE:
strcpy(ErrorText, "MAPI_E_MESSAGE_IN_USE");
break;
case MAPI_E_NETWORK_FAILURE:
strcpy(ErrorText, "MAPI_E_NETWORK_FAILURE");
break;
case MAPI_E_INVALID_EDITFIELDS:
strcpy(ErrorText, "MAPI_E_INVALID_EDITFIELDS");
break;
case MAPI_E_INVALID_RECIPS:
strcpy(ErrorText, "MAPI_E_INVALID_RECIPS");
break;
case MAPI_E_NOT_SUPPORTED:
strcpy(ErrorText, "MAPI_E_NOT_SUPPORTED");
break;
default:
strcpy(ErrorText, "MAPI_E_UNKNOWN");
}
return ErrorText;
}
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------



Edited 5 time(s). Last edit at 12/08/2007 05:17AM by Zigmund Bulinsh.

Options: ReplyQuote


Subject
Views
Written By
Posted
69755
July 09, 2005 09:24AM
35543
July 09, 2005 04:00PM
23107
July 10, 2005 07:18AM
34831
October 19, 2005 08:53AM
27979
October 20, 2005 09:40AM
24374
October 23, 2005 05:16AM
15523
February 13, 2007 06:35AM
15134
May 13, 2007 10:39AM
11564
November 24, 2008 11:10PM
14429
February 22, 2007 03:44AM
12724
December 08, 2007 08:17AM
10248
September 09, 2008 04:00AM
8942
March 12, 2009 07:08AM
18987
February 27, 2007 07:53AM
9741
September 09, 2008 04:02AM
12709
May 14, 2007 01:07AM
11915
June 04, 2007 01:14PM
11006
June 27, 2007 10:10PM
10352
August 09, 2007 06:15AM
12723
August 25, 2007 09:13AM
13158
August 30, 2007 01:24AM
16526
August 30, 2007 11:43PM
31899
September 12, 2007 05:39AM
Re: Send email via trigger
11554
December 08, 2007 03:25AM
8942
December 08, 2007 05:19AM
7705
August 04, 2008 01:15AM
7249
January 12, 2009 05:10AM
7218
January 07, 2009 04:37AM
10508
March 07, 2009 05:11AM
6937
March 12, 2009 06:21AM
8410
March 12, 2009 06:38AM
15862
April 16, 2009 05:53PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.