[英]VBScript SMTP Auto Email
I have a script to auto email a list of address' stored in Excel, but it is only sending to the first address and not looping to the rest, I cannot seem to fix it: 我有一个脚本来自动发送一个存储在Excel中的地址列表,但它只是发送到第一个地址而不是循环到其余地址,我似乎无法解决它:
Set objMessage = CreateObject("CDO.Message")
Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("Y:\Billing_Common\autoemail").Files
If LCase(fso.GetExtensionName(f)) = "xls" Then
Set wb = app.Workbooks.Open(f.Path)
set sh = wb.Sheets("Auto Email Script")
row = 2
email = sh.Range("A" & row)
LastRow = sh.UsedRange.Rows.Count
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim f
Set f = fso.OpenTextFile("Y:\Billing_Common\autoemail\Script\Email.txt", ForReading)
BodyText = f.ReadAll
For r = row to LastRow
If App.WorkSheetFunction.CountA(sh.Rows(r)) <> 0 Then
objMessage.Subject = "Billing: Meter Read"
objMessage.From = "billing@energia.ie"
row = row + 1
objMessage.To = email
objMessage.TextBody = BodyText
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") = "SERVER ADDRESS HERE"
'Server port
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
End if
Next
f.Close
Set f = Nothing
Set fso = Nothing
wb.Close
End If
Next
Any help would be much appreciated guys! 任何帮助将非常感谢伙计!
Thanks! 谢谢!
row = 2
email = sh.Range("A" & row)
...
For r = row to LastRow
...
objMessage.To = email
...
Next
You set email
to the value of the cell "A2"
and never change it. 您将
email
设置为单元格"A2"
的值,并且永远不会更改它。 If you want to send a mail to multiple recipients, you should make that 如果您想向多个收件人发送邮件,您应该这样做
objMessage.To = sh.Range("A" & r).Value
or (better) build a recipient list (assuming that your used range starts with headers in the first table row): 或者(更好)构建一个收件人列表(假设你使用的范围以第一个表格行中的标题开头):
ReDim recipients(LastRow - row)
For r = row To LastRow
recipients(r - row) = sh.Range("A" & r).Value
Next
objMessage.To = Join(recipients, ";")
and send the message just once. 并只发送一次消息。 The MTA will handle the rest.
MTA将处理其余的事情。
Side note: as Vishnu Prasad Kallummel pointed out in the comments your code doesn't close the Excel instance it started. 旁注:正如Vishnu Prasad Kallummel在评论中指出的那样,您的代码不会关闭它启动的Excel实例。 Unlike other objects created in VBScript, Office applications won't automatically terminate with the script, so you have to handle it yourself:
与在VBScript中创建的其他对象不同,Office应用程序不会自动终止脚本,因此您必须自己处理它:
...
wb.Close
app.Quit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.