Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

API for send email

296 views
Skip to first unread message

Noixe

unread,
Feb 18, 2009, 11:28:16 AM2/18/09
to
Hello,

I'm italian, sorry for my english :-)

Is there an API of Windows for send an email with attachment?

Something as FtpPutFile to send a file at server.

Thanks


Christian ASTOR

unread,
Feb 18, 2009, 12:23:54 PM2/18/09
to
On 18 fév, 17:28, "Noixe" <no...@TOGLIMIemail.it> wrote:

> Is there an API of Windows for send an email with attachment?

Use MAPI (MAPISendMail())
(or Winsock + RFC 821 + base64 encoding for binaries (BLAT source
code for example))

Noixe

unread,
Feb 18, 2009, 5:07:15 PM2/18/09
to
I try this code but don't work I believe because there aren't some
parameters.


#include <iostream>
#include <windows.h>
#include <mapi.h>

int main() {
LHANDLE lh;
lpMapiMessage lMM;
FLAGS f;
ULONG u;
LPLHANDLE lp;

lMM->lpszSubject = "Subject";
lMM->lpszNoteText = "Message";
lMM->nRecipCount = 1;
lMM->lpRecips->lpszName = "Recipient Name";
lMM->lpRecips->lpszAddress = "reci...@mail.com";
lMM->nFileCount = 1;
lMM->lpFiles->lpszPathName = "C:\\";
lMM->lpFiles->lpszFileName = "test.txt";

MAPILogon(u, "MyEmail", "MyPassword", f, u, lp);

MAPISendMail(lh, u, lMM, f, u);

MAPILogoff(lh, u, f, u);

return 0;
}

Where I must write smtp address?

Could you write a simple example?

Thanks


Noixe

unread,
Feb 18, 2009, 5:36:40 PM2/18/09
to
"Noixe" <no...@TOGLIMIemail.it> ha scritto:


> MAPILogon(u, "MyEmail", "MyPassword", f, u, lp);

This istruction is:
lh = MAPILogon(u, "MyEmail", "MyPassword", f, u, lp);


Noixe

unread,
Feb 19, 2009, 9:47:02 AM2/19/09
to
I had forgotten malloc function.

This code don't crash but don't send email:

#include <iostream>
#include <windows.h>
#include <mapi.h>


int main() {
LHANDLE lh;
lpMapiMessage lMM;
FLAGS f;
ULONG u;
LPLHANDLE lp;

lMM = (lpMapiMessage)malloc(sizeof(lpMapiMessage));


lMM->lpszSubject = "Subject";
lMM->lpszNoteText = "Message";
lMM->nRecipCount = 1;

lMM->nFileCount = 1;

lMM->lpRecips = (lpMapiRecipDesc)malloc(sizeof(lpMapiRecipDesc));
lMM->lpRecips->lpszName = "Noixe";
lMM->lpRecips->lpszAddress = "no...@hotmail.com";

/*lMM->lpFiles = (lpMapiFileDesc)malloc(sizeof(lpMapiFileDesc));


lMM->lpFiles->lpszPathName = "C:\\";

lMM->lpFiles->lpszFileName = "test.txt";*/

MAPILogon(0L, "MyEmail", "MyPassword", MAPI_LOGON_UI, 0L, &lh);

MAPISendMail(lh, u, lMM, f, u);

MAPILogoff(lh, 0L, 0L, 0L);

free(lMM->lpRecips);
free(lMM);

return 0;
}


Kellie Fitton

unread,
Feb 19, 2009, 10:58:13 AM2/19/09
to
> }- Hide quoted text -
>
> - Show quoted text -

Hi,

The MAPI APIs can be used to send eMail in concert
with a MAPI enabled mail client on your machine.

Also, you should check the return values to determine
if the call succeeded and the message was sent.

Additionally, you can send eMail by using the SMTP protocol, the
exact name for this technology is "Simple Mail Transfer Protocol",
the SMTP server enables you to send eMail directly from your machine
without using your internet service provider's server machine.

http://msdn.microsoft.com/en-us/library/dd296721(VS.85).aspx

http://www.smtp.com/

Kellie.

Christian ASTOR

unread,
Feb 19, 2009, 11:34:04 AM2/19/09
to
On 18 fév, 23:07, "Noixe" <no...@TOGLIMIemail.it> wrote:

> Could you write a simple example?

A basic sample =>

#include <windows.h>
#include <tchar.h>
#include <mapi.h>

int _tmain(int argc, _TCHAR* argv[])
{
HMODULE hMapiDLL = LoadLibrary("MAPI32.DLL");
if (hMapiDLL==NULL)
{
MessageBox(NULL, "Error loading Mapi32.dll", "Error", MB_OK |
MB_ICONSTOP);
return -1;
}

LPMAPILOGON MapiLogon = (LPMAPILOGON)GetProcAddress(hMapiDLL,
"MAPILogon");
LPMAPILOGOFF MapiLogoff = (LPMAPILOGOFF)GetProcAddress(hMapiDLL,
"MAPILogoff");
LPMAPISENDMAIL MapiSendMail = (LPMAPISENDMAIL)GetProcAddress
(hMapiDLL, "MAPISendMail");
if (!MapiLogon || !MapiLogoff || !MapiSendMail)
{
FreeLibrary(hMapiDLL);
MessageBox(NULL, "Error getting MAPI functions", "Error", MB_OK |
MB_ICONSTOP);
return -1;
}

LHANDLE hSession;
ULONG nResult;

nResult = MapiLogon(NULL, NULL, NULL, MAPI_NEW_SESSION |
MAPI_LOGON_UI, 0, &hSession);

if (nResult != SUCCESS_SUCCESS)
{
FreeLibrary(hMapiDLL);
MessageBox(NULL, "Error logging on", "Error", MB_OK | MB_ICONSTOP);

}

MapiMessage msg;
ZeroMemory(&msg, sizeof(msg));

msg.ulReserved = 0;
msg.lpszSubject = "Subject";
msg.lpszNoteText = "Hello";
msg.lpszMessageType = NULL;
msg.lpszDateReceived = NULL;
msg.lpszConversationID = NULL;
msg.flFlags = 0;
msg.lpOriginator = NULL;

MapiRecipDesc mapito;
ZeroMemory(&mapito, sizeof(mapito));
mapito.ulRecipClass = MAPI_TO;
mapito.lpszName = "de...@mail.com";
msg.nRecipCount = 1;
msg.lpRecips = &mapito;

MapiFileDesc mapifiles;
ZeroMemory(&mapifiles, sizeof(mapifiles));
char sPath[MAX_PATH];
GetWindowsDirectory(sPath, MAX_PATH);
lstrcat(sPath, "\\win.ini");
mapifiles.lpszPathName = sPath;
mapifiles.lpszFileName = "Win.ini";
msg.nFileCount = 1;
msg.lpFiles = &mapifiles;

nResult = MapiSendMail(hSession, NULL, &msg, MAPI_LOGON_UI |
MAPI_NEW_SESSION, 0L);
if (nResult != SUCCESS_SUCCESS && nResult != MAPI_USER_ABORT)
{
FreeLibrary(hMapiDLL);
MessageBox(NULL, "Error sending message", "Error", MB_OK |
MB_ICONSTOP);
}

nResult = MapiLogoff(hSession, NULL, 0, 0);
if (nResult != SUCCESS_SUCCESS)
{
FreeLibrary(hMapiDLL);
MessageBox(NULL, "Error login off", "Error", MB_OK | MB_ICONSTOP);
}

FreeLibrary(hMapiDLL);

return 0;
}

Paul J. Lucas

unread,
Feb 19, 2009, 12:20:53 PM2/19/09
to
Kellie Fitton wrote:

> The MAPI APIs can be used to send eMail in concert
> with a MAPI enabled mail client on your machine.

Only if the user bothered to configure their local e-mail client. In this case,
MAPI won't work. (The user could be using a web-mail service like Gmail.)

> Additionally, you can send eMail by using the SMTP protocol, the
> exact name for this technology is "Simple Mail Transfer Protocol",
> the SMTP server enables you to send eMail directly from your machine
> without using your internet service provider's server machine.

And many ISP's block this because it's a source of spam. They force you to
relay through their SMTP servers.

- Paul

Noixe

unread,
Feb 19, 2009, 1:47:27 PM2/19/09
to
I have deleted some istructions to make more clear the algorithm. Is there a
way to avoid that a windows of confirm is shown?

This is the previous code with modifications:

#include <windows.h>
#include <mapi.h>


int main(int argc, char* argv[]) {

LHANDLE hSession;

MAPILogon(0, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0,
&hSession);
MapiMessage msg;
ZeroMemory(&msg, sizeof(msg));

msg.ulReserved = 0;
msg.lpszSubject = "Subject";

msg.lpszNoteText = "Message";


msg.lpszMessageType = NULL;
msg.lpszDateReceived = NULL;
msg.lpszConversationID = NULL;
msg.flFlags = 0;
msg.lpOriginator = NULL;

MapiRecipDesc mapito;
ZeroMemory(&mapito, sizeof(mapito));
mapito.ulRecipClass = MAPI_TO;

mapito.lpszName = "no...@email.it";


msg.nRecipCount = 1;
msg.lpRecips = &mapito;

MapiFileDesc mapifiles;
mapifiles.lpszPathName = "C:\\test1.txt";
mapifiles.lpszFileName = "test2.txt";


msg.nFileCount = 1;
msg.lpFiles = &mapifiles;

MAPISendMail(hSession, 0, &msg, MAPI_LOGON_UI | MAPI_NEW_SESSION, 0L);

MAPILogoff(hSession, 0, 0, 0);

return 0;
}


Noixe

unread,
Feb 19, 2009, 1:54:33 PM2/19/09
to
"Kellie Fitton" <KELLIE...@YAHOO.COM> ha scritto nel messaggio
news:8f70d215-e707-4466-b73f-


> Hi,

>The MAPI APIs can be used to send eMail in concert
>with a MAPI enabled mail client on your machine.

>Additionally, you can send eMail by using the SMTP protocol, the
>exact name for this technology is "Simple Mail Transfer Protocol",
>the SMTP server enables you to send eMail directly from your machine
>without using your internet service provider's server machine.

MAPISendMail can be used for both cases?


Norman Bullen

unread,
Feb 19, 2009, 8:30:36 PM2/19/09
to
You are passing what appear to be the sizes of pointer to malloc(). You
should pass the size of the structure; without being familiar with the
MAPI declarations, I would guess that the three malloc() calls should be
(lpMapiMessage)malloc(sizeof(MapiMessage))
(lpMapiRecipDesc)malloc(sizeof(MapiRecipDesc))
(lpMapiFileDesc)malloc(sizeof(MapiFileDesc))
--
Norm

To reply, change domain to an adult feline.

Christian ASTOR

unread,
Feb 20, 2009, 2:07:45 AM2/20/09
to
Noixe wrote:

> Is there a way to avoid that a windows of confirm is shown?

If you're talking about Outlook Dlg, yes, with Extended MAPI.

Noixe

unread,
Feb 20, 2009, 5:05:02 AM2/20/09
to
"Christian ASTOR" <cast...@club-internet.fr> ha scritto:

> If you're talking about Outlook Dlg, yes, with Extended MAPI.

I talking about a windows shown by the program that you has sent.

I must change functions or only set some flag?


Dean Earley

unread,
Feb 20, 2009, 8:46:26 AM2/20/09
to

That message is designed to stop software sending emails without the
user confirming.
You need to use a completely different method to send them.

We however just settled with using ClickYes as it is a single internal
system.

--
Dean Earley (dean....@icode.co.uk)
i-Catcher Development Team

iCode Systems

Noixe

unread,
Feb 20, 2009, 9:28:02 AM2/20/09
to
"Dean Earley" <dean....@icode.co.uk> ha scritto:

> That message is designed to stop software sending emails without the user
> confirming.

> You need to use a completely different method to send them.

I can't use MAPI for this type of send?


> We however just settled with using ClickYes as it is a single internal
> system.

Could you write an example or give me a link?

Thank


Christian

unread,
Feb 20, 2009, 10:48:08 AM2/20/09
to
On 20 fév, 15:28, "Noixe" <no...@TOGLIMIemail.it> wrote:
> I can't use MAPI for this type of send?

Extended MAPI (MAPILogonEx() & MAPI_EXTENDED, ..., SubmitMessage())

Noixe

unread,
Feb 20, 2009, 11:12:44 AM2/20/09
to
"Christian" <cast...@club-internet.fr> ha scritto:

>Extended MAPI (MAPILogonEx() & MAPI_EXTENDED, ..., SubmitMessage())

I found only information for Windows CE and Windows Mobile...


Noixe

unread,
Feb 20, 2009, 11:32:00 AM2/20/09
to
"Christian" <cast...@club-internet.fr> ha scritto:

>Extended MAPI (MAPILogonEx() & MAPI_EXTENDED, ..., SubmitMessage())

Which header I must include?


Dean Earley

unread,
Feb 20, 2009, 12:20:47 PM2/20/09
to
Noixe wrote:
> "Dean Earley" <dean....@icode.co.uk> ha scritto:
>> We however just settled with using ClickYes as it is a single internal
>> system.
>
> Could you write an example or give me a link?

http://www.google.com/search?q=ClickYes

Noixe

unread,
Feb 20, 2009, 3:09:44 PM2/20/09
to
"Dean Earley" <dean....@icode.co.uk> ha scritto:

>> Could you write an example or give me a link?
>
> http://www.google.com/search?q=ClickYes

Thanks, but for using Extended MAPI I must download a SDK?

Is not supported in Windows as Simple MAPI?


Kellie Fitton

unread,
Feb 20, 2009, 3:18:48 PM2/20/09
to
On Feb 20, 8:32 am, "Noixe" <no...@TOGLIMIemail.it> wrote:
> "Christian" <casto...@club-internet.fr> ha scritto:

>
> >Extended MAPI (MAPILogonEx() & MAPI_EXTENDED, ..., SubmitMessage())
>
> Which header I must include?

Hi,

The header is Mapix.h as listed below.

http://msdn.microsoft.com/en-us/library/cc815545.aspx

Noixe

unread,
Feb 21, 2009, 3:53:52 AM2/21/09
to
"Kellie Fitton" <KELLIE...@YAHOO.COM> ha scritto:

>Hi,

>The header is Mapix.h as listed below.

>http://msdn.microsoft.com/en-us/library/cc815545.aspx

The compiler Mingw used by Dev-C++ 4.9.9.2 haven't that header


Noixe

unread,
Feb 21, 2009, 4:09:24 AM2/21/09
to
For sending a message to more recipient, I must creat an array of
MapiRecipDesc?

I try:

MapiRecipDesc mapito[2];
ZeroMemory(&mapito, sizeof(mapito));

mapito[0].ulRecipClass = MAPI_TO;
mapito[0].lpszName = "no...@email.it";

mapito[1].ulRecipClass = MAPI_CC;
mapito[1].lpszName = "no...@hotmail.com";

msg.nRecipCount = 2;
msg.lpRecips = mapito; // I believe that this istruction is wrong. It take
only first element of array and message was sent only at the first addres

Thanks


Noixe

unread,
Feb 21, 2009, 4:12:23 AM2/21/09
to
"Noixe" <no...@TOGLIMIemail.it> ha scritto:

Sorry, the code above work! :-)


Noixe

unread,
Feb 21, 2009, 10:07:20 AM2/21/09
to
"Noixe" <no...@TOGLIMIemail.it> ha scritto:

> The compiler Mingw used by Dev-C++ 4.9.9.2 haven't that header

With C++ Builder 3 I haven't error but I don't know how use the function
MAPILogonEx()
SubmitMessage()

suggested by Christian


Christian ASTOR

unread,
Feb 22, 2009, 1:07:13 PM2/22/09
to

It depends on which api you use.
There are several samples in MSDN, like KB200174, but I've had to modify
them to make them work...
Otherwise, you can also use CDOSYS. It's what works best for me on XP
with a very few lines of code.
(CoCreateInstance() on IMessage, IMessage::get_Fields()
ADODB::FieldsPtr::get_Item(),
ADODB::FieldPtr::put_Value() for recipient,
IMessage::AddAttachment(), ... etc... IMessage::Send())

Noixe

unread,
Feb 22, 2009, 2:45:58 PM2/22/09
to
"Christian ASTOR" <cast...@club-internet.fr> ha scritto:

> It depends on which api you use.

Only api necessary for send email with attachment without the window of
confirm

> There are several samples in MSDN, like KB200174, but I've had to modify
> them to make them work...

I have seen that message, but where I could find:

Mapi32.LIB
Version.LIB
Edkdebug.LIB
Edkmapi.LIB
Edkutils.LIB
Addrlkup.LIB
Edkguid.LIB
Rulecls.LIB
Msvcrt.LIB

???

Maybe it necessary outlook 2007

> Otherwise, you can also use CDOSYS. It's what works best for me on XP with
> a very few lines of code.

It is a SDK? I would want to know if I needs download something or is all
included in windows


Christian

unread,
Feb 23, 2009, 10:21:59 AM2/23/09
to
On 22 fév, 20:45, "Noixe" <no...@TOGLIMIemail.it> wrote:

> I have seen that message, but where I could find:
> Mapi32.LIB
> Version.LIB

> Edkdebug.LIB...

They are in the Platform SDK

> > Otherwise, you can also use CDOSYS. It's what works best for me on XP with
> > a very few lines of code.

> It is a SDK? I would want to know if  I needs download something or is all
> included in windows

No, it's a COM DLL, included since Win2000

A Test on XP, sending a mail with an attachment =>

//#import "c:\windows\system32\CDOSys.DLL" no_namespace auto_search
auto_rename named_guids

#include <windows.h>
#include "cdosys.tlh"

int main()
{
CoInitialize(NULL);
IMessage* pMessage = NULL;
HRESULT hr = CoCreateInstance(__uuidof(Message), NULL,
CLSCTX_INPROC_SERVER, __uuidof(IMessage), reinterpret_cast<void**>
(&pMessage));

ADODB::FieldsPtr pFields = NULL;
hr = pMessage->get_Fields(&pFields);

ADODB::FieldPtr pField = NULL;
hr = pFields->get_Item(_variant_t
("urn:schemas:mailheader:to"),&pField);
hr = pField->put_Value(_variant_t("your_email@mailcom"));
pField->Release();

hr = pFields->get_Item(_variant_t
("urn:schemas:mailheader:from"),&pField);
hr = pField->put_Value(_variant_t("nob...@gmail.com"));
pField->Release();

hr = pFields->get_Item(_variant_t
("urn:schemas:mailheader:sender"),&pField);
hr = pField->put_Value(_variant_t("Nobody"));
pField->Release();

hr = pFields->get_Item(_variant_t
("urn:schemas:mailheader:subject"),&pField);
hr = pField->put_Value(_variant_t("This is the Subject"));
pField->Release();
pField = NULL;

pFields->Update();
pFields->Release();
pFields = NULL;

IBodyPart* pBodyPart = NULL;
pBodyPart = pMessage->AddAttachment(L"C:\\your_path\\your_file.txt",
L"", L"");
pBodyPart->Release();
pBodyPart = NULL;

char sHTML[1024];
char sSystemPath[MAX_PATH], sPath[MAX_PATH];
GetSystemDirectory(sSystemPath, MAX_PATH);
wsprintf(sPath, "res://%s\\rasdlg.dll/#2/#1678", sSystemPath);
_variant_t vImage = sPath;
wsprintf(sHTML,
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">"
"<HTML>"
" <BODY>"
" <p><FONT COLOR=\"#663399\"><b>This is a Test</b></FONT></
p>"
" <p><IMG src=\"%s\"></p>"
" </BODY>"
"</HTML>",
(char *)(_bstr_t)vImage);

hr = pMessage->put_HTMLBody((_bstr_t)sHTML);
hr = pMessage->Send();
pMessage->Release();

CoUninitialize();
return 0;
}

Noixe

unread,
Feb 23, 2009, 12:38:02 PM2/23/09
to
"Christian" <cast...@club-internet.fr> ha scritto:

> I have seen that message, but where I could find:
> Mapi32.LIB
> Version.LIB
> Edkdebug.LIB...

They are in the Platform SDK

Then, I must search SDK of Extended MAPI?


>//#import "c:\windows\system32\CDOSys.DLL" no_namespace auto_search
>auto_rename named_guids

Mingw don't support that istruction.

However, I prefer use Extended MAPI.


Noixe

unread,
Feb 23, 2009, 1:02:02 PM2/23/09
to
"Noixe" <no...@TOGLIMIemail.it> ha scritto:

> Then, I must search SDK of Extended MAPI?

I can't find it.

I found only this wrapper of MAPI:
http://www.codeproject.com/KB/IP/CMapiEx.aspx

Have you a link?


Christian ASTOR

unread,
Feb 23, 2009, 3:30:50 PM2/23/09
to
Noixe wrote:

> Then, I must search SDK of Extended MAPI?

mapi32.lib, version.lib are standards libs (VS.NET or PSDK)
edkdebug.lib is an old lib (furnished with VC++ 6)
You can download its source at
http://www.microsoft.com/downloads/details.aspx?FamilyID=36a309c3-8c55-4476-8785-cafc59a2d075&DisplayLang=en

>>//#import "c:\windows\system32\CDOSys.DLL" no_namespace auto_search
>>auto_rename named_guids

> Mingw don't support that istruction.

You can generate interfaces definitions with OleView.

Leslie Milburn

unread,
Feb 25, 2009, 4:34:24 AM2/25/09
to

"Noixe" <no...@TOGLIMIemail.it> wrote in message
news:49a2df00$0$1114$4faf...@reader2.news.tin.it...

> "Christian" <cast...@club-internet.fr> ha scritto:
>
>> I have seen that message, but where I could find:
>> Mapi32.LIB
>> Version.LIB
>> Edkdebug.LIB...
>
> They are in the Platform SDK
>
> Then, I must search SDK of Extended MAPI?
>

Firstly, Extended MAPI will only work if the client machine has Outlook or
Exchange installed as the default Mail Client. If Outlook Express (or
WinMail) is the default mail Client then Extended MAPI does not apply and
you will have to use Simple MAPI.

Secondly, it is not recommended to statically link to the MAPI32.LIB as MAPI
is very fluid, for example you should no longer reference MAPI32.DLL but
instead use MAPISTUB.DLL (and its presence is Outlook version dependant) -
and the preferred method is to use LoadLibrary() and GetProcAddress() to get
a pointer to each function you are interested in.

Thirdly, if you do want to limit yourself to Extended Mapi/Outlook/Exchange
then there is a third party DLL available called Redemption which will most
likely make your life easier rather then you having to take a crash course
with the complicatd ins and outs of Extended MAPI.

HTH
Leslie.

0 new messages