asp, cdo and attachments

M

MoFo22

Im working on an asp page that retrieves a message(s) from my inbox. The
problem im having is getting to the attachment object. My final goal
is to save the email into a database along with the attachment(s).
Here is the code I have so far.
Thanks.

<%@ LANGUAGE=VBScript %>
<% Option Explicit %>
<HTML>
<BODY>
<P>
<%
Dim objMessage
Dim colRecips
Dim objRecip
Dim objFolder
Dim objSession
Dim intIndex
Dim strFolderID
Dim strFolderName
Dim strTo
Dim strCC
Dim strImportance
Dim collAttachments
Dim iAttachCollIndex
Dim objAttach
Dim objAttachname
Dim objAttachpath
Dim objMsg
Dim Item
Dim brad

strTo = " "
strCC = " "

'Get the information about the message to display
strFolderName = Request.QueryString("Folder")
strFolderID = Session(strFolderName)
intIndex = Cint(Request.QueryString("MsgID"))

'Retrieve the selected message object
Set objSession = Session("CurSession")
Set objFolder = objSession.GetFolder(strFolderID)
Set objMessage = objFolder.Messages(CInt(intIndex))
'set objAttachment = objSession.Attachment(CInt(intIndex))


Set collAttachments = objMessage.Attachments

If collAttachments.count < 1 then
response.write "Unable to set Attachments collection"
Else
response.write "Attachments count for this message: " &
collAttachments.Count

End If


'Process the recipients
Set colRecips = objMessage.Recipients



strImportance = "Normal"
%>

<BR>
<Table border=0 cellspacing=1 cellpadding=1 width="400, 400, 400, *">
<TR>
<td>
<input ID=btnReply NAME=btnReply Type=Button Value="Reply" >
</td>
<td>
<input id=btnReplyAll Name=btnReplyAll Type=Button Value="Replay
All">
</td>
<td>
<input id=btnForward name=btnFormward type=Button Value="Forward">
</td>
</tr>
</table>
<BR>

<table border=0 cellpadding=1 cellspacing=1 width="75%">
<TR>
<td><strong>From:</strong></td>
<td><label><% =objMessage.Sender %></label></td>
</tr>

<tr>
<td><strong>Subject:</strong></td>
<td><label><% =objMessage.Subject %></label></td>
</tr>
<tr>
<td><strong>Importance:</strong></td>
<td><label><% =strImportance %></label></td>
</tr>
<tr>
<td><strong>Message:</strong></td>
<td><textarea ID=textarea1 name=textarea1 style="Height: 390px;
width: 547px"><% =objMessage.Text %></textarea></td>
<td><label><% =strImportance %></label></td>
</tr>
<tr>
<td><strong>Attachment:</strong></td>
<td><label><% =objAttach %></label></td>

</tr>

</table>
</p>

</body>
</html>
<script id=clientEventHandlersVBS Language=VBScript>

Sub btnReply_onClick()
strWhat = "Reply"
end Sub

Sub btnReplyAll_onClick()
strWhat = "ReplyAll"
end Sub

Sub btnForward_onClick()
strWhat = "Forward"
End sub
</script>
 
R

Ray at

yourcode> If collAttachments.count < 1 then
yourcode> response.write "Unable to set Attachments collection"
yourcode> Else

For Each oAttachment In collAttachments
oAttachment.SaveAsFile "D:\Path\" & oAttachment.FileName
Next

yourcode> response.write "Attachments count for this message: " &
yourcode> collAttachments.Count
yourcode>
yourcode> End If


That would be the basic way to loop through the attachment(s) in the message
and save them to a specified path using the same filename as what it's
called in the message. You wanted to save the files to a DB though. I'm
not into that. Saving files to the file system would be a much, much better
approach, imo.

If you save them to the file system, you'll want to deal with the
possibility of duplicate file names, like, maybe do something like:


....
Set oFSO = CreateObject("Scripting.FileSystemObject")
sSaveToPath = "D:\RootForFiles\" & objMessage.EntryID & "\"
If Not oFSO.FolderExists(sSaveToPath) Then oFSO.CreateFolder(sSaveToPath)
Set oFSO = Nothing
For Each oAttachment In collAttachments
oAttachment.SaveAsFile sSaveToPath & oAttachment.FileName
Next
....

Ray at home
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top