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

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


  • Verified Answer

    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.