Sending Mail attachment with VBScript

I am trying to attach a file when I send an email.  The email sends fine unless I add the attachment line, then it breaks.  All of the examples I've seen require a hard path except the line I use below with Server.MapPath.  This doesn't seem to work.  Please tell me what I'm doing wrong here.  The file is located in the directory that I'm running this asp from.

Set objSession = CreateObject("CDONTS.Session")
        objSession.LogonSMTP fromName, fromEmail
            'objSession.LogonSMTP fromname, fromemail
       
        Set objOutbox = objSession.Outbox
        Set objMessage = objOutbox.Messages.Add
        objMessage.Subject = esubject
        objMessage.Text = eMsgString
        objMessage.AddAttachment (Server.MapPath("text.txt"))
        'objMessage.Importance = CdoHigh
            
        if recip1= "true" then
            objMessage.Recipients.Add "recip1",etoaddr, cdoTo
        end if
        if recip2= "true" then
            objMessage.Recipients.Add "recip2",etoaddr2, cdoTo
        end if

        objMessage.Send
leawassonAsked:
Who is Participating?
 
Computer101Connect With a Mentor Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
 
hc0904pcdCommented:
try this after objMessage.Text = eMsgString

strPath = Server.MapPath(".")
if attachment1 <> "" then objMessage.AddAttachment strPath & "\" & "attachment_filename"

0
 
hc0904pcdCommented:
Perhaps also test it with a word file.
I"m not a hundred per cent sure how a txt attachment will be treated.
Have you set the email to html, or text?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
hc0904pcdCommented:
When it breaks, what is the error msg?
0
 
jarwCommented:
Do you have to use CDONTS or can you use CDOSYS? An example with CDOSYS is given below. It might be interesting to see if it fails the same way.
Set mail = CreateObject("CDO.Message")
 
' Configure to use an external mail server (2), supply its domain name or IP
' address (your.smtp.server), and tell it which port to use (usally 25).
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your.smtp.server"
mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
mail.Configuration.Fields.Update()
 
mail.Subject = "Test Email"
mail.From = "yourname@somewhere.com"
mail.To = "anybody@somewhereelse.com"
mail.TextBody = "Hello World!"
 
mail.AddAttachment Server.MapPath("text.txt")
 
mail.Send()
 
Set mail = Nothing

Open in new window

0
 
leawassonAuthor Commented:
Sorry for the slow response.  I was taken off this project for a few weeks and haven't had a chance to test the solutions you offered.  I tried all of the suggestions and nothing seemed to work.  I ended up using the following solution (switching to CDONTS.Newmail).

Dim physicalPath
physicalPath = Server.MapPath(".")
Dim fullPath
fullPath = physicalPath & "\my.pdf"            
            
Set objMail = CreateObject("CDONTS.Newmail")
objMail.From = fromEmail
objMail.To = etoaddr3
objMail.Subject = "Requested PDF"
objMail.Body = "you email body"
objMail.AttachFile fullPath, "Your PDF"
objMail.Send
Set objMail = Nothing

Thank  you again very much for you suggestions.
0
All Courses

From novice to tech pro — start learning today.