OLE & Outlook (How to translate this Delphi code)

Post Reply
User avatar
Maurilio Viana
Posts: 252
Joined: Tue Oct 25, 2005 2:48 pm
Location: Garça/Garza/Heron City - Brazil
Contact:

OLE & Outlook (How to translate this Delphi code)

Post 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;
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
Gale FORd
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston
Contact:

Post 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.
User avatar
Maurilio Viana
Posts: 252
Joined: Tue Oct 25, 2005 2:48 pm
Location: Garça/Garza/Heron City - Brazil
Contact:

Post by Maurilio Viana »

James Bott wrote:MAPI Mail Syntax:

DEFINE MAIL [ <oMail> ];
(...)
Thanks a lot, James

Regards
Maurilio
User avatar
Maurilio Viana
Posts: 252
Joined: Tue Oct 25, 2005 2:48 pm
Location: Garça/Garza/Heron City - Brazil
Contact:

Post by Maurilio Viana »

Gale FORd wrote: I use Ole or pop3 for automatic sending.
Gale, can you post a short OLE sample?

Thanks!
Maurilio
Randal
Posts: 250
Joined: Mon Oct 24, 2005 8:04 pm

Post by Randal »

Gale,

You know you can turn that off in Outlook so you don't get the message.

Regards,
Randal Ferguson
Gale FORd
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston
Contact:

Post 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?
Gale FORd
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston
Contact:

Post 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
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
Gale FORd
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston
Contact:

Post 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.
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Post 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
User avatar
tnhoe
Posts: 83
Joined: Tue Nov 08, 2005 11:09 am
Location: Malaysia
Contact:

Post by tnhoe »

Outlook Redemption works around limitations imposed by the Outlook Security Patch :-

http://www.dimastr.com/redemption/
Regards

Hoe, email: easywin3@yahoo.com
Post Reply