1. Jon Stoller
  2. PowerBuilder
  3. Sunday, 20 May 2018 18:58 PM UTC

Can I send email  via Outlook from PowerBuilder? 
Can I send it with an attachment? 
How do I do it? 

QUESTION:
I created a report in PowerBuilder and I want the Power Script to send a copy of it as an email attachment. 
How is it done?  Assume the required information is as follows:

String    ls_folderfile, ls_email_addr, ls_subject, ls_email_text_message
//Do you need the sender's email address?
String     ls_sender_addr
integer    li_return

ls_folderfile = "c:\pdf\pbreport.pdf"
idw_rpt.Object.DataWindow.Export.PDF.Method = XSLFOP!
li_return = idw_rpt.SaveAs(ls_folderfile, PDF!, true)

ls_sender_addr = "jon@akaware.com"
ls_email_addr = "jonstoller56@gmail.com"
ls_subject = "Report from AKA Software"
ls_email_text_message = "The attachment is a report from AKA Software"

//What do I do next to send an email to the above email address, with the above subject and text message, with the above file attached (ls_folderfile). 

 

Daryl Foster Accepted Answer Pending Moderation
  1. Tuesday, 22 May 2018 07:20 AM UTC
  2. PowerBuilder
  3. # 1

Hi Jon,

 

Here is some code that I think should work.  I haven't tested it but it is modified from some code I do use.  There are a couple of optional bits in there which I have commented with OPTIONAL

 

OLEObject ole_item, ole_attach, ole_outlook
integer li_result

String    ls_folderfile, ls_email_addr, ls_subject, ls_email_text_message
//Do you need the sender's email address?
String     ls_sender_addr
integer    li_return = 1

ls_folderfile = "c:\pdf\pbreport.pdf"
idw_rpt.Object.DataWindow.Export.PDF.Method = XSLFOP!
li_return = idw_rpt.SaveAs(ls_folderfile, PDF!, true)

// The SaveAs failed so handle it. I'm just returning early for this example
if li_return < 1 then return -1

ls_sender_addr = "jon@akaware.com"
ls_email_addr = "jonstoller56@gmail.com"
ls_subject = "Report from AKA Software"
ls_email_text_message = "The attachment is a report from AKA Software"

ole_outlook = Create OLEObject

//Connect to Outlook session using 'Outlook.Application'
li_result = ole_outlook.ConnectToNewObject("outlook.application")

if li_result <> 0 then
     // ERROR CONDITION
    Messagebox("Outlook Error","Could not launch Outlook.~n~rReturn Code [" + string(li_result) + "]")
    destroy ole_outlook
    li_return = -1
else
    // Creates a new mail Item
    ole_item = ole_outlook.CreateItem(0)
    
    // OPTIONAL - If you don't fill this in then it will just use the default email from account
    ole_item.SentOnBehalfOfName = ls_sender_addr
    
    // Set the recipient
    ole_item.To = ls_email_addr

    // OPTIONAL - Set the CC
    ole_item.Cc = 'cc@madeup.domain.com'
    
    // Set the subject line of message
    ole_item.Subject = ls_subject
    
    // Body of mail message
    ole_item.Body = ls_email_text_message + char(13)

    // OPTIONAL - if you want to use HTML in the body
    //ole_item.BodyFormat = 2        // olFormatHTML
    //ole_item.HTMLBody = ls_email_text_message + char(13)

    try
        ole_attach = ole_item.Attachments
    
        ole_attach.Add(ls_folderfile)
    catch (throwable t2)
        MessageBox('Error Adding Attachments', "There was an error adding attachments to Outlook.  The attachment size may exceed Outlook's allowable limit.", StopSign!)
        li_return = -1
    end try

    if li_return = 1 then
        ole_item.Display // Displays the message

        try
            ole_item.Send
        catch (throwable t)
            // Swallow this exception.  Can occur if the user cancels the email (e.g. after being presented with blank subject warning message)
            li_return = -1
        end try
    end if

    ole_outlook.DisconnectObject()
    
    destroy ole_outlook
end if

return li_return

Comment
  1. radha rani
  2. Tuesday, 22 May 2018 19:11 PM UTC
Awesome Daryl..



I want to know that i created this application in which i made the report in which all mangers can see the report and save it on pdf.



can i set above email setup on the background that in morning 10am it will auto send to managers.

  1. Helpful
  1. Daryl Foster
  2. Tuesday, 22 May 2018 23:38 PM UTC
Hi Radha, how are you planning on running it at 10am every morning?  As a task run from Task Scheduler on a server?



You could use the Outlook code but it is probably not really meant for unattended tasks because it could block if a messagebox is displayed from Outlook.  For unattended emailing we use a .Net email object we wrote in C# via COM interop in Powerbuilder so it is a totally non-visual object.  I think you could do something similar using Bruce Armstrong's PBNISmtp object.

  1. Helpful
  1. Jon Stoller
  2. Monday, 28 May 2018 20:22 PM UTC
Thank you very much,

I will try it. 



Jon Stoller

  1. Helpful
There are no comments made yet.
Andres Slachevsky Accepted Answer Pending Moderation
  1. Monday, 21 May 2018 14:25 PM UTC
  2. PowerBuilder
  3. # 2

Check the example in TopWiz
http://www.topwizprogramming.com/freecode_outlook.html

Comment
  1. Jon Stoller
  2. Monday, 28 May 2018 20:21 PM UTC
Thank you,

I will try it. 



Jon Stoller

  1. Helpful
  1. Jon Stoller
  2. Tuesday, 26 June 2018 18:32 PM UTC
Daryl,



About a month ago you sent me some sample code as a reply to my question herein. 

The code showed me how to send an email through Microsoft Outlook, attach a file, add a "cc", etc. 

I used the code in my application and it worked.  I have a related question that I am hoping you can help me with. 



Now I need to pop up the New Email instead of sending it.  The user wants to see the email before it is sent. 

The subject, cc's, attachment, and other items should already be in the new email when it pops up. 

I tried using the code that sends the email, except I changed the "ole.item.Send" statement. 

However, I couldn't get it to create the email and put me in the Outlook window. 



Here is a screen sample of what I want the code to do. I would like it to pop up an outlook window like this:









The text below is a snapshot of the sample code you sent me. 

I used it to send the email. 

To put the user into Outlook, is it a matter of changing the "ole_item.send" instruction, or do I need to do something else?  

(NOTE - I took out most of the code to handle errors and bad return codes):

 ole_outlook = Create OLEObject

 li_result = ole_outlook.ConnectToNewObject("outlook.application")

 if li_result > 0 then

     Messagebox("Outlook Error","......")

     destroy ole_outlook

          li_return = -1

 else

            ole_item = ole_outlook.CreateItem(0)

            ole_item.SentOnBehalfOfName = ls_sender_addr

            ole_item.To = ls_email_addr

            ole_item.Cc = 'cc@madeup.domain.com'

            ole_item.Subject = ls_subject

            ole_item.Body = ls_email_text_message + char(13)

            ole_attach = ole_item.Attachments

            ole_attach.Add(ls_folderfile)



     ole_item.Send                 //Sends Email

     //ole_item.New                //Caused an error

     //ole_item.NewMailMessage     //Caused an error

        end if



 ole_outlook.DisconnectObject()

 destroy ole_outlook



Thank you ahead of time for any help.



Jon Stoller

AKA Software



 

  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Monday, 21 May 2018 02:46 AM UTC
  2. PowerBuilder
  3. # 3

I have an example that connects to Outlook. It doesn't send email but that can easily be done. This page also has a link to the Outlook Object Model Reference which will help you find the correct commands.

http://www.topwizprogramming.com/freecode_outlook.html

 

Comment
  1. Jon Stoller
  2. Monday, 28 May 2018 20:20 PM UTC
Thank you,

I will try it. 



Jon Stoller

  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.