' SmtpGmailEmail   
' File: SmtpGmailEmail.vb 
' Author: R.Metcalfe
' Date: July,2013
' Copyright: RJM Programming © 2013
' Usage SmtpGmailEmail.exe emailer@gmail.com password c:\a\file.nam [emailto@whatever.huh] [emailcc@whatever.huh] [emailbcc@whatever.huh] [subject words] 
'    ... where SmtpGmailEmail.exe has been placed into PATH ...
'    ... where example c:\a\file.nam could be (minus the ') ... and allowed are To:emailto@whatever.huh and/or Cc:emailcc@whatever.huh and/or Bcc:emailbcc@whatever.huh  ...
'Subject: This is a Windows email about sending an email from the command line
'To Whom It May Concern,
'
'I am concerned that you are concerned, and that concerns me.
'
'I am sorry, but just taking a Bex and lying down just does not hack it for me regarding this problem.
'
'Look forward to your continuing concern, but please do not concern yourself, about this matter.
'
'Yours sincerely,
'
'Worry Wort


Module Module1


    Public smtppass As String = ""
    Public smtpuser As String = "rmetcalfe15"
    Public showmbox As Boolean
    Public dit As Boolean = True
    Public eto As String = ""
    Public ecc As String = ""
    Public ebcc As String = ""
    Public batfile As String
    Public tmpdir As String = (System.Environment.GetEnvironmentVariable("TEMP") + "\*").Replace("\\*", "\").Replace("\*", "\").Replace("*", "")

    Sub Main(ByVal CmdArgs() As String)
        Dim i As Integer
        Dim blurb As String = ""
        Dim blurb2 As String = ""
        Dim inf1 As String = ""
        Dim inf2 As String = ""

        For i = 0 To UBound(CmdArgs)
            If CmdArgs(i).ToString().ToLower().Contains("@gmail.com") Then
                If smtpuser = "rmetcalfe15" Then
                    smtpuser = CmdArgs(i).ToString().Replace("@gmail.com", "").Replace("@GMAIL.COM", "")
                End If
                If eto = "" Then
                    eto = CmdArgs(i).ToString()
                ElseIf ecc = "" Then
                    ecc = CmdArgs(i).ToString()
                ElseIf ebcc = "" Then
                    ebcc = CmdArgs(i).ToString()
                End If
            ElseIf CmdArgs(i).ToString().ToLower().Contains("@") Then
                If eto = "" Then
                    eto = CmdArgs(i).ToString()
                ElseIf ecc = "" Then
                    ecc = CmdArgs(i).ToString()
                ElseIf ebcc = "" Then
                    ebcc = CmdArgs(i).ToString()
                End If
            ElseIf System.IO.File.Exists(CmdArgs(i).ToString()) Then
                If blurb2 = "" Then
                    blurb2 = System.IO.File.ReadAllText(CmdArgs(i).ToString())
                    If blurb2.IndexOf("Subject:") = 0 And blurb2.IndexOf(Chr(10)) > 0 Then
                        blurb = blurb2.Substring(8, blurb2.IndexOf(Chr(10)) - 8)
                        blurb2 = blurb2.Replace("Subject:" + blurb, "")
                    ElseIf blurb2.IndexOf("To:") >= 0 And blurb2.LastIndexOf(Chr(10)) > blurb2.IndexOf("To:") Then
                        eto = blurb2.Substring((blurb2.IndexOf("To:") + 3), blurb2.IndexOf(Chr(10)) - (blurb2.IndexOf("To:") + 3)).Replace(Chr(13), "").Replace(Chr(10), "")
                        blurb2 = blurb2.Replace("To:" + eto, "")
                        eto = eto.Replace(" ", "")
                    ElseIf blurb2.IndexOf("Bcc:") >= 0 And blurb2.LastIndexOf(Chr(10)) > blurb2.IndexOf("Bcc:") Then
                        ebcc = blurb2.Substring((blurb2.IndexOf("To:") + 4), blurb2.IndexOf(Chr(10)) - (blurb2.IndexOf("Bcc:") + 4)).Replace(Chr(13), "").Replace(Chr(10), "")
                        blurb2 = blurb2.Replace("Bcc:" + ebcc, "")
                        ebcc = ebcc.Replace(" ", "")
                    ElseIf blurb2.IndexOf("Cc:") >= 0 And blurb2.LastIndexOf(Chr(10)) > blurb2.IndexOf("Cc:") Then
                        ecc = blurb2.Substring((blurb2.IndexOf("To:") + 3), blurb2.IndexOf(Chr(10)) - (blurb2.IndexOf("Cc:") + 3)).Replace(Chr(13), "").Replace(Chr(10), "")
                        blurb2 = blurb2.Replace("Cc:" + ecc, "")
                        ecc = ecc.Replace(" ", "")
                    End If
                ElseIf inf1 = "" Then
                    inf1 = CmdArgs(i).ToString()
                ElseIf inf2 = "" Then
                    inf2 = CmdArgs(i).ToString()
                End If
            ElseIf smtppass = "" Then
                smtppass = CmdArgs(i).ToString()
            ElseIf blurb = "" Then
                blurb = CmdArgs(i).ToString()
            Else
                blurb = blurb + " " + CmdArgs(i).ToString()
            End If
        Next
        thisEmail_SendSMTP(inf1, inf2, blurb, blurb2, "")

    End Sub

    Public Sub thisEmail_SendSMTP(ByVal infile As String, Optional ByVal infile2 As String = "", Optional ByVal goodblurb As String = "", Optional ByVal goodblurb2 As String = "", Optional ByVal ccours As String = "")
        Dim stSubject As String
        Dim stName As String
        Dim stSender As String
        Dim prevstMessage As Object
        Dim stMessage As Object
        Dim stHelpDesk As String
        Dim stFinished As String
        Dim stPrefix As String
        Dim stSuffix As String
        Dim objMessage

        Dim stCSVHdr As String

        Dim badMsg As String
        Dim goodMsg As String
        Dim noMsg As String

        Dim ftpmode As Integer

        ftpmode = 1
        showmbox = False

        goodMsg = "Your message has been emailed."
        badMsg = "SMTP email not sent because of error."
        noMsg = "No email sent."

        'stOTN = ""
        stCSVHdr = ""
        prevstMessage = ""

        stPrefix = ""
        stSuffix = ""

        stSubject = goodblurb
        stSender = smtpuser + "@gmail.com"
        stName = ""
        stMessage = goodblurb2
        stHelpDesk = smtpuser + "@gmail.com"
        stFinished = goodMsg

        objMessage = CreateObject("CDO.Message")

        stSubject = stSubject & "- " & Replace(infile, Replace(Replace(Environ("TEMP") + "\*", "\\*", "\"), "\*", "\"), "")

        'Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
        'Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).

        'Const cdoAnonymous = 0 'Do not authenticate
        Const cdoBasic = 1 'basic (clear-text) authentication
        'Const cdoNTLM = 2 'NTLM

        objMessage.Subject = stSubject ' & ": " & stName
        objMessage.Sender = stSender
        If eto <> "" Then
            Dim iijj As Integer = eto.IndexOf(";")
            If iijj > 0 Then
                objMessage.To = eto.Substring(0, (iijj - 1))
                eto = (eto + " ").Substring((iijj + 1)).TrimEnd()
                iijj = eto.IndexOf(";")
                If iijj < 0 And eto <> "" Then
                    objMessage.Cc = eto
                Else
                    objMessage.Cc = eto.Substring(0, (iijj - 1))
                    eto = (eto + " ").Substring((iijj + 1)).TrimEnd()
                    iijj = eto.IndexOf(";")
                    If iijj < 0 And eto <> "" Then
                        objMessage.Bcc = eto
                    Else
                        objMessage.Bcc = eto.Substring(0, (iijj - 1))
                    End If
                End If
            Else
                objMessage.To = eto
            End If
        Else
            objMessage.To = stHelpDesk
        End If
        If ecc <> "" Then
            objMessage.Cc = ecc
        ElseIf ccours.Contains("@") = True Then
            objMessage.Cc = ccours.TrimEnd()
            ccours = ""
        End If
        If ebcc <> "" Then
            objMessage.Bcc = ebcc
        ElseIf ccours.Contains("@") = True Then
            objMessage.Bcc = ccours.TrimEnd()
        End If

        Dim isOk As Boolean

        isOk = True

        Dim bigStr As String
        bigStr = ""

        Dim isize As Long
        Dim fileDetail As System.IO.FileInfo
        isize = 0

        Try
            fileDetail = My.Computer.FileSystem.GetFileInfo(infile)
            isize = fileDetail.Length

        Catch exx As Exception

        End Try


        Dim stBig As Object
        stBig = stMessage
        stBig = stPrefix & stCSVHdr & stMessage & stSuffix
        objMessage.TextBody = stBig

        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

        'Name or IP of Remote SMTP Server
        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"

        'Type of authentication, NONE, Basic (Base64 encoded), NTLM
        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

        'Your UserID on the SMTP server
        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusername") = smtpuser

        'Your password on the SMTP server
        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = smtppass

        'Server port (typically 25)
        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465   ' 25

        'Use SSL for the connection (False or True)
        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True   ' False

        'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
        objMessage.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

        objMessage.Configuration.Fields.Update()

        '==End remote SMTP server configuration section==

        'Const cdoRefTypeID = 0
        Const CdoReferenceTypeName = 1

        If stMessage <> "" Then
            If InStr(infile2.ToLower(), ".jp") >= 1 Or InStr(infile2.ToLower(), ".png") >= 1 Or InStr(infile2.ToLower(), ".ico") >= 1 Then
                Dim objBP ' As CDO.IBodyPart
                objBP = objMessage.AddRelatedBodyPart(infile2, (infile2.Substring(infile2.LastIndexOf("\")).Replace("\", "")), CdoReferenceTypeName)
                objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "<" + (infile2.Substring(infile2.LastIndexOf("\")).Replace("\", "")) + "> "
                objBP.Fields.Update()
            End If
            If System.IO.File.Exists(infile) = True Then

                Try
                    stFinished = goodMsg
                    objMessage.Send()

                Catch ex As Exception
                    MsgBox(ex.Message)
                    stFinished = badMsg

                End Try
            Else
                Try
                    objMessage.Send()
                Catch ex2 As Exception
                    MsgBox(ex2.Message)
                End Try
            End If
        Else
            MsgBox(noMsg)
            stFinished = noMsg
        End If

    End Sub

 
End Module

