Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with script to send email with ms scheduler 1

Status
Not open for further replies.

larosa

MIS
Dec 20, 1999
7
0
0
US
I am trying to automate the execution of an email with an attachment and am at a point of no more ideas. My .vbs code is as follows with the .cfg file contents following. Can someone please help me correct this so I can move on.
Thanks, Mark

***** SendMessageOL.vbs ****
Code:
Option Explicit

Const I_REQ_PARMS = 5

Const S_CONFIG_FILE = "c:\OffisApps\SendMessage\SendMessage.cfg"

Const S_LOG_FILE = "c:\OffisApps\SendMessage\SendMessage.log"

Dim fsoLog 
    
Dim oLogFile 
    
Dim tsLogFile 
    
Dim sLine 
    
Dim sSubject 
    
Dim sText 
    
Dim sPassword 
    
Dim sToRecipient 

Dim sCcRecipient 
    
Dim sAttachment 

Dim iAttachIdx 

Private Sub OpenLogFile()

    Dim sErrMsg 
    
    Set fsoLog = CreateObject("Scripting.FileSystemObject")
    
    Set tsLogFile = fsoLog.CreateTextFile(S_LOG_FILE)

    tsLogFile.WriteLine "SendMessageOl.vbs started at " & Now & vbcrlf

End Sub


Private Function ReadConfigFile()  

    
    Dim fsoConfig 
    
    Dim oConfigFile 
    
    Dim tsConfigFile 
    
    Dim sLine 
    
    Dim iEqPos 
    
    Dim bConfigErr 
    
    Dim iParmCount 
    
    Dim sErrMsg 
    
    Set fsoConfig = CreateObject("Scripting.FileSystemObject")
    
    Set oConfigFile = fsoConfig.GetFile(S_CONFIG_FILE)
    
    Set tsConfigFile = oConfigFile.OpenAsTextStream
    
    bConfigErr = False
    
    iParmCount = 0
    
    Do While Not tsConfigFile.AtEndOfStream
    
        sLine = tsConfigFile.ReadLine
    
        iEqPos = InStr(1, sLine, "=")
        
        If InStr(1, sLine, "TO_RECIPIENT=") > 0 Then
        
            sToRecipient = Trim(Mid(sLine, iEqPos + 1))
            
            iParmCount = iParmCount + 1

        ElseIf InStr(1, sLine, "CC_RECIPIENT=") > 0 Then
        
            sCcRecipient = Trim(Mid(sLine, iEqPos + 1))
            
            iParmCount = iParmCount + 1
          
            
        ElseIf InStr(1, sLine, "TEXT=") > 0 Then
        
            sText = sText & Trim(Mid(sLine, iEqPos + 1)) & vbcrlf
            
            iParmCount = iParmCount + 1
            
            
        ElseIf InStr(1, sLine, "SUBJECT=") > 0 Then
        
            sSubject = Trim(Mid(sLine, iEqPos + 1))
            
            iParmCount = iParmCount + 1
            
        ElseIf InStr(1, sLine, "ATTACHMENT=") > 0 Then
        
            sAttachment = Trim(Mid(sLine, iEqPos + 1))
            
            iParmCount = iParmCount + 1
            
        Else
            sErrMsg = "An invalid parameter, " & _
                      Mid(sLine, 1, iEqPos - 1) & _
                      " was found in the config file, " & _
                      S_CONFIG_FILE & "."
                      
            tsLogFile.WriteLine (sErrMsg)
            
            bConfigErr = True
            
            Exit Do
            
        End If
    
    Loop
    
    tsConfigFile.Close
    
    Set tsConfigFile = Nothing
    
    Set oConfigFile = Nothing
    
    Set fsoConfig = Nothing
    
    If iParmCount < I_REQ_PARMS Or bConfigErr Then
        ReadConfigFile = False
    Else
        ReadConfigFile = True
        
    End If
  
    
End Function


Sub SendMessage(DisplayMsg)
    Dim fsoAttachment
    Dim objOutlook 
    Dim objOutlookMsg 
    Dim objOutlookRecip 
    Dim objOutlookAttach 
    Dim CallDescription 

    ' Create a FileSystemObject
    Set fsoAttachment = CreateObject(&quot;Scripting.FileSystemObject&quot;)

    Set objOutlook = CreateObject(&quot;Outlook.Application&quot;)

    ' Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(0) ''' olMailItem = 0 

    With objOutlookMsg
        ' Add the To recipient(s) to the message.

         .to = sToRecipient

        ' Add the CC recipient(s) to the message.
         If Len( sCcRecipient ) > 0 Then .cc = sCcRecipient

	''If Not .Recipients.ResolveAll Then tsLogFile.WriteLine &quot;No recipients!!!&quot; 

        ' Set the Subject, Body, and Importance of the message.

        .Subject = sSubject

        .Body = sText
		
		
        '.Body = .Body & vbCrLf & &quot;more text&quot; ' repeat this to append more text to the message.

        '' Add attachments to the message. The fully qualified name of the file goes here.
       
        If fsoAttachment.FileExists( sAttachment ) Then
          
           Set objOutlookAttach = .Attachments.Add( sAttachment )

        Else

           tsLogFile.WriteLine &quot;the file does not exist.&quot; & vbcrlf

        End If

       ' Resolve each Recipient's name.

        For Each objOutlookRecip In .Recipients

           if not objOutlookRecip.Resolve then 

             tsLogFile.WriteLine &quot;recipient &quot; & objOutlookRecip.Name & &quot; was NOT resolved.&quot; & vbcrlf

           else

             tsLogFile.WriteLine &quot;recipient &quot; & objOutlookRecip.Name & &quot; was resolved.&quot;

           end if

        Next

       ' Should we display the message before sending? NO!!!

       If DisplayMsg Then

          .Display

       Else

  		  .Send

       End If

    End With

    Set objOutlookRecip = Nothing

    Set objOutlookMsg = Nothing

    Set objOutlook = Nothing

    Set fsoAttachment = Nothing

    tsLogFile.WriteLine &quot;SendMessage.vbs completed at &quot; & Now & vbcrlf
End Sub


Private Sub CloseLogFile()

    tsLogFile.Close
    
    Set tsLogFile = Nothing
    
    Set oLogFile = Nothing
    
    Set fsoLog = Nothing

End Sub

OpenLogFile 

ReadConfigFile

SendMessage False

CloseLogFile


*********** SendMessage.cfg **************
TO_RECIPIENT=Mark
CC_RECIPIENT=
TEXT=Line 1 of some message. Omit if you want to.
TEXT=Line 2 of some message. Omit if you want to.
TEXT=Line N of some message. Omit if you want to.
SUBJECT=VB Send Email Test
ATTACHMENT=C:\OffisApps\SendMessage\stocklst.txt
 
Do you want to send an e-mail from the WEB site?????

try this:
-----------------------------
<% ' Email results to poster
Set Mail2 = Server.CreateObject(&quot;Persits.MailSender&quot;)
Mail2.Host = &quot;smtp.yoursite.com&quot; ' Specify a valid SMTP server
Mail2.From = Email_of_Poster ' Specify sender's address
Mail2.FromName = Name_of_Poster ' Specify sender's name

Mail2.AddAddress &quot;someone@yoursite.com&quot;

Mail2.AddAttachment server.mappath(&quot;yourFilename&quot;) ' Attach a file on the WEB site

Mail2.Subject = &quot;Reply form your Site&quot;
msg = Name_of_submitter & &quot; has posted a response to your &quot; & Subject_of_Current_Post
Mail2.Body = msg
On Error Resume Next
Mail2.Send
If Err <> 0 Then
Response.Write &quot;Error encountered: &quot; & Err.Description
End If
%>
----------------------------- DougP, MCP

Visit my WEB site to see how Bar-codes can help you be more productive
 
Doug,
I'm not trying to send it from our web site. It's an internal network with an external mail server. I'm trying to send a daily email with an attachment to the same individual with a refreshed .txt file.
Thanks,
Mark
 
So all u need is to put the code of DougP in your vb aplication and replace the line
Mail2.AddAttachment server.mappath(&quot;yourFilename&quot;)
with
Mail2.AddAttachment &quot;path_to_yourFilename&quot;
Have a nice day... ________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top