Highlighted
Absent Member.
Absent Member.
3882 views

Sending email from cobol program using GMAIL instead of Windows email.

[Migrated content. Thread originally posted on 07 February 2011]

Has anyone accomplished creating a program that will generate an email with an attachement using GMAIL from a cobol progarm? If so, are you willing to share your code. I presently have a program that uses windows email. We pass the email address and attachment path and the program creates an email with the attachment and calls microsoft email and sends the message. We want to do the same with GMAIL.

0 Likes
6 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Sending email from cobol program using GMAIL instead of Windows email.

It is pretty straight forward using our .Net support, the URL below shows how to do it in C#, for simplicity I uses the library from http://code.msdn.microsoft.com/CSharpGmail

The COBOL code equivalent of their example is:

      $set ilusing"System.Web.Mail"
      $set ilusing"GmailHelper"
       program-id. Program1 as "ConsoleApplication1.Program1".

       data division.
       working-storage section.
       *>Use the GmailMessage object to create and send your message
       01 gmailMsg             type RC.Gmail.GmailMessage.
       01 attachment           type MailAttachment.
       01 mailMessageObject    type MailMessage.

       procedure division.
           *> Send a message with one line of code
           invoke type RC.Gmail.GmailMessage::SendFromGmail("username", "password", "toAddress@gmail.com", "subject", "message body")


           *> Send a message with one line of code with a MailMessage object
           invoke type RC.Gmail.GmailMessage::SendMailMessageFromGmail("username", "password", mailMessageObject)


           set gmailMsg to new type RC.Gmail.GmailMessage("username", "password")
           set gmailMsg::To to "RCcode@gmail.com"
           set gmailMsg::From to "fromAddress@gmail.com"
           set gmailMsg::Subject to "C# Test Message"
           set gmailMsg::Body to "Test body"

           set attachment to new type System.Web.Mail.MailAttachment("c:\testfile.txt")
           invoke gmailMsg::Attachments::Add(attachment)

           invoke gmailMsg::Send()
           goback.
           
       end program Program1.
0 Likes
Highlighted
Micro Focus Contributor
Micro Focus Contributor

RE: Sending email from cobol program using GMAIL instead of Windows email.

In Visual COBOL managed code using .NET framework classes:

      $set ilusing"System.Net"
      $set ilusing"System.Net.Mail"
       program-id. SendMailCobol as "SendMailCobol".

       data division.
       working-storage section.
       01 mailaddr      Type MailAddress.
       01 mailmsg       Type MailMessage.
       01 client        Type SmtpClient.

       procedure division.
           set mailmsg to new MailMessage()
           set mailaddr to new MailAddress("somename@domain.com", "John Doe")
           invoke mailmsg::To::Add(mailaddr)
           
           set mailmsg::From to new MailAddress("youremail@gmail.com", "Your Name")
           set mailmsg::Subject to "Sent from Visual COBOL"
           set mailmsg::Body to "Your Message Text"
           set mailmsg::IsBodyHtml to false

       *> Add an attachment to the Attachments collection of the mail message object
           invoke mailmsg::Attachments::Add(new Attachment("C:\yourfile.pdf"))
           
           set client to new SmtpClient()
           set client::Credentials to new NetworkCredential("youremail@gmail.com", "yourpassword")
           set client::Port to 587
           set client::Host to "smtp.gmail.com"
           set client::EnableSsl to true
           try
               invoke client::Send(mailmsg)
           catch
               display exception-object::Message
           end-try
           goback.

       end program SendMailCobol.


Make sure you have a gmail account established if going thru the gmail smtp server. If using another smtp server, then you would need to change the host, port, and EnableSSL properties accordingly. If you need to add more than one attachment then call the Add method for each attachment.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Sending email from cobol program using GMAIL instead of Windows email.

PattyStep,

while these fine examples are using Visual COBOL, I suppose you are looking for one using Extend? Try to do the translation, if you get stuck, post your work here and we can see if we can work this out together.
0 Likes
Highlighted
New Member.

RE: Sending email from cobol program using GMAIL instead of Windows email.

Can i take the same result without using any .Net or google object?
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Sending email from cobol program using GMAIL instead of Windows email.

Hi, dear.
I need to compile this program in AcuCobol, can you help-me
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Sending email from cobol program using GMAIL instead of Windows email.

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.