Skip to main content

Hi all,

How can the following VBA code be written in COBOL?

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
   .To = "address@dot.com"
   .Subject = "test "
   .SendUsingAccount = OutApp.Session.Accounts.Item(2)
   '.HTMLBody = "test"
   .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing

In other words, I want to send an email using NOT the default account. In the example above, I want to use the second of the collection of accounts.

This has been asked three times in tha past but with no answer...

Thank you.

Hi all,

How can the following VBA code be written in COBOL?

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
   .To = "address@dot.com"
   .Subject = "test "
   .SendUsingAccount = OutApp.Session.Accounts.Item(2)
   '.HTMLBody = "test"
   .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing

In other words, I want to send an email using NOT the default account. In the example above, I want to use the second of the collection of accounts.

This has been asked three times in tha past but with no answer...

Thank you.

Attached is a sample program to give you more an idea what you need to do.

To set the sender, try using MailItem.Sender - https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.sender

Thank you


community.microfocus.com/.../MSOutlookSendMail.zip


Attached is a sample program to give you more an idea what you need to do.

To set the sender, try using MailItem.Sender - https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.sender

Thank you


community.microfocus.com/.../MSOutlookSendMail.zip

Thank you. Unfortunately that didn't work. A full working example would be much appreciated.

What I want to do is the following:

In Outlook I have set up two accounts, the work account and the gmail account.

Some mails should be sent by the work account, some mails should be sent by gmail account.

Thank you.


Thank you. Unfortunately that didn't work. A full working example would be much appreciated.

What I want to do is the following:

In Outlook I have set up two accounts, the work account and the gmail account.

Some mails should be sent by the work account, some mails should be sent by gmail account.

Thank you.

This looks more like what you need, it just needs to be converted to COBOL - www.slipstick.com/.../


This looks more like what you need, it just needs to be converted to COBOL - www.slipstick.com/.../

Thank you again but SentOnBehalfOfName is not an option.

This is my code so far:

CREATE APPLICATION OF OUTLOOK HANDLE IN OUTLOOK-HANDLE
inquire OUTLOOK-HANDLE @Session in W-Var-_NameSpace

inquire W-Var-_NameSpace @Accounts in W-Var-Accounts

modify W-Var-Accounts @Item (2) Returning H-Account         || this is the gmail account

MODIFY OUTLOOK-HANDLE, CREATEITEM (OLMAILITEM)
GIVING MAIL-HANDLE

MODIFY MAIL-HANDLE, @DISPLAY(0)
MODIFY MAIL-HANDLE, @TO = "address@dot.gr"
MODIFY MAIL-HANDLE, @SUBJECT = "test"
MODIFY MAIL-HANDLE, @BODY = "test"

modify MAIL-HANDLE @SendUsingAccount = H-Account     || this doesn't work. It keeps selecting the first account.

MODIFY MAIL-HANDLE, @Send()
destroy OUTLOOK-HANDLE


Thank you again but SentOnBehalfOfName is not an option.

This is my code so far:

CREATE APPLICATION OF OUTLOOK HANDLE IN OUTLOOK-HANDLE
inquire OUTLOOK-HANDLE @Session in W-Var-_NameSpace

inquire W-Var-_NameSpace @Accounts in W-Var-Accounts

modify W-Var-Accounts @Item (2) Returning H-Account         || this is the gmail account

MODIFY OUTLOOK-HANDLE, CREATEITEM (OLMAILITEM)
GIVING MAIL-HANDLE

MODIFY MAIL-HANDLE, @DISPLAY(0)
MODIFY MAIL-HANDLE, @TO = "address@dot.gr"
MODIFY MAIL-HANDLE, @SUBJECT = "test"
MODIFY MAIL-HANDLE, @BODY = "test"

modify MAIL-HANDLE @SendUsingAccount = H-Account     || this doesn't work. It keeps selecting the first account.

MODIFY MAIL-HANDLE, @Send()
destroy OUTLOOK-HANDLE

This worked for me  (using SendMail.cbl)
CREATE APPLICATION OF OUTLOOK
HANDLE IN OUTLOOK-HANDLE.
INQUIRE OUTLOOK-HANDLE VERSION IN WS-VERSION.
DISPLAY "Version of Outlook :" WS-VERSION.
DISPLAY "Application Created. Creating Mail".

MODIFY OUTLOOK-HANDLE CREATEITEM(OlMailItem)
GIVING MAIL-HANDLE.
MODIFY MAIL_HANDLE @SentOnBehalfOfName =
"stephen.hjerpe@microfocus.com".

MODIFY MAIL-HANDLE SUBJECT = "COM test".
MODIFY MAIL-HANDLE BODY =
"Good Job this one".

MODIFY MAIL-HANDLE @TO =
"sfhjerpe@gmail.com".
MODIFY MAIL-HANDLE @SEND().

My first attempt was using my personal email MODIFY MAIL_HANDLE @SentOnBehalfOfName = "sfhjerpe@gmail.com". .. the program executed however Outlook (Outlook administrator) sent an email showing this 

      Stephen Hjerpe on 12/7/2021 1:43 PM

            This message could not be sent. You do not have the permission to send the message on behalf of the specified user 

Once I set @SentOnBehalfOfName email name had the appropriate permissions the email succeeded


Hi all,

How can the following VBA code be written in COBOL?

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
   .To = "address@dot.com"
   .Subject = "test "
   .SendUsingAccount = OutApp.Session.Accounts.Item(2)
   '.HTMLBody = "test"
   .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing

In other words, I want to send an email using NOT the default account. In the example above, I want to use the second of the collection of accounts.

This has been asked three times in tha past but with no answer...

Thank you.

Hi!

Thank you all for your answers!

I managed to do it by using an external excel file with a macro that does the actual task.

My COBOL program opens the excel workbook, writes the "from", "to", cc" etc information in a sheet, and then fires the macro that loops the accounts, matches an account with the "from" and sends the email. It is working perfectly!

Thank you again!