Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Steven.

Asked: June 16, 2000 - 4:45 pm UTC

Last updated: July 19, 2008 - 6:38 am UTC

Version: v7.3.2

Viewed 1000+ times

You Asked

How to trigger email sending to clients while using Forms of Dev 2K? Or from database?

Thanks a lot.

Steven

and Tom said...

With 7.3.2 which is the database you have, sending email is "hard" from the database. It can be done, but it takes an amount of work.

With D2k, we can use ole automation to do this and since you are willing to do it from there -- here we go.

This is a support note on this very topic:

Article-ID: <Note:61736.1>

Exchange using OLE2
Keywords: DEVELOPER/2000;FORMS;REPORTS;FAX;EMAIL;EXCHANGE;OLE;OLE2
_________________________________________________________________Programmatically sending a Fax/Email
==========================================================


INTRODUCTION:
-------------

This bulletin explains how to programmatically send a fax/email message from a Forms/Reports application via Microsoft Exchange without any kind of user interaction. It shows the general usage of the 'Mail' package as well as a fully coded Forms sample application.

The concept of OLE (Object Linking and Embedding) automation is used to control the OLE server application (Microsoft Exchange) using the client application. The client in this case may be a Developer/2000 Forms or Reports application. It uses the objects and methods exposed by the OLE Messaging Library which are
much more robust than the MSMAPI OCX controls and allow access to many more MAPI properties.

Oracle provides support for OLE automation in its applications by means of the OLE2 built-in package. This package contains object types and built-ins for creating and manipulating the OLE objects. Some of these built-ins for e.g. OLE2.create_obj, OLE2.invoke, OLE2.set_property have been extensively used in
the code.


GENERAL USAGE:
--------------

The Mail package contains three procedures:

1. Procedure Mail_pkg.logon( profile IN varchar2 default NULL);
----------
Use this procedure to logon to the MS Exchange mail client. The procedure takes a character argument which specifies the Exchange Profile to use for logon. Passing a NULL argument to the logon procedure brings up a dialog box which asks you to choose a profile from a list of valid profiles or create a new one if it doesn't exist.


2. Procedure Mail_pkg.send(
--------- Recipient IN varchar2,
Subject IN varchar2 default NULL,
Text IN varchar2 default NULL,
Attachment IN varchar2 default NULL
);

This is the procedure that actually sends the message and attachments, if any, to the recipient. The recipient may be specified directly as a valid email address or as an alias defined in the address book. If the message is intended for a fax recipient then a valid alias must be used that is defined as a fax address in the address book.


3. Procedure Mail_pkg.logoff;
-----------

This procedure closes the Exchange session and deallocates the resources used by the OLE automation objects.



SAMPLE FORMS APPLICATION:
-------------------------

1. Create the Mail Package using the following two Program Units:

(a) Mail Package Spec
(b) Mail Package Body


Mail Package Spec:
------------------

PACKAGE Mail_pkg IS

session OLE2.OBJ_TYPE; /* OLE object handle */
args OLE2.LIST_TYPE; /* handle to OLE argument list */

procedure logon( Profile IN varchar2 default NULL );

procedure logoff;

procedure send( Recp IN varchar2,
Subject IN varchar2,
Text IN varchar2,
Attch IN varchar2
);

END;



Mail Package Body:
------------------

PACKAGE BODY Mail_pkg IS

session_outbox OLE2.OBJ_TYPE;
session_outbox_messages OLE2.OBJ_TYPE;
message1 OLE2.OBJ_TYPE;
msg_recp OLE2.OBJ_TYPE;
recipient OLE2.OBJ_TYPE;
msg_attch OLE2.OBJ_TYPE;
attachment OLE2.OBJ_TYPE;


procedure logon( Profile IN varchar2 default NULL )is
Begin

session := ole2.create_obj('mapi.session');
/* create the session object */
args := ole2.create_arglist;

ole2.add_arg(args,Profile);/* Specify a valid profile name */
ole2.invoke(session,'Logon',args);
/* to avoid the logon dialog box */
ole2.destroy_arglist(args);

End;


procedure logoff is
Begin

ole2.invoke(session,'Logoff');
/* Logoff the session and deallocate the */


/* resources for all the OLE objects */

ole2.release_obj(session);
ole2.release_obj(session_outbox);
ole2.release_obj(session_outbox_messages);
ole2.release_obj(message1);
ole2.release_obj(msg_recp);
ole2.release_obj(recipient);
ole2.release_obj(msg_attch);
ole2.release_obj(attachment);

End;



procedure send( Recp IN varchar2,
Subject IN varchar2,
Text IN varchar2,
Attch IN varchar2
)is
Begin

/* Add a new object message1 to the outbox */

session_outbox := ole2.get_obj_property(session,'outbox');
session_outbox_messages := ole2.get_obj_property(session_outbox,'messages');
message1 := ole2.invoke_obj(session_outbox_messages,'Add');

ole2.set_property(message1,'subject',Subject);
ole2.set_property(message1,'text',Text);

/* Add a recipient object to the message1.Recipients collection */

msg_recp := ole2.get_obj_property(message1,'Recipients');
recipient := ole2.invoke_obj(msg_recp,'add') ;

ole2.set_property(recipient,'name',Recp);
ole2.set_property(recipient,'type',1);
ole2.invoke(recipient,'resolve');

/* Add an attachment object to the message1.Attachments collection */

msg_attch := ole2.get_obj_property(message1,'Attachments');
attachment := ole2.invoke_obj(msg_attch,'add') ;

ole2.set_property(attachment,'name',Attch);
ole2.set_property(attachment,'position',0);
ole2.set_property(attachment,'type',1); /* 1 => MAPI File Data */
ole2.set_property(attachment,'source',Attch);

/* Read the attachment from the file */

args := ole2.create_arglist;
ole2.add_arg(args,Attch);
ole2.invoke(attachment,'ReadFromFile',args);
ole2.destroy_arglist(args);

args := ole2.create_arglist;
ole2.add_arg(args,1); /* 1 => save copy */
ole2.add_arg(args,0); /* 0 => no dialog */

/* Send the message without any dialog box, saving a copy in the Outbox */

ole2.invoke(message1,'Send',args);

ole2.destroy_arglist(args);
message('Message successfully sent');

End;

END;




2. Create a block called MAPIOLE with the following canvas layout:

|-------------------------------------------------------------|
| |
| Exchange Profile: |====================| |
| |
| To: |============================| |
| |
| Subject: |============================| |
| |
| Message: |============================| |
| | | |
| | | |
| | | |
| | | |
| | | |
| |============================| |
| |-----| |
| Attachment: |============================| |SEND | |
| |-----| |
|-------------------------------------------------------------|



The layout contains 5 text-itmes:

- Profile
- To
- Subject
- Message (multiline functional property set to true)
- Attach

and a 'Send' button with the following WHEN-BUTTON-PRESSED trigger:
---------------------------
mail_pkg.logon(:profile);
mail_pkg.send(:to,:subject,:message,:attch);
mail_pkg.logoff;



CONCLUSION:
-----------

This bulletin explained how to create a sample Forms application which can send a fax/email using OLE automation. The same concept/code can be extended to programmatically fax/email an Oracle Report without any user interaction. In order to fax a report, first generate the report output as a PDF file and then
send this to the fax recipient as an attachment using the same Mail Package.




Rating

  (27 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, March 29, 2001 - 5:13 am UTC


The idea has helped us with the project

Sharath Shivanna, May 15, 2001 - 3:46 am UTC

This article has really helped us take a leap in our project
Thanks a lot

Doubt

Murali, June 14, 2001 - 7:30 am UTC

Hi tom,

i tried with the same but it is not working. when i try to create a sesison session id is coming as 0. after that it is failing to get the object properties. can you tell me what is the solutions for the same.

Thanks in advance
Murali

Problem

Sayantan, June 20, 2001 - 12:32 am UTC

It is working perfectly with Outlook 97 but errors are raised when the same thing is used with Outlook 2000.
Any soultion?

And finally it Works .... Yeh..!!!!!

Praveen Kumar Talla [Pune], July 05, 2001 - 3:51 am UTC

This is very facinating to send mail by using forms. And finally it works very well. Thanx for the support. Thanks Exclusive team of Oracle online @ Ask Tom.

Error

Marco, October 23, 2001 - 10:16 am UTC

Sorry, but this code gives me a

ORA - 305500

Can you help me !?

Please send me some solution

Thanks in advance

E-mail from D2K Forms

Harish Betrabet, October 31, 2001 - 4:16 am UTC

The code runs excellently with Forms 6i and Microsoft Outlook 97 (which is part of the MS-Office 97 suite of applications). We configured Micosoft Outlook 97 to connect to our Lotus Notes e-mail server using POP3.
However, the same code gave an error ORA - 305500
when we tried using Outlook Express 5 instead of Microsoft Outlook 97.

Tom Kyte
October 31, 2001 - 7:50 am UTC

You gotta ask MS to stop changing their "standards" and their interfaces. You need to find out from MS what the proper, magic sequence of events is for Outlook Express.

A reader, November 01, 2001 - 12:54 am UTC

i think all applicaton development must tie the windows integration not window tie the application as i like oracle but oracle developing must upgrade for this point i meet with a lot of developer and they think that

Tom Kyte
November 01, 2001 - 8:27 am UTC

Huh? that makes no sense. No clue what point you are trying to make there.

mapi session

ceylan kortan, November 16, 2001 - 5:02 am UTC

hi, I tried article-id = 61736.1. I wanted to send txt file by email. but couldn't. my attachement file : c:\hello.txt and send it. when I entered inbox, attachement file appeared "Untitled".

cutted source is below
-------------------------------------
msg_attch := OLE2.GET_OBJ_PROPERTY(message1, 'Attachments');
attachment := OLE2.INVOKE_OBJ(msg_attch, 'add') ;
OLE2.SET_PROPERTY(attachment, 'name', Attch);
OLE2.SET_PROPERTY(attachment, 'position', 0);
OLE2.SET_PROPERTY(attachment, 'type', 1);
OLE2.SET_PROPERTY(attachment, 'source', 'C:\hello.txt');

what can I do?

many thanks.



mapi session

ceylan kortan, November 16, 2001 - 5:05 am UTC

hi, I tried article-id = 61736.1. I wanted to send txt file by email. but couldn't. my attachement file : c:\hello.txt and send it. when I entered inbox, attachement file appeared "Untitled".

cutted source is below
-------------------------------------
msg_attch := OLE2.GET_OBJ_PROPERTY(message1, 'Attachments');
attachment := OLE2.INVOKE_OBJ(msg_attch, 'add') ;
OLE2.SET_PROPERTY(attachment, 'name', Attch);
OLE2.SET_PROPERTY(attachment, 'position', 0);
OLE2.SET_PROPERTY(attachment, 'type', 1);
OLE2.SET_PROPERTY(attachment, 'source', 'C:\hello.txt');

what can I do?

many thanks.



Solution to Outlook 2000 problem

Tamas zecsyu, December 14, 2001 - 7:27 am UTC

There is an article on metalink on the Outlook 2000 problem. Look for it under Forms white papers (With a title 'Cracking Outlook' or something similar).

Tamas Szecsy

Senior Software Engineer

Maneesh Kholay, April 23, 2002 - 7:42 am UTC

If I am runing the form in GUI MODE it is working fine,
but when i run the same for on web server it is gives Error 305500 non-oracle exception..

As we have created a single profile on server side and setting it as default in the form then also it is not working

How to solve this problem...
please help me out

Thanks

Trying with MS. Office XP

Fancy, May 01, 2002 - 4:51 am UTC

I was trying to send mail using Outlook Express 6 and it did not work. Maybe, its because i didn't know what to put in the 'Profile' field. Please let me know. Will it work with this version? Thanks.

Tom Kyte
May 01, 2002 - 7:23 am UTC

As I say from time to time on this question:


You gotta ask MS to stop changing their "standards" and their interfaces. You
need to find out from MS what the proper, magic sequence of events is for
Outlook Express.




If you want to do this (send email) reliably, without having to change it, tweak it for each and every little service pack and upgrade -- suggest you do it in the database. Search for UTL_SMTP. I haven't had to change my code -- in forever.

Download Sample Form

Sikandar Hayat Awan, June 16, 2002 - 2:45 am UTC

I had placed a sample form to send email from forms "with attachements" to facilitate the oracle developers. You may download the sample from,

</code> http://ourworld.compuserve.com/homepages/peter_koletzke/tip.htm#formstips1 <code>




Error

debjit, July 17, 2002 - 9:59 am UTC

Error Ora - 305500 is coming. How to solve the problem.

Tom Kyte
July 17, 2002 - 1:53 pm UTC

search through this for 305500 and you'll see my answer. You are hitting a protocol error -- MS changed the interface - you'll need to figure it out from that end (i don't even have a windows machine anymore to play with)

eMAIL CLIENT

N, July 18, 2002 - 12:22 am UTC

Hi Tom,
In most of the discussion we talk about D2k and mail client outlook/outlook express (MS prduct),.
What about other client like Eudora and other email...
Will it work with them also or not?
Thanks
N

Tom Kyte
July 18, 2002 - 8:30 am UTC

Don't know, are they OLE enabled?

Me -- I would just be using UTL_SMTP or JavaMail in the database and not even bothering with trying to run an email client. It is by far the "hard way" to do it.

question

amit karkhanis, January 31, 2003 - 1:48 am UTC

will this be machine specific

Tom Kyte
January 31, 2003 - 7:50 am UTC

no, it'll be very specific to the version of MS software you have to talk to however. they like to change things -- as you should be able to see from some of the other followups.

accepting multiple addresses

Sijo, April 21, 2004 - 7:37 am UTC

HI Tom,
This mail_pkg is defintely very useful.
Now if i want to send mails to mutiple ids i went on typing
eg a@.com,b@.com in the "to" text item but it is not sending to the recepient.
i tried by giving ; as the separator but didn't worked



Tom Kyte
April 21, 2004 - 8:13 pm UTC

sorry, not an ole programmer at all, not even a teeny bit (i can hardly spell it)...

if you want to do it from the database, we can do that easily, search for utl_smtp on this site for tons of examples.

mail using ip

Debasish Ghosh, March 08, 2006 - 11:59 pm UTC

This mail_pkg is defintely very useful. & its works fine thanks for that.
Now i want to send mails through lotus server address directly, not via MS Outlook or Outlook express.
actully I adon't want to configure ms outlook or outlook express in client machine, forms send mail directly to lotus server mailbox address directly.
thanks for advance
DG


Tom Kyte
March 09, 2006 - 1:02 pm UTC

go for it?




Email through D2K

M.P.Kiran Kumar, April 19, 2006 - 12:37 pm UTC

Hi,

I am running this pkg to send email through d2k, I am getting Ora - 305500 error, as per your suggestion I tried searching in this site but I could not found any solution, pls provide me the solution for this problem


Regards
Kiran

Tom Kyte
April 19, 2006 - 1:00 pm UTC

please try out the forums on otn.oracle.com, I haven't used forms since march, 1995 (been quite a long while)

send mails from D2k

munira, June 15, 2006 - 5:23 am UTC

It works fine for one receipient. but i want to send it to multiple receipiants, Pls help me.

send mails from D2k

munira anwar, June 18, 2006 - 5:16 am UTC

now i tried the code given in your sample form. it worked perfectly well when i run the form locally. but when i run the form from application server i get error ora-305500
i am using forms 9i and oracle 9i applicattion server.

and also how can we avoid the message
" A program is trying to automatically send e-mail on your behalf .Do you want to allow this?????

waiting for ur reply


Tom Kyte
June 18, 2006 - 12:18 pm UTC

"ur" isn't here? Might that be a close friend of "U" - they are frequently asked for.

In any case - it has been over a decade since I last touched forms - suggest you try the discussion forums on otn.oracle.com, there is one just for forms related questions.

SENDINF MAIL FROM D2K

munira anwar, June 18, 2006 - 6:22 am UTC

BELOW IS TEH CODE I DID IN MY FORM TO SEND MAILS FROM ORACLE FORM 9i. this works fine when i run the form locally. but when i run teh form from oracle 9i application server i get error

ORA-305500 when i press the send button

what is the reason???? what change do i have to make in my code.???

please help

Mail Package Spec:
------------------

PACKAGE Mail_pkg IS

session OLE2.OBJ_TYPE; /* OLE object handle */
args OLE2.LIST_TYPE; /* handle to OLE argument list */

procedure logon( Profile IN varchar2 default NULL );

procedure logoff;

procedure send( Recp IN varchar2,
Subject IN varchar2,
Text IN varchar2,
Attch IN varchar2
);

END;



Mail Package Body:
------------------

PACKAGE BODY Mail_pkg IS

session_outbox OLE2.OBJ_TYPE;
session_outbox_messages OLE2.OBJ_TYPE;
message1 OLE2.OBJ_TYPE;
msg_recp OLE2.OBJ_TYPE;
recipient OLE2.OBJ_TYPE;
msg_attch OLE2.OBJ_TYPE;
attachment OLE2.OBJ_TYPE;


procedure logon( Profile IN varchar2 default NULL )is
Begin

session := ole2.create_obj('mapi.session');
/* create the session object */
args := ole2.create_arglist;

ole2.add_arg(args,Profile);/* Specify a valid profile name */
ole2.invoke(session,'Logon',args);
/* to avoid the logon dialog box */
ole2.destroy_arglist(args);

End;


procedure logoff is
Begin

ole2.invoke(session,'Logoff');
/* Logoff the session and deallocate the */


/* resources for all the OLE objects */

ole2.release_obj(session);
ole2.release_obj(session_outbox);
ole2.release_obj(session_outbox_messages);
ole2.release_obj(message1);
ole2.release_obj(msg_recp);
ole2.release_obj(recipient);
ole2.release_obj(msg_attch);
ole2.release_obj(attachment);

End;



procedure send( Recp IN varchar2,
Subject IN varchar2,
Text IN varchar2,
Attch IN varchar2
)is
Begin

/* Add a new object message1 to the outbox */

session_outbox := ole2.get_obj_property(session,'outbox');
session_outbox_messages := ole2.get_obj_property(session_outbox,'messages');
message1 := ole2.invoke_obj(session_outbox_messages,'Add');

ole2.set_property(message1,'subject',Subject);
ole2.set_property(message1,'text',Text);

/* Add a recipient object to the message1.Recipients collection */

msg_recp := ole2.get_obj_property(message1,'Recipients');
recipient := ole2.invoke_obj(msg_recp,'add') ;

ole2.set_property(recipient,'name',Recp);
ole2.set_property(recipient,'type',1);
ole2.invoke(recipient,'resolve');

/* Add an attachment object to the message1.Attachments collection */

msg_attch := ole2.get_obj_property(message1,'Attachments');
attachment := ole2.invoke_obj(msg_attch,'add') ;

ole2.set_property(attachment,'name',Attch);
ole2.set_property(attachment,'position',0);
ole2.set_property(attachment,'type',1); /* 1 => MAPI File Data */
ole2.set_property(attachment,'source',Attch);

/* Read the attachment from the file */

args := ole2.create_arglist;
ole2.add_arg(args,Attch);
ole2.invoke(attachment,'ReadFromFile',args);
ole2.destroy_arglist(args);

args := ole2.create_arglist;
ole2.add_arg(args,1); /* 1 => save copy */
ole2.add_arg(args,0); /* 0 => no dialog */

/* Send the message without any dialog box, saving a copy in the Outbox */

ole2.invoke(message1,'Send',args);

ole2.destroy_arglist(args);
message('Message successfully sent');

End;

END;




2. Create a block called MAPIOLE with the following canvas layout:

|-------------------------------------------------------------|
| |
| Exchange Profile: |====================| |
| |
| To: |============================| |
| |
| Subject: |============================| |
| |
| Message: |============================| |
| | | |
| | | |
| | | |
| | | |
| | | |
| |============================| |
| |-----| |
| Attachment: |============================| |SEND | |
| |-----| |
|-------------------------------------------------------------|



The layout contains 5 text-itmes:

- Profile
- To
- Subject
- Message (multiline functional property set to true)
- Attach

and a 'Send' button with the following WHEN-BUTTON-PRESSED trigger:
---------------------------
mail_pkg.logon(:profile);
mail_pkg.send(:to,:subject,:message,:attch);
mail_pkg.logoff;




Tom Kyte
June 18, 2006 - 12:18 pm UTC

see above

Storing sent mail in my "sent mail" folder

Vinay Krishna Ravi, June 19, 2006 - 5:12 am UTC

Hi Tom,

i wanna to send sent mail into my "sent mail" folder.

how can i do it?

thx

V K Ravi (Noida,India)

Tom Kyte
June 19, 2006 - 6:07 pm UTC

"use your email client to send email"???

that would be about it, it would have to sort of be logged into your email client to be able to modify your folders (thank goodness! would not be good otherwise)

You could .bcc yourself.

Mailing thr Lotus Notes

vishal, July 19, 2008 - 5:40 am UTC

Absolutely great knowledge share.

I am sending mail through D2k via Lotus Notes. But i have no validation for current user of notes. How can i check username and password in D2k.

Pls help me out

Thanks
Tom Kyte
July 19, 2008 - 6:38 am UTC

if you mean by d2k "developer 2000", the software we sold in the middle of the 1990's.....


you can try the forums on otn.oracle.com -> forms.


But, you'll want to tell them a bit better what you are asking. Check what username and password?

re: mail through Lotus Notes

Stew Ashton, July 29, 2008 - 1:48 pm UTC


Just a guess: you are trying to access the Lotus Notes client on the user's workstation. In that case, the easiest way is for the user to allow third party applications to access the client without prompting again for the password. Where you click to do that depends on the Notes client version.

abhay, September 09, 2010 - 5:08 am UTC

Sir

please tell me how to work this d2k block and coding

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library