By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

CDO.sys to send mail with base64 encoding ASP help

P: 2
Hi

Can anyone help me with some asp code , I changed the code to use CDO.message instead of the old cdont.sys to send mail from a ASP webpage which works fine.

Our problem is that when we send mail externally to a internet email site like Gmail the PDF is sent but is corrupted because CDOSYS ends up using binary encoding
rather than Base64 encoding when creating the attachment. More information here: Anthonys Code

My knowledge of ASP is basic. The second bit of code is at the bottom is written by Anthony so i am trying to change my code to support base64 with the help of his code but at th moment i am lost

Thanks for any imput :-)

Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. if Session("LoggedIn") = False Then
  4. Response.Redirect("autoexit.htm")
  5. end if
  6. Response.Buffer = False
  7. Server.ScriptTimeout = 310
  8.  
  9. Dim Q, iAtt, bErr, strErrNum, strErrDesc, strFilepath, PCL2PDF, ObjReference
  10. Dim strFrom, strTo, strCC, strSubject, strBody, strAttachments, strFromString, strSubjectString
  11. Dim strCompany, strDocumentname, strInvoicenumber, strVisibleFileName, strExtension 'for building the visible attachment name
  12. Dim strUser, strPassword, strServer, strDatabase 'connection-variables to the database
  13. Dim strShortAttName 'e.g.: 154209.tif
  14. Dim strAttFileName ' e.g.: KundFaktura29875199F3769788749425000.gz
  15. Dim strAttFile 'e.g.: DFC6C9CECAC8CCCACACC154209.tif
  16. Dim dosendmail
  17.  
  18. Q = Chr(34)
  19. iAtt=0
  20. bErr = False
  21. strErrNum = ""
  22. strErrDesc = ""
  23. strFilepath = Application("filepath")+Session("sid")+"\"
  24. PCL2PDF = Request.Cookies("PCL2PDF")
  25. strUser = Session("u")
  26. strPassword = Session("p")
  27. strServer = Request.Cookies("RePrintWebClient")("s")
  28. strDatabase = Request.Cookies("RePrintWebClient")("d")
  29.  
  30. Function AttachmentName(ObjId, FileType)
  31. Dim strFilename 'the real filename in the temp-directory, e.g.: .PDF or .MIME
  32. Dim splitFile 'array
  33. If UCase(FileType)="PCL" Then
  34.  
  35. If PCL2PDF = "No" Then
  36. strFilename = ObjReference.GetAttachmentName(strUser, strPassword, strServer, strDatabase, strFilepath, ObjId, FileType)
  37. Else
  38. strFilename = ObjReference.GetAttachmentNamePDF(strUser, strPassword, strServer, strDatabase, strFilepath, ObjId, FileType)
  39. End If
  40. Else
  41. strFilename = ObjReference.GetAttachmentName(strUser, strPassword, strServer, strDatabase, strFilepath, ObjId, FileType)
  42. End If
  43. If Left(strFilename, 5)="ERROR" Then
  44. strFilename = ""
  45. End if
  46.  
  47. If ucase(right(strFilename,4))="MIME" Then
  48. splitFile = split(ObjReference.GetAttachmentFile( strFilepath+strFilename ),"#")
  49. strShortAttName = splitFile(0)
  50. strAttFileName = splitFile(1)
  51. strAttFile = splitFile(2)
  52. 'strTo = splitFile(3)
  53. 'strCC = splitFile(4)
  54. 'strFromString = splitFile(5)
  55. 'strSubjectString = splitFile(6)
  56. Else
  57. strShortAttName = strFilename
  58. End If
  59. strExtension = right(strShortAttName,len(strShortAttName)-InStrRev(strShortAttName,".")+1)
  60. If ucase(right(strFilename,4))="MIME" Then
  61. strVisibleFileName = left(strFilename,len(strFilename)-5) & strExtension
  62. AttachmentName = strFilepath & ObjReference.GetConfiguration(strServer,strDatabas  e,strUser,strPassword,"mailcompany") & " " & Request.Form("objType") & " " & ObjReference.GetAtt(strUser, strPassword, strServer, strDatabase, strFilepath, ObjId, strAttFileName, strAttFile)
  63. Else
  64. strVisibleFileName = strFilename
  65. AttachmentName = strFilepath & strFilename
  66. End If
  67. End Function
  68.  
  69. Function Sendmail()
  70. Dim Mail, strmailitems, splitmailitems, i, splitmailitem, splitobjid, splitft
  71. strmailitems = Request.Form("mailitems")
  72. splitmailitems = split(strmailitems,";")
  73. Set Mail = CreateObject("CDO.Message")
  74.  
  75.  
  76.  
  77.  
  78. If err.number = 0 then
  79. for i=0 to ubound(splitmailitems) step 1
  80. splitmailitem = splitmailitems(i)
  81. splitft = mid(splitmailitem,instr(splitmailitem,"#")+1)
  82. splitobjid = mid(splitmailitem,1,len(splitmailitem)-len(splitft)-1)
  83. If splitmailitem <> "" Then
  84. Mail.AddAttachment AttachmentName(splitobjid,splitft),strVisibleFileN  ame
  85. iAtt = iAtt + 1
  86. End If
  87. next
  88. Mail.From = strFrom
  89. Mail.To = strTo
  90. Mail.CC = strCC
  91. Mail.Subject = strSubject
  92. ' Mail.BodyFormat = 1
  93. ' Mail.MailFormat = 1
  94. ' Mail.Body= strBody
  95. Mail.Configuration.Fields.Item _
  96. ("h***p://schemas.microsoft.com/cdo/configuration/sendusing")=2
  97.  
  98. 'Name or IP of remote SMTP server
  99. Mail.Configuration.Fields.Item _
  100. ("h***p://schemas.microsoft.com/cdo/configuration/smtpserver") _
  101. ="**********"
  102. 'Server port
  103. Mail.Configuration.Fields.Item _
  104. ("h***p://schemas.microsoft.com/cdo/configuration/smtpserverport") _
  105. =25
  106.  
  107. Mail.Configuration.Fields.Update
  108. Mail.Send
  109. Set Mail = Nothing
  110. Else
  111. bErr = True
  112. strErrDesc = err.description
  113. strErrNum = err.number
  114. End if
  115. Set ObjReference = Nothing
  116. End Function
  117.  
  118. Function GetFormData()
  119. Dim strmailitems, splitmailitems, i, splitmailitem, splitobjid, splitft, strtest, strAhref
  120. strmailitems = Request.Form("mailitems")
  121. strAhref = ""
  122. splitmailitems = split(strmailitems,";")
  123. If err.number = 0 then
  124. for i=0 to ubound(splitmailitems) step 1
  125. splitmailitem = splitmailitems(i)
  126. splitft = mid(splitmailitem,instr(splitmailitem,"#")+1)
  127. splitobjid = mid(splitmailitem,1,len(splitmailitem)-len(splitft)-1)
  128. If splitmailitem <> "" Then
  129. strtest = AttachmentName(splitobjid,splitft)
  130. If UCase(splitft)="MIME" Then
  131. strAhref = strAhref & "<a href=" & Q & "Show Attachment" & Q & " OnClick=" & Q & "ShowAtt('" & splitobjid & "','" & strAttFileName & "','" & strAttFile & "');return false" & Q & "><img border=" & Q & "0" & Q & " src=" & Q & "images/att.gif" & Q & " align=" & Q & "texttop" & Q & ">" & strVisibleFileName & "</a><br>"
  132. Else
  133. strAhref = strAhref & "<a href=" & Q & "Show Attachment" & Q & " OnClick=" & Q & "Show('" & splitobjid & "','" & splitft & "');return false" & Q & "><img border=" & Q & "0" & Q & " src=" & Q & "images/att.gif" & Q & " align=" & Q & "texttop" & Q & ">" & strVisibleFileName & "</a><br>"
  134. End If
  135. strAttachments = strAttachments & strVisibleFileName
  136. iAtt = iAtt + 1
  137. End If
  138. next
  139. Else
  140. bErr = True
  141. strErrDesc = err.description
  142. strErrNum = err.number
  143. End if
  144. Response.Write "<table width=" & Q & "70%" & Q & " bgcolor=" & Q & "#FFFFFF" & Q & " style=" & Q & "border-style:groove;border-width:medium" & Q & ">"
  145. Response.Write "<tr>"
  146. Response.Write "<td width=" & Q & "110" & Q & " valign=" & Q & "top" & Q & "><b>From:</b></td>"
  147. Response.Write "<td title=" & Q & "From" & Q & " valign=" & Q & "top" & Q & "><INPUT type=" & Q & "text" & Q & " name=" & Q & "fldFrom" & Q & " id=" & Q & "fldFrom" & Q & " value=" & Q & strFrom & Q & " style=" & Q & "width:100%" & Q & "></td>"
  148. Response.Write "</tr>"
  149. Response.Write "<tr>"
  150. Response.Write "<td width=" & Q & "110" & Q & " valign=" & Q & "top" & Q & "><b>To:</b></td>"
  151. Response.Write "<td title=" & Q & "To" & Q & " valign=" & Q & "top" & Q & "><INPUT type=" & Q & "text" & Q & " name=" & Q & "fldTo" & Q & " id=" & Q & "fldTo" & Q & " value=" & Q & strTo & Q & " style=" & Q & "width:100%" & Q & "></td>"
  152. Response.Write "</tr>"
  153. Response.Write "<tr>"
  154. Response.Write "<td width=" & Q & "110" & Q & " valign=" & Q & "top" & Q & "><b>Cc:</b></td>"
  155. Response.Write "<td title=" & Q & "Cc" & Q & " valign=" & Q & "top" & Q & "><INPUT type=" & Q & "text" & Q & " name=" & Q & "fldCC" & Q & " id=" & Q & "fldCC" & Q & " value=" & Q & strCC & Q & " style=" & Q & "width:100%" & Q & "></td>"
  156. Response.Write "</tr>"
  157. Response.Write "<tr>"
  158. Response.Write "<td width=" & Q & "110" & Q & " valign=" & Q & "top" & Q & "><b>Subject:</b></td>"
  159. Response.Write "<td title=" & Q & "Subject" & Q & " valign=" & Q & "top" & Q & "><INPUT type=" & Q & "text" & Q & " name=" & Q & "fldSubject" & Q & " id=" & Q & "fldSubject" & Q & " value=" & Q & strSubject & Q & " style=" & Q & "width:100%" & Q & "></td>"
  160. Response.Write "</tr>"
  161. Response.Write "<tr>"
  162. Response.Write "<td width=" & Q & "110" & Q & " valign=" & Q & "top" & Q & "><b>Body:</b></td>"
  163. Response.Write "<td title=" & Q & "Body" & Q & " valign=" & Q & "top" & Q & "><textarea name=" & Q & "fldBody" & Q & " id=" & Q & "fldBody" & Q & " wrap=" & Q & "soft" & Q & " style=" & Q & "width:100%;font-family:Verdana;font-size:8pt" & Q & ">" & strBody & "</textarea></td>"
  164. Response.Write "</tr>"
  165. Response.Write "<tr>"
  166. Response.Write "<td width=" & Q & "110" & Q & " valign=" & Q & "top" & Q & "><b>Attachments:</b></td>"
  167. Response.Write "<td title=" & Q & "View attachment" & Q & " valign=" & Q & "top" & Q & ">"
  168. Response.Write strAhref
  169. Response.Write "</td>"
  170. Response.Write "</tr>"
  171. Response.Write "</table>"
  172. Set ObjReference = Nothing
  173. End Function
  174.  
  175.  
  176. %>
Anthonys Code to send base64 attachments

Expand|Select|Wrap|Line Numbers
  1. Function AddAttachment(Message, Source, FileName, MimeType)
  2.  
  3. Dim oPart ' As IBodyPart
  4. Dim oStreamIn ' As ADODB.Stream
  5. Dim oStreamOut ' As ADODB.Stream
  6.  
  7. Set oPart = Message.Attachments.Add
  8.  
  9. oPart.ContentMediaType = MimeType & "; Name = """ & FileName & """"
  10. oPart.ContentTransferEncoding = "base64"
  11. oPart.Fields("urn:schemas:mailheader:content-disposition").Value =
  12. "attachment; FileName = """ & FileName & """"
  13. oPart.Fields.Update
  14.  
  15. Set oStreamIn = Server.CreateObject("ADODB.Stream")
  16.  
  17. oStreamIn.Open
  18. oStreamIn.Type = adTypeBinary
  19. oStreamIn.LoadFromFile Source
  20.  
  21. Set oStreamOut = oPart.GetDecodedContentStream
  22.  
  23. oStreamIn.CopyTo oStreamOut
  24.  
  25. oStreamOut.Flush
  26.  
  27. oStreamIn.Close
  28.  
  29. Set AddAttachment = oPart
  30.  
  31. End Function
  32.  
  33.  
Dec 20 '07 #1
Share this Question
Share on Google+
1 Reply


P: 2
Fixed it :)

I cant believe it was so simple :)

I just needed to add:

Expand|Select|Wrap|Line Numbers
  1. Mail.HTMLBody = ("message")
This causes the encoding of the mail to change
Dec 20 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.