使用以下vbscript在邮件正文中添加新行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用以下vbscript在邮件正文中添加新行相关的知识,希望对你有一定的参考价值。

我创建了一个txt文件来支持vbscript的消息体,但它只读取了messagebody.txt的最后一行

    WScript.Sleep 100

    Set WshShell=WScript.CreateObject("WScript.Shell")

    Set objShell=WScript.CreateObject("WScript.Shell")
    set objOutlook=CreateObject("Outlook.Application")
    Set objMail=CreateObject("CDO.Message")

Set objMail=objOutlook.CreateItem(0)
strDesktop = WshShell.SpecialFolders("Desktop")


Set objFileToReadTo          = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\send email with attachmentList_To.txt",1)
Set objFileToReadCC          = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\send email with attachmentList_CC.txt",1)
Set objFileToReadSubject     = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\send email with attachmentList_Subject.txt",1)
Set objFileToReadBody        = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\send email with attachmentEmail Body.txt",1)
Set objFileToReadAttachments = CreateObject("Scripting.FileSystemObject").OpenTextFile(strDesktop + "\send email with attachmentList_Attachments_withFileExtension.txt",1)

Dim strLineTo
Dim strLineCC
Dim strLineSubject
Dim strLineBody
Dim strLineAttachments

objMail.Display

WScript.Sleep 10

do while not objFileToReadTo.AtEndOfStream
strLineTo = objFileToReadTo.ReadLine()
objMail.To=strLineTo
loop
objFileToReadTo.Close 

WScript.Sleep 10
do while not objFileToReadCC.AtEndOfStream
strLineCC = objFileToReadCC.ReadLine()
objMail.cc = strLineCC
loop
objFileToReadCC.Close 
'41
WScript.Sleep 10
do while not objFileToReadSubject.AtEndOfStream
strLineSubject = objFileToReadSubject.ReadLine()
objMail.Subject = strLineSubject
loop
objFileToReadSubject.Close 
'48
WScript.Sleep 10
do while not objFileToReadBody.AtEndOfStream
strLineBody = objFileToReadBody.ReadLine()
objMail.Body = strLineBody & vbCRLF  
loop
objFileToReadBody.Close 
'55
WScript.Sleep 10
do while not objFileToReadAttachments.AtEndOfStream
strLineAttachments = objFileToReadAttachments.ReadLine()
objMail.Attachments.Add(strLineAttachments)
loop
objFileToReadAttachments.Close 
'62

'objShell.Sendkeys "%s"
WScript.Sleep 40
'objShell.SendKeys "{TAB}"
'objShell.SendKeys "{UP}"
'objShell.SendKeys "{Enter}"
'set MyEmail=nothing
'objOutlook.Quit
'Set objMail = Nothing
'Set objOutlook = Nothing

这是我的messagebody.txt

Hi,

Testing vbscript

Regards,

abcd

它只读取最后一个ABCD并在Outlook窗口中显示相同的内容。

如何让脚本理解多行?

答案

我真的不知道为什么你使用不同的文本文件存储ToList,CCList,body等,但如果你确定使用这种方法,我不会改变它。

我只是指出为什么你没有在电子邮件正文中获得全文。替换以下代码:

do while not objFileToReadBody.AtEndOfStream
    strLineBody = objFileToReadBody.ReadLine()            'Here you are just overwriting the value contained in strLineBody in each loop iteration. Hence, in the end, only last line is left in this variable
    objMail.Body = strLineBody & vbCRLF  
loop

WITH

objMail.Body = objFileToReadBody.readAll
另一答案

在循环中,您应该将Body替换为您读取的每一行,当您应该附加它时。切换这条线;

objMail.Body = strLineBody & vbCRLF

成为;

objMail.Body = objMail.Body & strLineBody & vbNewLine

如果你放弃循环并使用ReadAll(因为@Gurmansuggested),请记住,虽然这对于最小的文本来说没问题,但是较大的文本文件将使得该过程效率低于循环开始已经开始的每一行。

以上是关于使用以下vbscript在邮件正文中添加新行的主要内容,如果未能解决你的问题,请参考以下文章

添加到Outlook电子邮件正文的超链接

使用 Python 根据磁盘使用情况在 HTML 表格电子邮件正文中添加标题和文本颜色

使用 c# windows 应用程序在电子邮件正文中添加多个图像(内联)

在 SMTP 电子邮件 VB.NET 的正文中添加多行

从excel范围vba添加邮件正文

在邮件正文中插入图像