Email using GMAIL with TLS

Does anyone have a solution using C$SOCKET with sending email through SMTP.GMAIL.COM with TLS. Is there any other alternative without having to learn VB SCRIPT or another language. Any examples would be appreciated.

Thanks,

David

  • Since you refer specifically to VBScript, I presume you have seen and rejected this approach:

    community.microfocus.com/.../10743.aspx

    I can almost guarantee you that you will have a VBScript based solution running before you can even assemble all the knowledge you need for creating a COBOL TLS solution for GMail. There are several VBScript tutorials available on the web to help you get started.

  • Thanks Tom for the quick reply. I was hoping I would not have to learn anything new right now but you know!!!!

    Would the VBScript option be used for all email or just GMAIL?

    Thanks,

    David

  • Thanks Tom for the quick reply. I was hoping I would not have to learn anything new right now but you know!!!!

    Would the VBScript option be used for all email or just GMAIL?

    Thanks,

    David

  • Verified Answer

    Hi David,

    Email is email.  The main issue that folks run into with GMail is the authentication, which not all SMTP servers have.  If you are planning on running all your outbound email via GMail, then the example scripts do the trick.

    If you wish to use multiple SMTP servers to send email for some reason (which is unusual), you can make the server addressing and other server details configurable.  The second link, which I am repeating here, shows quite clearly how you set up the SMTP server address.

    My company is using a variation of this script to send application-generated email with large To lists and with attachments, using a standard (not GMail) SMTP host.  I had it up and running in less than a day.  I was not new to VBScript, but did need to learn about the details of using CDO.

  • Thanks Tom for your responses. All my customers do not use GMAIL. That is exactly how I have written my system - configurable. I believe that the CDO option would be best since we are sending many emails with attachments. I just wished C$Socket would have worked since I already had that working for email that did not require TLS.

    Thanks again,

    David

  • Enjoy.


    ''  sendmailxml.vbs  parameters.xml [/delete:both|msg|parms] [/log:logfilename]
    ''
    ''  Logging is to the optional logfilename
    ''  Specify /delete:both to delete BOTH the parameters.xml file and the message file
    ''  Specify /delete:msg to delete only the message file
    ''  Specify /delete:parms to delete only the parameters.xml file
    ''        Nothing deleted if error; attachments are NOT deleted
    ''
    Const forReading = 1
    Const forWriting = 2
    Const forAppending = 8
    Dim objArgs, fso, theFile, strMessage, strBcc

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set objUnamedArgs = Wscript.Arguments.Unnamed
    Set objNamedArgs  = Wscript.Arguments.Named

    If objNamedArgs.Exists("log") Then
     If fso.FileExists(objNamedArgs("log")) Then
      Set logfile = fso.OpenTextFile(objNamedArgs("log"), forAppending)
     Else
      Set logfile = fso.CreateTextFile(objNamedArgs("log"))
     End If
    Else
     Set logfile = Null
    End If

    strDelete = "NO"
    If objNamedArgs.Exists("delete") Then strDelete = UCase(objNamedArgs("delete"))

    If Not IsNull(logfile) Then logfile.WriteLine chr(013) & chr(010) & "sendmailxml invoked at " & Now()

    strParmsFilename = objUnamedArgs(0)
    strAbsFilename = fso.GetAbsolutePathName(strParmsFilename)
     If Not IsNull(logfile) Then logfile.WriteLine "Parms:   " & strAbsFilename
    Set XMLParms = CreateObject("Microsoft.XMLDOM")
    XMLParms.async = false
    XMLParms.load(strAbsFilename)
    If XMLParms.parseError.errorCode <> 0 Then
     Set myErr = XMLParms.parseError
     If Not IsNull(logfile) Then logfile.WriteLine "Parse error: " myErr.Reason
     WScript.Quit
    End If

    strErrorFound = "NO"
    If objUnamedArgs.Count > 1 Then
     If UCase(objUnamedArgs(1)) = "DELETE" Then strDelete = UCase(objUnamedArgs(1))
    End If

    strTo = ""
    strComma = ""
    Set Nodes = XMLParms.documentElement.SelectNodes("//to")
    For i = 0 To Nodes.Length - 1
     strTo = strTo & strComma & Nodes(i).Text
     strComma = ","
    Next

    strBCC = ""
    strComma = ""
    Set Nodes = XMLParms.documentElement.SelectNodes("//bcc")
    For i = 0 To Nodes.Length - 1
     strBCC = strBCC & strComma & Nodes(i).Text
     strComma = ","
    Next

    Set Nodes = XMLParms.documentElement.SelectNodes("//account")
    If Nodes.Length = 1 Then
     strAccountID = Nodes(0).Text
    Else
     strAccountID = "No Account or multiple accounts found."
     strErrorFound = "YES"
    End If

    Set Nodes = XMLParms.documentElement.SelectNodes("//pw")
    If Nodes.Length = 1 Then
     strPassword = Nodes(0).Text
    Else
     strPassword = "No Password or multiple passwords found."
     strErrorFound = "YES"
    End If

    Set Nodes = XMLParms.documentElement.SelectNodes("//smtp-server")
    If Nodes.Length = 1 Then
     strSMTPServer = Nodes(0).Text
    Else
     strSMTPServer = "No smtp-server or multiple smtp-server found."
     strErrorFound = "YES"
    End If

    Set Nodes = XMLParms.documentElement.SelectNodes("//smtp-port")
    If Nodes.Length = 1 Then
     smtpServerPort = Nodes(0).Text
    Else
     smtpServerPort = "No smtp-port or multiple smtp-port found."
     strErrorFound = "YES"
    End If

    Set Nodes = XMLParms.documentElement.SelectNodes("//subject")
    If Nodes.Length = 1 Then
     strSubject = Nodes(0).Text
    Else
     strSubject = "No Subject"
    End If

    Set Nodes = XMLParms.documentElement.SelectNodes("//from")
    If Nodes.Length = 1 Then
     strFrom = Nodes(0).Text
    Else
     strFrom = "No From or multiple from found."
     strErrorFound = "YES"
    End If

    strSender = ""
    Set Nodes = XMLParms.documentElement.SelectNodes("//sender")
    If Nodes.Length = 1 Then strSender = Nodes(0).Text

    Set Nodes = XMLParms.documentElement.SelectNodes("//message-file")
    If Nodes.Length = 1 Then
     strMessageFilename = Nodes(0).Text
    Else
     strMessageFilename = "No message-file or multiple message-file found."
     strErrorFound = "YES"
    End If

     If Not IsNull(logfile) Then logfile.WriteLine "From:    " & strFrom
     If Not IsNull(logfile) Then logfile.WriteLine "Sender:  " & strSender
     If Not IsNull(logfile) Then logfile.WriteLine "To:      " & strTo
     If Not IsNull(logfile) Then logfile.WriteLine "Bcc:     " & strBCC
     If Not IsNull(logfile) Then logfile.WriteLine "Subject: " & strSubject
     If Not IsNull(logfile) Then logfile.WriteLine "Account: " & strAccountID
     If Not IsNull(logfile) Then logfile.WriteLine "Passwd:  " & String(Len(strPassword), "*")
     If Not IsNull(logfile) Then logfile.WriteLine "SMTP Srv:" & strSMTPServer
     If Not IsNull(logfile) Then logfile.WriteLine "Port:    " & CStr(smtpServerPort)
     If Not IsNull(logfile) Then logfile.WriteLine "MsgFile: " & CStr(strMessageFilename)
     If strErrorFound = "YES" Then
     If Not IsNull(logfile) Then logfile.WriteLine "Error processing parameters.  No connection attempt."
     WScript.Quit
     End If


    Set theFile = fso.OpenTextFile(strMessageFilename, forReading)
    If theFile.AtEndOfStream Then
     strMessage = ""
    Else
     strMessage = theFile.ReadAll
    End If
    theFile.Close

    strNewText = Replace(strMessage, chr(013) & chr(010), " ") ' chr(010) = line feed chr(013) = carriage return
    If Not IsNull(logfile) Then logfile.WriteLine "Msg:     " & Left(strNewText,20) & "<..>" & Right(strNewText,20) & "(" & CStr(Len(strMessage)) & ")"
     

    Set oEmail = CreateObject("CDO.Message")
     
     'configure message
    With oEmail.Configuration.Fields
     .Item("schemas.microsoft.com/.../sendusing") = 2
        .Item("schemas.microsoft.com/.../smtpserver") = strSMTPServer
        .Item("schemas.microsoft.com/.../smtpserverport") = smtpServerPort
        .Item("schemas.microsoft.com/.../smtpauthenticate") = 1 'basic
        .Item("schemas.microsoft.com/.../sendusername") = strAccountID
        .Item("schemas.microsoft.com/.../sendpassword") = strPassword
        .Update
    End With
     
     ' build message
    With oEmail
        .From = strFrom
     .Sender = strSender
        .To = strTo
        .Bcc = strBCC
     .Subject = strSubject
     .TextBody = strMessage
    End With

    Set Nodes = XMLParms.documentElement.SelectNodes("//attachment-file")
    For i = 0 To Nodes.Length - 1
     strAttachFile = fso.GetAbsolutePathName(Nodes(i).Text)
     If Not IsNull(logfile) Then logfile.WriteLine "Attach:  " & strAttachFile
     oEmail.AddAttachment strAttachFile
    Next

    If strDelete = "BOTH" or strDelete = "PARMS" or strDelete = "MSG" Then
        If Not IsNull(logfile) Then logfile.WriteLine "DelMode: " & strDelete
    End If
    If strDelete = "BOTH" or strDelete = "PARMS" Then
        If Not IsNull(logfile) Then logfile.WriteLine "Delete:  " & strAbsFilename
     fso.DeleteFile(strAbsFilename)
    End If
    If strDelete = "BOTH" or strDelete = "MSG" Then
     strMsgFileForDelete = fso.GetAbsolutePathName(strMessageFilename)
        If Not IsNull(logfile) Then logfile.WriteLine "Delete:  " & strMsgFileForDelete
     fso.DeleteFile(strMsgFileForDelete)
    End If
     
     ' send message
    On Error Resume Next
     oEmail.Send
     If Err Then
          If Not IsNull(logfile) Then logfile.WriteLine "SendMail Failed:" & Err.Description
     End If
    If Not IsNull(logfile) Then logfile.WriteLine "sendmailxml complete at " & Now()

  • Thanks Tom!

    Since I am new to all this VBScript and CDO functionality how would I actually run this using parameters from my program?

    I know I can use the system command to run the sendmailxml.vbs. Would they be parameters after the sendmailxml.vbs?

    Thanks,

    David

  • Hi David,

    I cannot tell you exactly what you need to do in AcuCOBOL, since I am an RM/COBOL user.  In the forum thread I referenced above, there was some discussion.  In RM/COBOL I use:

        MOVE SPACES TO CMD-LINE
        STRING 'cmd.exe /c sendmailxml.vbs "' delimited by size
               XML-PATHNAME            delimited by space *> I make sure there are no spaces in the name
               '" /delete:both /log:"' delimited by size
               LOG-PATHNAME            delimited by space
               '"'                     delimited by size
            INTO CMD-LINE
        CALL "SYSTEM" USING CMD-LINE

    The comments at the top of the script describe the command parameters.  On RM/COBOL, I use XML Extensions to export the parameter document to a temporary file (the pathname of which is in XML-PATHNAME), then pass that filename as a parameter to the VBScript.as shown above.  I use a parameter document because (1) exporting is so easy using XML Extensions, (2) the parameters are really too complex for a command line, even with named parameters available, and (3) processing an XML document on the VBScript side is also quite easy.  The parameters that I put in the parameter document include everything I need for assembling the email message.

    Executing a VBScript file is very similar to executing a .BAT file.

  • That helped a lot.

    Would you have an example of what the parameter document would look like?

    Thanks,

    David

  • That helped a lot.

    Would you have an example of what the parameter document would look like?

    Thanks,

    David