【问题标题】:Set binary/base64 data as attachment in VB Script CDO.Message在 VB Script CDO.Message 中将二进制/base64 数据设置为附件
【发布时间】:2015-05-27 17:33:10
【问题描述】:

我正在尝试发送一封带有 PDF 附件的电子邮件。我有二进制/Base64 格式的附件数据,但是当我尝试将其写入流时,它不起作用。这是我的代码。我正在使用来自这里的上传类 - http://www.codeguru.com/csharp/.net/net_asp/article.php/c19297/Pure-ASP-File-Upload.htm

<!--METADATA TYPE="TypeLib" FILE="C:\windows\system32\cdosys.dll" -->
<!-- #include file="upload.asp" -->
<%

' Create the FileUploader
Dim Uploader, File
Set Uploader = New FileUploader

' This starts the upload process
Uploader.Upload()

Dim uploadFileName
Dim uploadFileData
' Check if any files were uploaded
If Uploader.Files.Count > 0 Then
    ' Loop through the uploaded files
    For Each File In Uploader.Files.Items
        uploadFileName = File.FileName
        uploadFileData = File.FileData
    Next
End If



Set ObjMailer = CreateObject("CDO.Message")
Set iConf = Server.CreateObject("CDO.Configuration")

Set Flds = iConf.Fields
With Flds
    .Item(cdoSendUsingMethod) = cdoSendUsingPort
    .Item(cdoSMTPServer) = "localhost"
    .Item(cdoSMTPServerPort) = 5000
    .Item(cdoSMTPconnectiontimeout) = 10
    .Update
End With

Set ObjMailer.Configuration = iConf

ObjMailer.Subject = "Subject"
ObjMailer.From = "from@testdomain.com"
ObjMailer.To = "to@testdomain.com"
ObjMailer.HTMLBody  = "<h1>This is a sample html email body</h1>"

' Code to Attach the Email
Const cdoContentDisposition = "urn:schemas:mailheader:content-disposition"

Dim oPart : Set oPart = ObjMailer.Attachments.Add
oPart.ContentMediaType = "application/pdf"

oPart.Fields(cdoContentDisposition).Value = "attachment;filename=""Test.pdf"""
oPart.Fields.Update

Dim oStreamOut: Set oStreamOut = oPart.GetDecodedContentStream
oStreamOut.Type = 1
oStreamOut.Write uploadFileData
oStreamOut.Flush

ObjMailer.Send
Set ObjMailer = Nothing
Set iConf = Nothing
Set Flds = Nothing
If Err.number <> 0 Then
    Response.Write "Error: " & Err.Number
    Response.Write "Error (Hex): " & Hex(Err.Number)
    Response.Write "Source: " &  Err.Source
    Response.Write "Description: " &  Err.Description
End If

我收到以下错误。我不知道我哪里错了。

ADODB.StreamDescription: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

有人可以帮忙吗?

【问题讨论】:

  • oStreamOut.Write base64 你能显示填充base64的代码吗?
  • 您是否尝试更改 oStreamOut.Type = 0
  • 你的意思是你有一个base64编码的文件,你想把它附加到电子邮件中。这里的问题是二进制编码为 base64,ADODB.Stream 首先无法理解,您需要解码 base64 以返回原始二进制。但是,如果您已经拥有附件数据,那么您的代码将毫无意义,为什么需要将其流出?目前,代码回溯到前面。
  • @GELR StreamTypeEnumadTypeBinary = 1adTypeText = 2 没有 0
  • 由于您使用METADATA 导入类型信息,您需要删除声明Const cdoContentDisposition,否则您将收到运行时错误name redefined。然后你需要确保varType(uploadFileData) = vbArray + vbByte 因为错误Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another. 意味着你传递了一个意外类型的参数。由于您的流是二进制的,因此您需要传递一个字节数组,但 uploadFileData 显然不是。

标签: iis vbscript asp-classic cdo.message


【解决方案1】:

最后,我得到了这个工作。感谢kul-tiginvbArray + vbByte 指出问题。上传的数据是多字节数据,我需要对其进行归一化处理。

这是我得到的here 转换多字节数组的函数。

Function GetFileDataBinary()

    Dim RS, LMultiByte, Binary
    Const adLongVarBinary = 205
    Set RS = CreateObject("ADODB.Recordset")
    LMultiByte = LenB(FileData)
    If LMultiByte > 0 Then
        RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
        RS.Open
        RS.AddNew
        RS("mBinary").AppendChunk FileData & ChrB(0)
        RS.Update
        Binary = RS("mBinary").GetChunk(LMultiByte)
    End If

    GetFileDataBinary = Binary
End Function

我像下面这样更改了我的代码,但它运行得非常顺畅。

Dim uploadFileName
Dim uploadFileData
' Check if any files were uploaded
If Uploader.Files.Count > 0 Then
    ' Loop through the uploaded files
    For Each File In Uploader.Files.Items
        uploadFileName = File.FileName
        uploadFileData = File.GetFileDataBinary
    Next
End If

我可以打电话给File.GetFileDataBinary,因为我把我的GetFileDataBinary放在了我的upload.asp中。

【讨论】:

    猜你喜欢
    • 2017-07-14
    • 2013-06-18
    • 2012-06-14
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多