Highlighted
Absent Member.
Absent Member.
3107 views

Open Outlook with Attachment

Jump to solution

Hi,

Does anyone know how to open Outlook with an attachment from Visual Cobol.

This would have to work with all the "different" versions of Outlook 2007, 2010 etc etc without any program changes.

Thanks

Neil.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

I have just attached it here.

Please download emailwithattach.zip.

 

View solution in original post

0 Likes
9 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

If you are referring to sending an e-mail with an attachment from Outlook, the following example will do that:

     $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.

0 Likes
Highlighted
Absent Member.
Absent Member.

Hi Chris,

Thanks for that, a couple of questions?

1) Will this work with any version of outlook a client has installed on their pc?

2) Does this code open Outlook, create email, send email, then close Outlook, all automatically?

Many thanks

Neil.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

This sample uses the .NET Framework classes in microsoft.office.interop.outlook assembly.

You would need to add a reference to this assembly in your project.

This is documented as being supported for Outlook 2007 and 2010 and on my system I tested with Outlook 2010.

This uses the Outlook COM Server so Outlook does not have to be up and running in order for this to work.

The only suggestion that I can make is for you to test it on the required target systems to ensure compatibility with all.

Thanks.

0 Likes
Highlighted
Absent Member.
Absent Member.

Thanks Chris, is this project/code available to download any where?

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

I have just attached it here.

Please download emailwithattach.zip.

 

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.
Hello Chris, good morning, God bless you for your courageous collaboration with the issue of separation in columns of excel, as you can see I could enable my id in the community.
I am trying the code that you attached and I managed to implement it in my project, you know or know a way to apply the values ​​of my POP3 server and SMTP for user and password

Aprediz desarrollador en cobol nativo

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Hi Edwin,

There is a property called account which contains some fields that can be set including the smtp server name which can be found here but this example is for use with managed .NET COBOL as it references a .NET class. If you are using native COBOL then this will not work. Are you using native or managed .NET code?

0 Likes
Highlighted
Absent Member.
Absent Member.
Hello Chris

I can not use Outlook from native cobol, I saw that it was simpler and faster using Cobol .net. The main program is built in native cobol, so I will have to make a call to the routine that will send the email from said code, sending the parameters of body, recipients and subject. The e-mail account that you must use to send e-mail has user password an SMTP and POP3 configuration


Thank you for your valuable help.

Aprediz desarrollador en cobol nativo

0 Likes
Highlighted
Absent Member.
Absent Member.
Chris, good day
I try to configure POP3 and SMTP, in the documentation they speak that the instruction only returns values ​​does not allow the direct configuration of parameters, you have some guide on which I can base to make said configuration

Aprediz desarrollador en cobol nativo

0 Likes
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.