将二进制/base64数据设置为VB脚本CDO中的附件 [英] Set binary/base64 data as attachment in VB Script CDO.Message

查看:147
本文介绍了将二进制/base64数据设置为VB脚本CDO中的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送带有PDF附件的电子邮件.我的附件数据位于binary/Base64中,但是当我尝试将其写入流时,它不起作用.这是我的代码.我在这里使用上传类-

I am trying to send out an email with attachment as PDF. I have my attachment data in binary/Base64, but when I am trying to write this to the stream, it is not working. Here is my code. I am using upload class from here - 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

我收到以下错误.我不知道我在哪里弄错了.

I get following error. I don't know where am I getting wrong.

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

有人可以帮忙吗?

推荐答案

最后,我完成了这项工作.感谢 kul-tigin 指出了vbArray + vbByte的问题.上传的数据是多字节数据,我需要对其进行规范化.

Finally, I got this working. Thanks to kul-tigin for pointing the issue for vbArray + vbByte. The data uploaded is multi-byte data and I need to normalize it.

这是我在此处获得的功能转换多字节数组.

Here is the function I got here to convert the multi-byte array.

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

我像下面那样更改了代码,但效果似乎很差.

I changed my code like below and it worked just seemlessly.

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中.

I can call File.GetFileDataBinary because I put my GetFileDataBinary in my upload.asp.

这篇关于将二进制/base64数据设置为VB脚本CDO中的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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