Send email with MULTIPLE ATTACHMENTS using CDO

Posted on 2004-10-21
Last Modified: 2012-05-05
I would like to have an ASP page that can send email with multiple attachment. Below is my code with 1 attachment and it is working.

SendMsg Mailto, MailCC, MailBCC, MailFrom, MailSub, MsgBody,"C:\\InetPub\wwwroot\"&mid(File.Path,35,len(File.Path))"
Question by:CochiseCounty
    LVL 15

    Accepted Solution

    your going to have to change your sendmsg subroutine to accept an array of filenames, then loop through the file names and add the attacments.

    Could you post your sendmsg subroutine
    LVL 4

    Assisted Solution


    If you just modify your routine, as I have given an example of below, to check to see whether the attachment argument is a string or an array, you can allow multiple attachments or single attachments, without breaking any code that is only using single.

    Here is my sample SendMsg subroutine:

    Sub SendMsg (to, cc, bcc, from, subj, body, attach)
          Dim oMail
          Dim oFs
          Dim sAttach

          Set oMail = Server.CreateObject("CDONTS.NewMail")
          oMail.To = to
          If cc <> "" Then
                oMail.CC = cc
          End If
          If bcc <> "" Then
                oMail.BCC = bcc
          End If

          oMail.From = from
          If subj = "" Then
                oMail.Subject = "[no subject]"
                oMail.Subject = subj
          End If

          oMail.Body = body

          Set oFs = Server.CreateObject("Scripting.FileSystemObject")
          On Error Resume Next
          If IsArray(Attach) Then
                For i = 0 To Ubound(Attach)
                      sAttach = ""
                      If oFs.FileExists(attach(i)) Then
                            sAttach = attach(i)
                      ElseIf oFs.FileExists(Server.MapPath(attach(i))) Then
                            sAttach = Server.MapPath(attach(i))
                      End If
                      If sAttach <> "" Then
                            oMail.AttachFile sAttach
                      End If
                sAttach = ""
                If attach <> "" Then
                      If oFs.FileExists(attach) Then
                            sAttach = attach
                      ElseIf oFs.FileExists(Server.MapPath(attach)) Then
                            sAttach = Server.MapPath(attach)
                      End If

                      If sAttach <> "" Then
                            oMail.AttachFile sAttach
                      End If
                End If
          End If

          On Error GoTo 0
          Set oFs = Nothing


          Set oMail = Nothing
    End Sub

    - misty

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now