Page 1 of 1
OLE & Outlook (How to translate this Delphi code)
Posted: Wed Jun 28, 2006 6:58 pm
by Maurilio Viana
Does anybody can help me to translate this Delphi code that send mail using Outlook to FW/xHarbour?
Code: Select all
procedure TForm1.Button1Click(Sender: TObject);
var
Outlook : OleVariant;
vMailItem: variant;
begin
try
Outlook := GetActiveOleObject('Outlook.Application');
except
Outlook := CreateOleObject('Outlook.Application');
end;
vMailItem := Outlook.CreateItem(olMailItem);
vMailItem.Recipients.Add('fulando@mundo.com.br'); // 1st recipient
vMailItem.Recipients.Add('ciclano@mundo.com.br'); // 2dn recipient
vMailItem.Subject := 'teste de email'; // subject
vMailItem.Body := 'Este é um teste'; // mail body
vMailItem.Attachments.Add('C:\temp\arq.txt'); // Attached file
vMailItem.Send;
VarClear(Outlook);
end;
Posted: Wed Jun 28, 2006 9:05 pm
by James Bott
MAPI Mail Syntax:
DEFINE MAIL [ <oMail> ];
[ SUBJECT <cSubject> ];
[ TEXT <cText> ];
[ TYPE <cType> ];
[ DATE <dDate> ];
[ TIME <cTime> ];
[ CONVERSATION <cConversation> ]:
[ RECEIPT ];
[ FROM USER ];
[ FILES <cFilename1> ,<cDescript1>, <cFilenameN>, <cDescriptN> ] ];
[ ORIGIN <cOrigin> [ <cOriginAddress> ] ];
[ TO <cTarget1>, [ <cTargetAddress1> ] [ <cTargetN> [ <cTargetAddressN> ] ] ];
If the attachment file is html (has a .htm or .html extension) and you do not specify any TEXT (a message) then the file will appear in the message area.
ERRORS
If you specify a TO and the parameter is blank and you are using the FROM USER clause, the message compose box will not appear. Likewise if the filename is invalid or not found. It is best to use an error trap:
DEFINE MAIL...
ACTIVATE MAIL...
IF oMail:nRetCode!=0
MsgAlert("The message could not be sent due to an error."+CRLF+;
"MAPI error code: "+ltrim(str(oMail:nRetCode)),"Alert")
ENDIF
Posted: Wed Jun 28, 2006 10:21 pm
by Gale FORd
Hi James,
The only problem with using mapi is that the later versions of outlook popup a security warning message if you want to send without user intervention.
This Microsoft "Feature" is really irritating if you want an automatic sending feature.
I use Ole or pop3 for automatic sending.
Posted: Thu Jun 29, 2006 11:19 am
by Maurilio Viana
James Bott wrote:MAPI Mail Syntax:
DEFINE MAIL [ <oMail> ];
(...)
Thanks a lot, James
Regards
Maurilio
Posted: Thu Jun 29, 2006 11:20 am
by Maurilio Viana
Gale FORd wrote:
I use Ole or pop3 for automatic sending.
Gale, can you post a short OLE sample?
Thanks!
Maurilio
Posted: Thu Jun 29, 2006 2:01 pm
by Randal
Gale,
You know you can turn that off in Outlook so you don't get the message.
Regards,
Randal Ferguson
Posted: Thu Jun 29, 2006 2:08 pm
by Gale FORd
Randal,
I have looked into the problem but could not find a simple solution. Microsoft has something called extended mapi that does not have that security warning but I have not dug into it yet.
Do you have a solution for Outlook 2003?
Posted: Thu Jun 29, 2006 3:37 pm
by Gale FORd
Maurilo,
I am sending sample.
I have not used Outlook with ole for some time so when I tested this sample I found that Outlook security warning prompt came up so this may or not work for you. I have a utility installed to overide the security warning when necessary but to install this utility on all my users machines is not practical.
FUNCTION SendMail()
LOCAL oOutLook,;
oMailItem,;
oRecip,;
oAttach
/* creating the OLE object */
TRY
oOutLook := CreateObject( "Outlook.Application" )
CATCH
Alert( "ERROR! Outlook not avialable." ) /// [" + Ole2TxtError()+ "]" )
RETURN
END
oMailItem := oOutLook:CreateItem( 0 )
//Recipients
oRecip := oMailItem:Recipients()
oRecip:Add( "
gale.ford@wwrowland.com" )
// Subject and body
oMailItem:Subject := "Ole Email Message"
oMailItem:Body := "This is the message body"
// Attachments
oAttach := oMailItem:Attachments()
//oAttach:Add( "g:\dispatch\who.bat" )
oAttach:Add( "c:\config.sys" )
oMailItem:Send()
/* Destroy all the created OLE objects */
oRecip := nil
oAttach := nil
oMailItem := nil
oOutLook := nil
RETURN NIL
Posted: Thu Jun 29, 2006 4:44 pm
by James Bott
Gale,
As Randal says, I think you can turn off the warning. I don't have a copy of Outlook here, but in Outlook Express you go to Tools-Options, select the Security tab, and there is a checkbox labled "Warn me if other applications try to send mail as me." Just uncheck it.
James
Posted: Thu Jun 29, 2006 4:55 pm
by Gale FORd
James,
Outlook has a Security tab but there is no setting related to that warning.
I have looked into this and it is not that simple.
I recommend tsmtp() to send email quietly. Tmail() works great if you use "from user" option but for mass mailing or unattended operation I cannot recommend it.
Posted: Thu Jun 29, 2006 5:15 pm
by James Bott
Gale,
>but for mass mailing or unattended operation I cannot recommend it.
I agree with you on that. I would use MAPI only for occasional emails when the user may want or need to enter the address and/or when they want to retain a copy of the message in their MAPI client app.
For mass emailing SMTP is the only feasible solution.
Microsoft may have eliminated the ability to turn off the warning in later versions of Outlook since it really is a security risk to have it turned off.
James
Posted: Thu Jun 29, 2006 6:03 pm
by tnhoe
Outlook Redemption works around limitations imposed by the Outlook Security Patch :-
http://www.dimastr.com/redemption/