Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

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

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

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

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-04-19 08:47
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.