BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Send an automatic email

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
freddy mas
Guest





PostPosted: Thu May 10, 2007 8:11 am    Post subject: Send an automatic email Reply with quote



Hi,

My application is used to test the knowledge of some students (questions and multiple answers, etc.)
At the end of the test, I need my application to send a silent
email which contains the name and the score of the student
automatically

I googled around and I believe mailto and ShellExecute won't do it
Do you know if MAPI can do this? how?

Thanks
freddy
Back to top
David Ayre
Guest





PostPosted: Thu May 10, 2007 8:30 pm    Post subject: Re: Send an automatic email Reply with quote



"freddy mas" <nospam (AT) thanks (DOT) com> wrote:
Quote:

Hi,

My application is used to test the knowledge of some students (questions and multiple answers, etc.)
At the end of the test, I need my application to send a silent
email which contains the name and the score of the student
automatically

I googled around and I believe mailto and ShellExecute won't do it
Do you know if MAPI can do this? how?

Thanks
freddy

I use an Indy component. It work very well.

David
Back to top
freddy mas
Guest





PostPosted: Thu May 10, 2007 8:45 pm    Post subject: Re: Send an automatic email Reply with quote



David
can you please point me to the component (name)

thanks

"David Ayre" <davidcayre (AT) ntlworld (DOT) com> wrote:
Quote:

I use an Indy component. It work very well.

David
Back to top
Hans Galema
Guest





PostPosted: Thu May 10, 2007 9:24 pm    Post subject: Re: Send an automatic email Reply with quote

freddy mas wrote:

Quote:
can you please point me to the component (name)

TIdSMTP
Back to top
freddy mas
Guest





PostPosted: Thu May 10, 2007 11:21 pm    Post subject: Re: Send an automatic email Reply with quote

Indy is good but requires the settings hostname and user and password, where in my case I want
to make this transparent to the user (each user/student) has his/own login

Ideally, I want to use the user's default email settings (example Outlook) to send the email.
I tried MAPI with the code below, but I get the annoying messages prompted to the user which I cannot get rid of

- a program is trying to send an email on your behalf
- a program is trying to access e-mail addresses you have stored in Outlook MAPI Borland C++

sending the email should be automatic...nothing will be required from the user

Do you know how can I fix the code below to bypass the prompts?

Thanks and sorry for the long post

#include "Mapi.hpp"
#include <vector>
#include <memory>

HINSTANCE hMAPILib52 = 0;
LPMAPILOGON pMAPILogon52 = NULL;
LPMAPILOGOFF pMAPILogoff52 = NULL;
LPMAPISENDMAIL pMAPISendMail52 = NULL;
LPMAPIRESOLVENAME pMAPIResolveName52;
LPMAPIFREEBUFFER pMAPIFreeBuffer52=NULL;

void CleanupMAPIFunctions()
{
if( hMAPILib52 )
{
::FreeLibrary(hMAPILib52);
hMAPILib52 = NULL;
}

pMAPILogon52 = NULL;
pMAPISendMail52 = NULL;
pMAPILogoff52 = NULL;
pMAPIResolveName52 = NULL;
pMAPIFreeBuffer52 = NULL;
}

void InitMAPIFunctions()
{
hMAPILib52 = ::LoadLibrary("MAPI32.DLL");
if( !hMAPILib52 )
throw Exception("SendMailMessage:LoadLibrary(\"MAPI32.DLL\") failed");

try
{
pMAPILogon52 = (LPMAPILOGON) GetProcAddress (hMAPILib52,"MAPILogon");
if( !pMAPILogon52 )
throw Exception("SendMailMessage: GetProcAddress (hMAPILib52, \"MAPILogon\") failed");

pMAPISendMail52 = (LPMAPISENDMAIL) GetProcAddress (hMAPILib52, "MAPISendMail");
if( !pMAPISendMail52 )
throw Exception("SendMailMessage: GetProcAddress (hMAPILib52, \"MAPISendMail\") failed");

pMAPILogoff52 = (LPMAPILOGOFF) GetProcAddress (hMAPILib52, "MAPILogoff");
if( !pMAPILogoff52 )
throw Exception("SendMailMessage: GetProcAddress (hMAPILib52, \"Logoff\") failed");

pMAPIResolveName52 = (LPMAPIRESOLVENAME) GetProcAddress(hMAPILib52, "MAPIResolveName");
if( !pMAPIResolveName52 )
throw Exception("SendMailMessage: GetProcAddress (hMAPILib52, \"MAPIResolveName\") failed");

pMAPIFreeBuffer52 = (LPMAPIFREEBUFFER) GetProcAddress (hMAPILib52, "MAPIFreeBuffer");
if( !pMAPIFreeBuffer52 )
throw Exception("SendMailMessage: GetProcAddress (hMAPILib52, \"MAPIFreeBuffer\") failed");
}
catch(const Exception &)
{
CleanupMAPIFunctions();
throw;
}
}

struct RecipWrapper
{
MapiRecipDesc *Desc;
RecipWrapper() : Desc(NULL) {}
~RecipWrapper() { if(Desc) pMAPIFreeBuffer52(&Desc); }
};

bool SendMailMessage(TStrings *Recipients, const AnsiString &EmailBody)
{
LHANDLE hSession = NULL;
ULONG ulResult;
MapiMessage Message = {0};
MapiRecipDesc SenderInfo = {0};

try
{
if( !((Recipients) && (Recipients->Count > 0)) )
return "Cannot send mail: no recipients specified";

InitMAPIFunctions();
try
{
// Logon session
ulResult = pMAPILogon52(hSession, NULL, NULL, MAPI_LOGON_UI | MAPI_NEW_SESSION, 0, &hSession);
if( ulResult != SUCCESS_SUCCESS )
throw Exception("Cannot send mail: Logon failed");

try
{
SenderInfo.ulRecipClass = MAPI_ORIG;
SenderInfo.lpszName = "Sender";
SenderInfo.lpszAddress = "emailhere";

std::vector<RecipWrapper> ResolvedRecipients(Recipients->Count);
std::vector<MapiRecipDesc> RecipientArr(Recipients->Count);
memset(&RecipientArr[0], 0, sizeof(MapiRecipDesc)* Recipients->Count);

for(int i = 0; i < Recipients->Count; ++i)
{
ulResult = pMAPIResolveName52(hSession, 0, Recipients->Strings[i].c_str(), 0, 0, &ResolvedRecipients[i].Desc);
if( ulResult != SUCCESS_SUCCESS )
throw Exception("Cannot send mail: resolve name failed");

RecipientArr[i].ulRecipClass = MAPI_TO;
RecipientArr[i].lpszName = ResolvedRecipients[i].Desc->lpszName;
RecipientArr[i].lpszAddress = ResolvedRecipients[i].Desc->lpszAddress;
}

Message.nRecipCount = Recipients->Count;
Message.lpRecips = &RecipientArr[0];
Message.lpszSubject = "Subject";
Message.lpszNoteText = EmailBody.c_str();
Message.flFlags = MAPI_LOGON_UI;
Message.lpOriginator = &SenderInfo;

ulResult = pMAPISendMail20(hSession, 0, &Message, MAPI_LOGON_UI, 0); //this will make the email goes in silent mode - automatically

switch( ulResult )
{
case SUCCESS_SUCCESS:
break;

case MAPI_E_AMBIGUOUS_RECIPIENT:
throw Exception("Ambiguous Recipient E-mail Address");

case MAPI_E_ATTACHMENT_OPEN_FAILURE:
throw Exception("Error Opening Attachement");

case MAPI_E_BAD_RECIPTYPE:
throw Exception("Bad Recipient Type");

case MAPI_E_FAILURE:
throw Exception("E-mail Error");

case MAPI_E_INSUFFICIENT_MEMORY:
throw Exception("Insufficient Memroy");

case MAPI_E_LOGIN_FAILURE:
throw Exception("E-mail Login Failure");

case MAPI_E_TEXT_TOO_LARGE:
throw Exception("E-mail Text Too Large");

case MAPI_E_TOO_MANY_FILES:
throw Exception("Too Many Files");

case MAPI_E_TOO_MANY_RECIPIENTS:
throw Exception("Too Many Recipients");

case MAPI_E_UNKNOWN_RECIPIENT:
throw Exception("Unknown Recipient");

case MAPI_E_USER_ABORT:
throw Exception("Operation Aborted");

default:
throw Exception("Cannot send mail, Unknown Error");
}
}
__finally {
pMAPILogoff52(hSession, 0, 0, 0);
}
}
__finally {
CleanupMAPIFunctions();
}

return true;
}
catch(const Exception &E)
{
ShowMessage(E.Message);
return false;
}
catch(...)
{
ShowMessage("Unknown error");
return false;
}

}

void __fastcall TForm52::Button6Click(TObject *Sender)
{
AnsiString Email;
std::auto_ptr<TStringList> Recipients(new TStringList);

Email = "johndoe (AT) domainname (DOT) com";
Recipients->Add(Email);
SendMailMessage(Recipients.get(), RichEdit2->Text); //Richedit2 contains the name and the score

}
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri May 11, 2007 1:07 am    Post subject: Re: Send an automatic email Reply with quote

"freddy mas" <nospam (AT) thanks (DOT) com> wrote in message
news:46436299$1 (AT) newsgroups (DOT) borland.com...

Quote:
Indy is good but requires the settings hostname and user and
password,
where in my case I want to make this transparent to the user (each
user/student) has his/own login

There is no unified way to do that. Each email program has to
maintain its own local settings. There is no central storage for that
kind of info from one program to another.

Quote:
Ideally, I want to use the user's default email settings (example
Outlook)
to send the email.

Then you will have to use MAPI, assuming the user has a MAPI-enabled
program installed to begin with.

Quote:
I tried MAPI with the code below, but I get the annoying messages
prompted to the user which I cannot get rid of

Sounds like the user has its security settings turned up. There is
nothing you can do about that.

Quote:
sending the email should be automatic...nothing will be required
from the user


Then you are likely going to be out of luck if you take the MAPI
approach.

Quote:
Do you know how can I fix the code below to bypass the prompts?

You can't bypass them if they are turned on. You will likely have to
go the SMTP/IMAP approach afterall. Then you are in full control of
everything.

Quote:
ulResult = pMAPILogon52(hSession, NULL, NULL, MAPI_LOGON_UI |
MAPI_NEW_SESSION, 0, &hSession);


You are passing an incorrect handle in the first parameter. That
parameter does not expect a session handle. It expects a window
handle instead.

You are also not specifying any profile name to log into, either. You
can grab that value from the Registry:

NT+:
HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows
Messaging Subsystem\Profiles

Win9x/Me:
HKCU\Software\Microsoft\Windows Messaging Subsystem\Profiles

Look at the "DefaultProfile" value. If it does not exist, then you
should at least specify a temporary profile name when calling
MAPILogon().

Quote:
Message.flFlags = MAPI_LOGON_UI;

MAPI_LOGON_UI is not a valid message flag.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.