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