Forum Discussion
rmanning
15 years agoContributor
Okay, that works (somewhat) but I'm a complete moron when it comes to VBScript, so how do I use the Email function in combination with creating this file list to email attachements? My code is as follows:
Function ListFiles(folder)
Dim oFolder, colFiles, f
Set oFolder = aqFileSystem.GetFolderInfo(folder)
Set colFiles = oFolder.Files
strFileList = ""
aqString.ListSeparator = ","
While colFiles.HasNext
Set f = colFiles.Next
strFileList = aqString.AddListItem(strFileList, f.Name)
Wend
ListFiles = strFileList
End Function
Function SendEmail(mFrom, mTo, mSubject, mBody, mAttachment)
Err.Clear
On Error Resume Next
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set mConfig = Sys.OleObject("CDO.Configuration")
mConfig.Fields.Item(schema + "sendusing") = 2 ' cdoSendUsingPort
mConfig.Fields.Item(schema + "smtpserver") = "my.smtp.server" ' SMTP server
mConfig.Fields.Item(schema + "smtpserverport") = 25 ' Port number
' mConfig.Fields.Item(schema + "sendusername") = "" ' User name (if needed)
' mConfig.Fields.Item(schema + "sendpassword") = "" ' User password (if needed)
mConfig.Fields.Update()
Set mMessage = Sys.OleObject("CDO.Message")
mMessage.Configuration = mConfig
mMessage.From = mFrom
mMessage.To = mTo
mMessage.Subject = mSubject
mMessage.HTMLBody = mBody
aqString.ListSeparator = ","
For i = 0 To aqString.GetListLength(mAttachment) - 1
mMessage.AddAttachment aqString.GetListItem(mAttachment, i)
Next
mMessage.Send
If Err.Number > 0 Then
Log.Error "E-mail cannot be sent", Err.Description
SendEMail = False
Else
Log.Message "Message to <" + mTo + "> was successfully sent"
SendEMail = True
End If
End Function
Sub MainTest
If SendEmail("do-not-reply@mail.com", "automated@test.com", "Automated Testing", _
"This is a test message.", ListFiles("C:/Documents and Settings/User/My Documents/TestComplete 8 Projects/Automation/Screenshots/")) Then
' Message was sent
Else
' Message was not sent
End If
End Sub
The script runs without error, but the attachements are not being sent (says "noname" as an attachment).
UPDATE: I know what the issue is. The comma seperator is being placed at the begining of the list of files, so they show up as ",C:\temp\1.jpg, C:\temp\2.jpg, C:\temp\3.jpg" ect. I need to remove that comma at the begining and it should work...
Function ListFiles(folder)
Dim oFolder, colFiles, f
Set oFolder = aqFileSystem.GetFolderInfo(folder)
Set colFiles = oFolder.Files
strFileList = ""
aqString.ListSeparator = ","
While colFiles.HasNext
Set f = colFiles.Next
strFileList = aqString.AddListItem(strFileList, f.Name)
Wend
ListFiles = strFileList
End Function
Function SendEmail(mFrom, mTo, mSubject, mBody, mAttachment)
Err.Clear
On Error Resume Next
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set mConfig = Sys.OleObject("CDO.Configuration")
mConfig.Fields.Item(schema + "sendusing") = 2 ' cdoSendUsingPort
mConfig.Fields.Item(schema + "smtpserver") = "my.smtp.server" ' SMTP server
mConfig.Fields.Item(schema + "smtpserverport") = 25 ' Port number
' mConfig.Fields.Item(schema + "sendusername") = "" ' User name (if needed)
' mConfig.Fields.Item(schema + "sendpassword") = "" ' User password (if needed)
mConfig.Fields.Update()
Set mMessage = Sys.OleObject("CDO.Message")
mMessage.Configuration = mConfig
mMessage.From = mFrom
mMessage.To = mTo
mMessage.Subject = mSubject
mMessage.HTMLBody = mBody
aqString.ListSeparator = ","
For i = 0 To aqString.GetListLength(mAttachment) - 1
mMessage.AddAttachment aqString.GetListItem(mAttachment, i)
Next
mMessage.Send
If Err.Number > 0 Then
Log.Error "E-mail cannot be sent", Err.Description
SendEMail = False
Else
Log.Message "Message to <" + mTo + "> was successfully sent"
SendEMail = True
End If
End Function
Sub MainTest
If SendEmail("do-not-reply@mail.com", "automated@test.com", "Automated Testing", _
"This is a test message.", ListFiles("C:/Documents and Settings/User/My Documents/TestComplete 8 Projects/Automation/Screenshots/")) Then
' Message was sent
Else
' Message was not sent
End If
End Sub
The script runs without error, but the attachements are not being sent (says "noname" as an attachment).
UPDATE: I know what the issue is. The comma seperator is being placed at the begining of the list of files, so they show up as ",C:\temp\1.jpg, C:\temp\2.jpg, C:\temp\3.jpg" ect. I need to remove that comma at the begining and it should work...
Related Content
- 7 years ago
- 13 years ago
Recent Discussions
- 18 hours ago
- 4 days ago