Example Visual COBOL .NET program sending an e-mail with attachment using Microsoft Outlook

0 Likes
over 8 years ago

Problem

Does anyone have an example of sending an e-mail including an attachment using Microsoft Outlook by using a managed code .NET program?

Resolution:

Here is an example:

     $set ilusing"System.Net.Mail"
     $set ilusing"System.Net"     
     $set ilusing"Microsoft.Office.Interop.Outlook"
      program-id. Program1 as "testemail.Program1".
      data division.
      working-storage section.
      01 oApp type Microsoft.Office.Interop.Outlook.ApplicationClass.
      01 omsg type Microsoft.Office.Interop.Outlook.MailItem.
      01 oAttach type Microsoft.Office.Interop.Outlook.Attachment.
      01 oRecips type Microsoft.Office.Interop.Outlook.Recipients.
      01 oRecip type Microsoft.Office.Interop.Outlook.Recipient.
      01 sDisplayName string.
      01 iPosition binary-long.
      01 iAttachType binary-long.
      procedure division.
            try
       *> Create the Outlook application.
             set oApp to new type Microsoft.Office.Interop.Outlook.ApplicationClass
       *> Create a new mail item.
             set oMsg to oApp::CreateItem(type
                Microsoft.Office.Interop.Outlook.OlItemType::olMailItem)
                       as type Microsoft.Office.Interop.Outlook.MailItem
       *> Set HTMLBody.
       *> add the body of the email
             set oMsg::HTMLBody to "Hello, your message body will go here!!"
       *> Add an attachment.
             set sDisplayName to "MyAttachment"
             set iPosition to oMsg::Body::Length 1
             set iAttachType to type Microsoft.Office.Interop.Outlook.OlAttachmentType::olByValue
                as binary-long
       *> now attach the file
             set oAttach to oMsg::Attachments::Add("C:\\temp\\testfile.txt", iAttachType, iPosition, sDisplayName)        *> Subject line
             set oMsg::Subject to "Your Subject will go here."
       *> Add a recipient.
             set oRecips to oMsg::Recipients
       *> Change the recipient in the next line if necessary
              set oRecip to oRecips::Add(chris.glazier@microfocus.com)
             invoke oRecip::Resolve
       *> Send.
             invoke oMsg::Send
       *> Clean up.
             set oRecip to null
             set oRecips to null
             set oMsg to null
             set oApp to null
          catch ex as type System.Exception
             display ex::Message
          end-try
          goback.
      end program Program1

Comment List
Anonymous
Related Discussions
Recommended