发送带有正文作为文本文件内容的 Outlook 电子邮件 [英] Sending outlook email with body as contents of a text file

查看:45
本文介绍了发送带有正文作为文本文件内容的 Outlook 电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 VBScript 发送 Outlook 电子邮件.电子邮件的正文应包含文本文件的内容,例如 sha.txt.下面是我正在使用的代码,但它给了我这个错误:

I want to send an outlook email using VBScript. The body of the email should contains the contents of a text file, say sha.txt. Below is the code I am using but it's giving me this error:

运行时错误287":应用程序定义或对象定义错误

Run Time error '287': Application-defined or Object defined error

Sub email1()   
  Dim outobj, mailobj    
  Dim strFileText 
  Dim objFileToRead    

  Set outobj = CreateObject("Outlook.Application")    
  Set mailobj = outobj.CreateItem(0)    
  Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\sonu\Desktop\auto\sha.txt", 1)    
  strFileText = objFileToRead.ReadAll()
  objFileToRead.Close
  Set objFileToRead = Nothing    
  With mailobj    
    .To = "user@user.com"    
    .Subject = "Testmail"    
    .Body = strFileText    
    .Send    
  End With    

  'Clear the memory
  Set outobj = Nothing    
  Set mailobj = Nothing    
End Sub

推荐答案

    'I didn't look into the particular issue with your file reading.
    'Below my example, just tested it works.
    'Good luck mate :)

        Sub CatchMe()

          Dim outobj, mailobj
          Dim strFileText
          Dim objFileToRead

          Set outobj = CreateObject("Outlook.Application")
          Set mailobj = outobj.CreateItem(0)
          strFileText = GetText("C:\Share\1.txt")

            With mailobj
            .To = "user@user.com"
            .Subject = "Testmail"
            .Body = strFileText
            .Display
          End With

          'Clear the memory
          Set outobj = Nothing
          Set mailobj = Nothing

        End Sub

        Function GetText(sFile As String) As String
           Dim nSourceFile As Integer, sText As String
           nSourceFile = FreeFile
           'Write the entire file to sText
           Open sFile For Input As #nSourceFile
           sText = Input$(LOF(1), 1)
           Close
           GetText = sText
        End Function

这篇关于发送带有正文作为文本文件内容的 Outlook 电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆