How to send an email with multiple attachments using Outlook

How to send an email with multiple attachments using Outlook

How to send an email with multiple attachments using Outlook

Titleimage

Posted by Patrick Hamou on 2017:09:12 14:44:39

PURPOSE

How to send an email with multiple attachments using Outlook.

Caution

The sample program in this article is provided for educational purposes only

and is NOT supported by Oracle Support Services.  It has been tested

internally, however, and works as documented.  We do not guarantee that it

will work for you, so be sure to test it in your environment before relying

on it.

SCOPE & APPLICATION

The code given below will send an email with multiple 

attachments using Outlook.

---

Declare

  /*declaration of the Outlook Object Variables*/
  application ole2.OBJ_TYPE;      
  hMailItem ole2.OBJ_TYPE;
  hRecipients ole2.OBJ_TYPE;
  recipient ole2.OBJ_TYPE;

  /*declaration of the argument list*/  
  args OLE2.LIST_TYPE;    
          
  msg_attch OLE2.OBJ_TYPE; 
  attachment OLE2.OBJ_TYPE; 

  des VARCHAR2(80) := 'FILE_NAME'; 
  attch VARCHAR2(80) := 'C:\temp\bt.TXT';

  begin
  /*create the Application Instance*/
  application:=ole2.create_obj('Outlook.Application');            
    
  args:=ole2.create_arglist;                                      
  ole2.add_arg(args,0);
  hMailItem:=ole2.invoke_obj(application,'CreateItem',args);
  ole2.destroy_arglist(args);
  
  msg_attch := OLE2.GET_OBJ_PROPERTY(hMailItem,'Attachments'); 

  /* Attach the 1st file */
  args := OLE2.CREATE_ARGLIST; 
  OLE2.ADD_ARG(args,attch); 
  attachment := OLE2.INVOKE_OBJ(msg_attch,'Add',args); 
  ole2.destroy_arglist(args); 
  OLE2.SET_PROPERTY(attachment,'name',des); 
  OLE2.SET_PROPERTY(attachment,'position',1); 
  OLE2.SET_PROPERTY(attachment,'type',1); 
  OLE2.SET_PROPERTY(attachment,'source',Attch); 
  args := OLE2.CREATE_ARGLIST; 
  OLE2.ADD_ARG(args,Attch); 
  OLE2.INVOKE(attachment,'ReadFromFile',args); 
  OLE2.DESTROY_ARGLIST(args); 
  
  /* Attach the 2nd file */
  attch:='c:\temp\test.dst';
  args := OLE2.CREATE_ARGLIST; 
  OLE2.ADD_ARG(args,attch); 
  attachment := OLE2.INVOKE_OBJ(msg_attch,'Add',args); 
  ole2.destroy_arglist(args); 
  OLE2.SET_PROPERTY(attachment,'name',des); 
  OLE2.SET_PROPERTY(attachment,'position',2); 
  OLE2.SET_PROPERTY(attachment,'type',1); 
  OLE2.SET_PROPERTY(attachment,'source',attch); 
  args := OLE2.CREATE_ARGLIST; 
  OLE2.ADD_ARG(args,Attch); 
  OLE2.INVOKE(attachment,'ReadFromFile',args); 
  OLE2.DESTROY_ARGLIST(args); 
  
  /*Get the Recipients property of the MailItem object:  
  Returns a Recipients collection that represents all the Recipients for the Outlook item*/
  args:=ole2.create_arglist;
  hRecipients:=ole2.get_obj_property(hMailItem,'Recipients',args);
  ole2.destroy_arglist(args);
    
  /*Use the Add method to create a recipients Instance and add it to the Recipients collection*/
  args:=ole2.create_arglist; 
  ole2.add_arg(args,'myname@myserver.com');
  recipient:=ole2.invoke_obj(hRecipients,'Add',args);

    /* put the property Type of the recipient Instance  to value needed (0=Originator,1=To,2=CC,3=BCC)*/
  ole2.set_property(recipient,'Type',1);
  ole2.destroy_arglist(args);
    
  /*Resolve the Recipients collection*/
  args:=ole2.create_arglist;
  ole2.invoke(hRecipients,'ResolveAll',args);
   
  /*set the Subject and Body properties*/
  ole2.set_property(hMailItem,'Subject','Test OLE2 to Outlook');
  ole2.set_property(hMailItem,'Body','this is body text');
  
    
  /*Save the mail*/ 
  ole2.invoke(hMailItem,'Save',args);
  ole2.destroy_arglist(args);

  /*Send the mail*/ 
  args:=ole2.create_arglist;
  ole2.invoke(hMailItem,'Send',args);
  ole2.destroy_arglist(args); 
   
  /*Release all your Instances*/
  release_obj(application);
  release_obj(hRecipients);
  release_obj(recipient);
  release_obj(hMailItem);
   
  end; 

 

RELATED DOCUMENTS

Note 119828.1 OLE AUTOMATION: Example Sending a Mail From Forms to Outlook

Posted by Patrick Hamou on 2017:09:12 14:44:39

Return to Blog