Email VBScript通过SMTP发送电子邮件
我有以下代码,我的目标是使用文本文件作为模板,向excel文档中的人员列表发送自动电子邮件:Email VBScript通过SMTP发送电子邮件,email,vbscript,smtp,Email,Vbscript,Smtp,我有以下代码,我的目标是使用文本文件作为模板,向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").
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
Set objMessage=CreateObject(“CDO.Message”)
Set app=CreateObject(“Excel.Application”)
设置fso=CreateObject(“Scripting.FileSystemObject”)
对于fso.GetFolder(“f:\Billing\u Common\autoemail”)文件中的每个f
如果LCase(fso.GetExtensionName(f))=“xls”,则
设置wb=app.Workbooks.Open(f.Path)
设置sh=wb.Sheets(“自动电子邮件脚本”)
行=2
email=sh.Range(“A”行和行)
主题=“计费”
LastRow=sh.UsedRange.Rows.Count
对于r=行到最后一行
如果App.WorkSheetFunction.CountA(sh.Rows(r))为0,则
objMessage.Subject=“计费:仪表读数”
objMessage.From=”billing@energia.ie"
objMessage.To=电子邮件
读取常数=1,写入常数=2,外观常数=8
Dim emailText
设置emailText=fso.OpenTextFile(“F:\Billing\u Common\autoemail\Script\Email.txt”,用于阅读)
BodyText=emailText.ReadAll
objMessage.TextBody=emailText
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/sendusing“”=CdoSendUsingPort
'远程SMTP服务器的名称或IP
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/smtpserver“”=“此处的服务器地址”
'服务器端口
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
如果结束
下一个
emailText,关闭
设置emailText=Nothing
设置fso=无
wb.关闭
如果结束
下一个
它在objMessage.TextBody上抛出一个错误,表示类型不匹配。如果有人能帮助我,我将不胜感激
谢谢 要发送内联图像,您需要创建
HTMLBody
而不是TextBody
,并在图像中添加RelatedBodyPart
(请参阅):
Set msg=CreateObject(“CDO.Message”)
...
msg.HTMLBody=”“&vbLf&_
“测试”&vbLf&_
“”&vbLf&_
""
msg.AddRelatedBodyPart“C:\path\to\your.jpg”,“foo.jpg”,0
要发送内联图像,您需要创建HTMLBody
而不是TextBody
,并在图像中添加RelatedBodyPart
(请参阅):
Set msg=CreateObject(“CDO.Message”)
...
msg.HTMLBody=”“&vbLf&_
“测试”&vbLf&_
“”&vbLf&_
""
msg.AddRelatedBodyPart“C:\path\to\your.jpg”,“foo.jpg”,0
在BodyText=emailText.ReadAll
行之后,您应该分配它,而不是文件(“emailText”是前一行由fso
打开的文本文件),这就是它抱怨类型不匹配的原因
因此,只需将objMessage.TextBody=emailText
替换为objMessage.TextBody=BodyText
,它应该可以工作…在BodyText=emailText.ReadAll
行之后,您应该分配它,而不是分配文件(“emailText”是前一行中由fso
编辑的TextFile
,这就是它抱怨类型不匹配的原因
因此,只要用objMessage.TextBody=emailText
替换objMessage.TextBody=BodyText
,它就应该可以工作了…我现在已经可以工作了,但无法让它使用文本文件作为模板。现在一切都正常了,但有人知道如何将图像添加到其中吗?作为附件?或者你想撰写HTML邮件?我想它必须是HTML邮件,因为它需要在文档中。我现在有它的工作,但不能让它使用文本文件作为模板。所有这些现在都在工作,但有人知道如何添加图像吗?作为附件?或者你想撰写HTML邮件?我认为它必须是HTML邮件,因为它需要在文档中
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