We help IT Professionals succeed at work.

Sending Mail attachment with VBScript

leawasson asked
Medium Priority
Last Modified: 2010-05-18
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

Watch Question

try this after objMessage.Text = eMsgString

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

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?
When it breaks, what is the error msg?

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.Subject = "Test Email"
mail.From = "yourname@somewhere.com"
mail.To = "anybody@somewhereelse.com"
mail.TextBody = "Hello World!"
mail.AddAttachment Server.MapPath("text.txt")
Set mail = Nothing

Open in new window


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"
Set objMail = Nothing

Thank  you again very much for you suggestions.
PAQed with points refunded (500)

EE Admin

Explore More ContentExplore courses, solutions, and other research materials related to this topic.