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
> 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))
#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
> MAPILogon(u, "MyEmail", "MyPassword", f, u, lp);
This istruction is:
lh = MAPILogon(u, "MyEmail", "MyPassword", f, u, lp);
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;
}
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
Kellie.
> 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;
}
> 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
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;
}
> 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?
To reply, change domain to an adult feline.
> Is there a way to avoid that a windows of confirm is shown?
If you're talking about Outlook Dlg, yes, with Extended MAPI.
> 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?
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
> 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
Extended MAPI (MAPILogonEx() & MAPI_EXTENDED, ..., SubmitMessage())
>Extended MAPI (MAPILogonEx() & MAPI_EXTENDED, ..., SubmitMessage())
I found only information for Windows CE and Windows Mobile...
>Extended MAPI (MAPILogonEx() & MAPI_EXTENDED, ..., SubmitMessage())
Which header I must include?
http://www.google.com/search?q=ClickYes
>> 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?
Hi,
The header is Mapix.h as listed below.
>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
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
Sorry, the code above work! :-)
> 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
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())
> 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
> 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;
}
> 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.
> 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?
> 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.
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.