[英]VBScript to send email via SMTP
I have the following code, my goal is to send automatic emails to a list of people in an excel document, using a text file as a template: 我有以下代码,我的目标是使用文本文件作为模板,将自动电子邮件发送到excel文档中的人员列表:
Set objMessage = CreateObject("CDO.Message")
Set app = CreateObject("Excel.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("F:\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)
subject = "Billing"
LastRow = sh.UsedRange.Rows.Count
For r = row to LastRow
If App.WorkSheetFunction.CountA(sh.Rows(r)) <> 0 Then
objMessage.Subject = "Billing: Meter Read"
objMessage.From = "billing@energia.ie"
objMessage.To = email
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim emailText
Set emailText = fso.OpenTextFile("F:\Billing_Common\autoemail\Script\Email.txt", ForReading)
BodyText = emailText.ReadAll
objMessage.TextBody = emailText
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = CdoSendUsingPort
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "ADDRESS OF SERVER HERE"
'Server port
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
End if
Next
emailText.Close
Set emailText = Nothing
Set fso = Nothing
wb.Close
End If
Next
It throws an error at the objMessage.TextBody, saying type mismatch. 它在objMessage.TextBody上引发错误,提示类型不匹配。 If anyone could help me it would be much appreciated!
如果有人可以帮助我,将不胜感激!
Thanks! 谢谢!
For sending inline images you need to create an HTMLBody
instead of a TextBody
and add a RelatedBodyPart
with the image (see here ): 为了发送内联图像,您需要创建一个
HTMLBody
而不是TextBody
并添加一个与该图像有关的RelatedBodyPart
(请参见此处 ):
Set msg = CreateObject("CDO.Message")
...
msg.HTMLBody = "<html>" & vbLf & _
"<head><title>Test</title></head>" & vbLf & _
"<body><p><img src='foo.jpg'></p></body>" & vbLf & _
"</html>"
msg.AddRelatedBodyPart "C:\path\to\your.jpg", "foo.jpg", 0
After the line BodyText = emailText.ReadAll
, you ought to assign that , and not the file ("emailText" is the TextFile
that was Open
ed by fso
on the previous line), that's why it's complaining about a Type Mismatch... 在
BodyText = emailText.ReadAll
行之后,您应该分配它 ,而不是文件(“ emailText”是前一行由fso
Open
的TextFile
),这就是为什么它抱怨类型不匹配的原因...
So just replace objMessage.TextBody = emailText
with objMessage.TextBody = BodyText
and it should work... 所以只需将
objMessage.TextBody = emailText
替换为objMessage.TextBody = BodyText
,它应该可以工作...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.