2

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.

Can somebody help?

divinedragon
  • 5,105
  • 13
  • 50
  • 97
  • `oStreamOut.Write base64` can you show the code that is populating base64? – Phil Mar 24 '15 at 12:37
  • Did you try to change oStreamOut.Type = 0 – GELR Mar 24 '15 at 12:51
  • What you mean is you have a base64 encoded file and you want to attach it to the e-mail. Problem here is the binary is encoded as base64 which `ADODB.Stream` will not understand first you need to decode the base64 to get back to raw binary. However your code makes no sense if you already have the attachment data why do you need to stream it out? At the moment the code looks back to front. – user692942 Mar 24 '15 at 13:09
  • @GELR The `StreamTypeEnum` is `adTypeBinary = 1` and `adTypeText = 2` there is no `0`. – user692942 Mar 24 '15 at 14:39
  • The base64 data is correct. I am testing that by hard coding that in a variable. ````base64```` stores that correctly. – divinedragon Mar 24 '15 at 14:49
  • I am uploading the file and the file data is in memory in binary. – divinedragon Mar 24 '15 at 15:15
  • 2
    Since you imported the type information with `METADATA` you need to remove the declaration `Const cdoContentDisposition`, otherwise you'll get a runtime error `name redefined`. Then you need to make sure that `varType(uploadFileData) = vbArray + vbByte` because the error `Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.` means you passed an argument with unexpected type. Since your stream is Binary, you need to pass an array of bytes but `uploadFileData` isn't apparently. – Kul-Tigin Mar 25 '15 at 09:02
  • +1 for use of `` so you can use named constants but consider specifying it in the `global.asa` file so it is available to the whole web application and doesn't need defining on a per page basis. Also consider using the `` or similar so you can specify ADO named constants such as `adTypeBinary` etc. – user692942 Mar 25 '15 at 10:13

1 Answers1

1

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

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

Community
  • 1
  • 1
divinedragon
  • 5,105
  • 13
  • 50
  • 97