Article View: borland.public.cppbuilder.nativeapi
Article #13702Re: Send a mail with attachment
From: Remy Lebeau (TeamB)
Date: Mon, 29 Sep 2003 10:01
Date: Mon, 29 Sep 2003 10:01
157 lines
6328 bytes
6328 bytes
"Rodolfo Frino" <rodolfofrino@yahoo.com> wrote in message
news:3f77e4bb@newsgroups.borland.com...
> Can you post an example of the code you refer to?
Here is an adaptation of your OE code. I have tested this with my OE, it
works for me:
#include <utilcls.h>
#include <oleacc.h>
#include <mshtml.h>
#define CTRLID_DOCHOST 1000
#define CTRLID_ENVELOPE 1015
#define CTRLID_REBAR 1999
#define CTRLID_TOOLBAR 2101
#define CTRLID_CC 1003
#define CTRLID_SUBJECT 1004
HWND __fastcall FindNewMessageWnd()
{
HWND hWnd = FindWindow("ATH_Note", "New Message");
if( !hWnd )
{
// wait for the window to appear, up to 5 seconds
DWORD dw = 0;
do
{
Sleep(500);
hWnd = FindWindow("ATH_Note", "New Message");
}
while( (hWnd == NULL) && (++dw < 10) );
}
return hWnd;
}
HWND __fastcall FindReBarWnd(HWND hWnd)
{
// find the ReBar that hosts the Toolbar containing the
// "Send" button
HWND hSizableReBarWnd = GetDlgItem(hWnd, 0);
if( hSizableReBarWnd )
return GetDlgItem(hSizableReBarWnd, CTRLID_REBAR);
return NULL;
}
HWND __fastcall FindMainTextWnd(HWND hWnd)
{
if( hWnd )
{
// obtain the HWND of the Internet Explorer instance
// that is used for the main text of the message
HWND hDocHost = GetDlgItem(hWnd, CTRLID_DOCHOST);
if( hDocHost )
{
HWND hMimeEdit = GetWindow(hDocHost, GW_CHILD);
if( hMimeEdit )
return GetWindow(hMimeEdit, GW_CHILD);
}
}
return NULL;
}
void __fastcall SetEditText(HWND hWnd, const AnsiString &asText)
{
// The edit fields are RichEdit v2.0 instances, which apparently
// does not handle WM_SETTEXT correctly when sent from
// another process, so post the individual characters instead.
if( hWnd )
{
int iLength = asText.Length();
for(int x = 1; x <= iLength(); ++x)
SendMessage(hWnd, WM_CHAR, (WPARAM)asText[x], 0);
}
}
void __fastcall SetMainMsgText(HWND hWnd, const AnsiString &asText)
{
// Since the main text is an Internet Explorer instance, use
// the DHTML DOM to modify the text. Use ObjectFromLresult()
// to help get the IHTMLDocument2 interface from the HWND.
static const UINT uMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");
if( hWnd )
{
HINSTANCE hDll = LoadLibrary("OLEACC.DLL");
if( hDll )
{
LPFNOBJECTFROMLRESULT pfObjectFromLresult =
(LPFNOBJECTFROMLRESULT)GetProcAddress(hDll, "ObjectFromLresult");
if( pfObjectFromLresult )
{
TInitOle ole;
TComInterface<IHTMLDocument2> spDoc;
LRESULT lRes = 0;
SendMessageTimeout(hWnd, uMsg, 0, 0, SMTO_ABORTIFHUNG,
1000, (DWORD*)&lRes);
if( SUCCEEDED(pfObjectFromLresult(lRes,
IID_IHTMLDocument2, 0, (LPVOID*)&spDoc)) )
{
TComInterface<IHTMLElement> spBody;
if( SUCCEEDED(spDoc->get_body(&spBody)) )
spBody->put_innerText(WideString(asText));
}
}
FreeLibrary(hDll);
}
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( ShellExecute(NULL, "open", "mailto:rodolfofrino@yahoo.com", 0,
0, SW_SHOWNORMAL) > (HINSTANCE)32 )
{
// find the new message window
HWND hNewMsgWnd = FindNewMessageWnd();
if( hNewMsgWnd )
{
// find the envelope containing the header fileds
HWND hEnvelope = GetDlgItem(hNewMsgWnd, CTRLID_ENVELOPE);
if( hEnvelope )
{
// fill in the desired headers of the message
SetEditText(GetDlgItem(hEnvelope, CTRLID_CC),
"name2@hotmail.com");
SetEditText(GetDlgItem(hEnvelope, CTRLID_SUBJECT), "This
is the subject");
}
// set the main text of the message
SetMainMsgText(FindMainTextWnd(hNewMsgWnd), "Hello E-World,
this is the body of my e-mail
(yahoo)\n\nname@hotmail.com\n\n!@#$%^&*()_+{}|:\"<>?\n\"Bye Bye\"!");
// submit the message by simlating the "Send" button being
clicked
HWND hReBar = FindReBarWnd(hNewMsgWnd);
if( hReBar )
SendMessage(hReBar, WM_COMMAND, MAKEWPARAM(0x9DDB, 0),
(LPARAM)GetDlgItem(hReBar, CTRLID_TOOLBAR));
}
}
}
Gambit
Technical Details
Message-ID:
<3f7802c2$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3f70794d@newsgroups.borland.com> <3f7545de@newsgroups.borland.com> <3f763170$1@newsgroups.borland.com> <3f770460@newsgroups.borland.com> <3f774a65@newsgroups.borland.com> <3f77a4c8@newsgroups.borland.com> <3f77b78b$1@newsgroups.borland.com> <3f77e4bb@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,23d9e1242f297072 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-09-29 03:09:11 PST From: "Remy Lebeau \(TeamB\)" <gambit47@yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f70794d@newsgroups.borland.com> <3f7545de@newsgroups.borland.com> <3f763170$1@newsgroups.borland.com> <3f770460@newsgroups.borland.com> <3f774a65@newsgroups.borland.com> <3f77a4c8@newsgroups.borland.com> <3f77b78b$1@newsgroups.borland.com> <3f77e4bb@newsgroups.borland.com> Subject: Re: Send a mail with attachment Date: Mon, 29 Sep 2003 03:01:57 -0700 Lines: 160 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 67.23.78.204 Message-ID: <3f7802c2$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1064829634 67.23.78.204 (29 Sep 2003 03:00:34 -0700) Path: arc1-ca.pugleaf.net!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!news.tele.dk!news.tele.dk!small.news.tele.dk!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail