Highlighted
Absent Member.
Absent Member.
3607 views

Send email from cobol native with SMTP parameters

Good day please someone has knowledge or has an example of sending email by defining an SMTP server and attaching a file native Cobol, Many thanks for your collaboration

Aprediz desarrollador en cobol nativo

0 Likes
13 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Send email from cobol native with SMTP parameters

It looks like this can be done using the SmtpClient class in managed code. It would be possible to create a .NET program that uses this class and then call this from native code.

msdn.microsoft.com/.../system.net.mail.smtpclient(v=vs.110).aspx
0 Likes
Highlighted
Outstanding Contributor.
Outstanding Contributor.

RE: Send email from cobol native with SMTP parameters

Have a look to www.marshallsoft.com.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Send email from cobol native with SMTP parameters

I think I used marshallsoft from an mf cobol program over 15 years ago to mass send email for an email news service. Worked very well.
NEXT QUESTION... how to create PDFs and attach them too?
It would be great to be able to create PDFs and send an email (to invoice clients etc) all from a visual cobol app.
Thanks,
Linden

Linden Rowland - IT Consultant
Owner/Developer, www.SchoolReportWriter.com
IT Consultant to Tindle Newspaper Group

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Send email from cobol native with SMTP parameters

The entire platform of files and tables is on a server with Windos Server 2012r2 through cobol server 2.3. Perform tests with MarshallSoft, but for the most part, the process stops when it is run, and send a ticket to the developer without answer. COBEMAIL has not worked, the only way I have is to make a code through which I can send an email confirmation of a transaction, attaching a .txt with the information of the transaction made. But I must use the SMTP and POP mail server, not do it through an API that controls Outlook.
God bless you, thank you very much for your interest in this subject

Aprediz desarrollador en cobol nativo

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Send email from cobol native with SMTP parameters

Sending email via native COBOL can be done using the CDO libraries from Microsof (I think you can still use these).

This is relatively easy to convert to COBOL.

Just remember to create an exception handler as well as a local working storage section to manage the exceptions

The code below is just VB script. Perhaps download the CDO library, modify the VB script first to get it working, then convert to COBOL. If this is an in-house solution. If you are sending via Exchange server you must give permissions to enable SMTP.

msdn.microsoft.com/.../ms526130(v=exchg.10).aspx


Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Test SMTP FROM MANDY- Please confirm i f you receive this message"

objMessage.Sender = "MANDY@xyz.com"

objMessage.To = "Fred@aaa.com"

objMessage.TextBody = "Cool. Please email me if you received this message, thanks"

objMessage.Configuration.Fields.Item _
("schemas.microsoft.com/.../sendusing") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("schemas.microsoft.com/.../smtpserver") = "192.168.1.55"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("schemas.microsoft.com/.../smtpserverport") = 25


'SMTP authenticate
'objMessage.Configuration.Fields.Item _
'("schemas.microsoft.com/.../smtpauthenticate") = 1

'SMTP UserName
objMessage.Configuration.Fields.Item _
("schemas.microsoft.com/.../sendusername") = "mydomain\Mandy"

'SMTP Password
objMessage.Configuration.Fields.Item _
("schemas.microsoft.com/.../sendpassword") = "mypassword"


objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==

objMessage.Send
Msgbox("Done")

This might work for you.

Neil
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Send email from cobol native with SMTP parameters

The following is an example of sending an email with an attachment using the .NET SmtpClient class. This allows you to specify the host name and port to use as well as many other properties. This example uses an async send method.

 

 
      $set ilusing"System.Net"
      $set ilusing"System.Net.Mail"
      $set ilusing"System.Net.Mime"
      $set ilusing"System.Threading"
      $set ilusing"System.ComponentModel"

       class-id sendsmtpmail.SimpleAsynchronousExample.

       working-storage section.
       01 mailSent condition-value static value false.

       method-id SendCompletedCallback static private (sender as object, e as type AsyncCompletedEventArgs).
           *> Get the unique identifier for this asynchronous operation.
           declare token as string = e::UserState as string

           if e::Cancelled
              invoke type Console::WriteLine("[{0}] Send canceled.", token)
           end-if
           if e::Error not = null
              invoke type Console::WriteLine("[{0}] {1}", token, e::Error::ToString())
           else
              invoke type Console::WriteLine("Message sent.")
           end-if
           set mailSent to true
           goback.
       end method.
       method-id Main public static (args as string occurs any)
.          *> The SMTP host name is passed as a string in the constructor in this example
           declare client as type SmtpClient = new SmtpClient("mail.myserver.com")
           *> Specify the e-mail sender.
           *> Create a mailing address
           declare #from as type MailAddress = new MailAddress("chris.glazier@microfocus.com", 
               "Chris Glazier", type System.Text.Encoding::UTF8)
           *> Set destinations for the e-mail message.
           declare #to as type MailAddress = new MailAddress("chris.glazier@microfocus.com")
           *> Specify the message content.
           declare #message as type MailMessage = new MailMessage(#from, #to)
           set #message::Body = "This is a test e-mail message sent by an application. " 
              & type Environment::NewLine
           set #message::BodyEncoding =  type System.Text.Encoding::UTF8
           set #message::Subject = "test message 1"
           set #message::SubjectEncoding = type System.Text.Encoding::UTF8
           *> Set the method that is called back when the send operation ends.
           
           invoke #message::Attachments::Add(new Attachment("C:\temp\testfile.txt"))
            attach method self::SendCompletedCallback to client::SendCompleted
           
           *> The userState can be any object that allows your callback 
           *> method to identify this send operation.
           *> For this example, the userToken is a string constant.
           declare userState as  string = "test message1"
           set client::Credentials = type CredentialCache::DefaultNetworkCredentials
           invoke client::SendAsync(#message, userState)
           invoke type Console::WriteLine("Sending message... press c to cancel mail. Press any other key to exit.")
           declare answer as string = type Console::ReadLine
           *>  If the user canceled the send, and mail hasn't been sent yet,
           *>  then cancel the pending operation.
           if answer::StartsWith("c") and mailSent = false
              invoke client::SendAsyncCancel
           end-if
           *> Clean up.
           invoke #message::Dispose
           invoke type Console::WriteLine("Goodbye.")

           goback.
       end method.
           
       end class.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Send email from cobol native with SMTP parameters

Hello Chris Good day, I am testing your solution, but I generate errors in the definition, I am running under net framework 4.6, I am not clear if I am using a wrong version of netframework, muchas gracias Chris  

Aprediz desarrollador en cobol nativo

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Send email from cobol native with SMTP parameters

Sorry, you would need to add a reference to the System.Net assembly in your project in order to access the classes.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Send email from cobol native with SMTP parameters

Hi chris, sorry for the inconvenience, modify what you need. But I still do not get the receipt of the email generated in the code, and validate that the email account and everything is fine




$set ilusing"System.Net"
$set ilusing"System.Net.Mail"
$set ilusing"System.Net.Mime"
$set ilusing"System.Threading"
$set ilusing"System.ComponentModel"

class-id sendsmtpmail.SimpleAsynchronousExample.

working-storage section.
01 mailSent condition-value static value false.


procedure division.
method-id Main public static (args as string occurs any).
. *> The SMTP host name is passed as a string in the constructor in this example
declare client as type SmtpClient = new SmtpClient("smtp.croydon.com.co")
*> Specify the e-mail sender.
*> Create a mailing address
declare #from as type MailAddress = new MailAddress("sistemaaurora@croydon.com.co",
"Chris Glazier", type System.Text.Encoding::UTF8)
*> Set destinations for the e-mail message.
declare #to as type MailAddress = new MailAddress("soporte_sistemas@crydon.com.co")
*> Specify the message content.
declare #message as type MailMessage = new MailMessage(#from, #to)
set #message::Body = "Prueba de correo electronico utilizando servidor SMTP"
& type Environment::NewLine
set #message::BodyEncoding = type System.Text.Encoding::UTF8
set #message::Subject = "Prueba SMTP"
set #message::SubjectEncoding = type System.Text.Encoding::UTF8
*> Set the method that is called back when the send operation ends.

invoke #message::Attachments::Add(new Attachment("C:\temp\testfile.txt"))
* attach method self::SendCompletedCallback to client::SendCompleted

*> The userState can be any object that allows your callback
*> method to identify this send operation.
*> For this example, the userToken is a string constant.
declare userState as string = "test message1"
set client::Credentials = new NetworkCredential("sistemaaurora@croydon.com.co", "**********")
invoke client::SendAsync(#message, userState)
invoke type Console::WriteLine("Enviando correo, press C para cancelar")
declare answer as string = type Console::ReadLine
*> If the user canceled the send, and mail hasn't been sent yet,
*> then cancel the pending operation.
if answer::StartsWith("c") and mailSent = false
invoke client::SendAsyncCancel
end-if
*> Clean up.
invoke #message::Dispose
invoke type Console::WriteLine("Goodbye.")

goback.
end method.

Aprediz desarrollador en cobol nativo

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Send email from cobol native with SMTP parameters

Is this a typo? should crydon be Croydon?
MailAddress("soporte_sistemas@crydon.com.co")
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Send email from cobol native with SMTP parameters

Sorry Chris, when copying and editing the text erase without intention, and check in my code and I do not have that error

Aprediz desarrollador en cobol nativo

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.