Micro Focus Contributor
Micro Focus Contributor
3141 views

A quick sample of sending Email using Visual COBOL and .NET (using the Gmail smtp server)

Jump to solution

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

In Visual COBOL managed code using .NET framework classes:

System.Net.Mail.MailAddress
System.Net.Mail.MailMessage
System.Net.SmtpClient


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.


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


0 Likes
1 Solution

Accepted Solutions
Highlighted
Absent Member.
Absent Member.
If you want to send an attachment along with your email. You might as well want to compress it, here is an example of how to accomplish this.

$set ilusing "System"
$set ilusing "System.IO"
$set ilusing "System.IO.Compression"
  program-id. ZipFile as "ZipFile".

  data division.
  working-storage section.
  01 FInfo                type FileInfo.
  01 InStream             type FileStream.
  01 OutStream            type FileStream.
  01 ZipIt                type GZipStream.

  procedure division.
  invoke new type FileInfo("c:\example.txt") giving FInfo
  invoke FInfo::OpenRead() giving InStream
  invoke type File::Create(FInfo::FullName & ".gz") giving OutStream
  invoke new type GZipStream(OutStream, type CompressionMode::Compress) giving ZipIt
  invoke InStream::CopyTo(ZipIt)
  invoke ZipIt::Close()
  invoke InStream::Close()
  invoke OutStream::Close()
  goback
  .
               
  end program ZipFile.

View solution in original post

0 Likes
1 Reply
Highlighted
Absent Member.
Absent Member.
If you want to send an attachment along with your email. You might as well want to compress it, here is an example of how to accomplish this.

$set ilusing "System"
$set ilusing "System.IO"
$set ilusing "System.IO.Compression"
  program-id. ZipFile as "ZipFile".

  data division.
  working-storage section.
  01 FInfo                type FileInfo.
  01 InStream             type FileStream.
  01 OutStream            type FileStream.
  01 ZipIt                type GZipStream.

  procedure division.
  invoke new type FileInfo("c:\example.txt") giving FInfo
  invoke FInfo::OpenRead() giving InStream
  invoke type File::Create(FInfo::FullName & ".gz") giving OutStream
  invoke new type GZipStream(OutStream, type CompressionMode::Compress) giving ZipIt
  invoke InStream::CopyTo(ZipIt)
  invoke ZipIt::Close()
  invoke InStream::Close()
  invoke OutStream::Close()
  goback
  .
               
  end program ZipFile.

View solution in original post

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.