How do I email an attachment using cdo.message

swansonplace used Ask the Experts™
How to I add an email attachment on a website.  I want to add a document that is on a website, but I
do not know how to format the line to show show it is from a website not my local hard drive.

Error Line trying to correct:
 .AddAttachment = "C:\Project_Overview_Apr2.pdf"               'This line of code is incorrect

Current Code:
Function SendMail (outErrorMessage)
' Purpose: To send email messages  
' Assumption: p_body, p_fromWho, and p_ToWho have data in them
' Output: SendMail  
'                 SendMail = 1 if error exists
'                 SendMail = 0 if no error exists

    Dim ReturnCode    
    ReturnCode = 1
'   Validate that the data has been filled in properly
    If len(p_body) < 1 or len(p_fromWho) < 1 or len(p_ToWho) < 1 Then          
       outErrorMessage = "The information to send an email was blank: message body, fromWho, or toWho"         
       SendMail = ReturnCode  
       Exit Function
    End If
    Set cdoConfig = CreateObject("CDO.Configuration")  
    With cdoConfig.Fields  
        .Item(cdoSendUsingMethod) = cdoSendUsingPort        
        .Item(cdoSMTPServer) = "888.88.8.88"  
    End With
    Set cdoMessage = CreateObject("CDO.Message")  
    With cdoMessage
        Set .Configuration = cdoConfig
        .From = p_fromWho
        .To = p_toWho
        .Subject = p_Subject
        '.TextBody = p_Body
        .HTMLBody = "This is a <b>test</b> of an html body"
        .AddAttachment = "C:\Project_Overview_Apr2.pdf"               'This line of code is incorrect
    End With
    Set cdoMessage = Nothing  
    Set cdoConfig = Nothing  
    ReturnCode = 0
    SendMail = ReturnCode  
END Function
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It has to be on the server machine before it can be attached.
So, you may wish to look into doing a file upload to server from client to server and then attempting to do a mail attachment.

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

PS :
You need to use a fully qualified path to attach files:
With objMail
     .To       = strMailTo
     .CC       = strCopyTo
     .From    = strLoginID
     .Subject  = FORM_NAME
     .HtmlBody = strBody
     .AddAttachment Server.Mappath("Project_Overview_Apr2.pdf")
End With
Ok.. After reading your question again, it seems the file is already on your server.
Then please follow kelvinwkw's instruction.

swansonplace, classic ASP would be running at the web server already, so your path of C:\Project_Overview_Apr2.pdf would correctly reference a file named Project_Overview_Apr2.pdf on the root of the C: drive on the web server.  Does the file exist in that location?


I do not know the directory structure on the server.  I have access to my webpage area.  Is it possible to give a path relative to the website:

I tried this it does not work:
.AddAttachment = " of Project/Project_Overview_Apr2.pdf"
Ok.. where is your current asp file located?
Let's do a test

Response.Write Server.MapPath(".")
Response.Write "<br>"

From there, you can try to make it point to the directory where your pdf is.

' Not sure if it will point to the directory you want
Response.Write Server.MapPath("/Administration/Administration of Project/Project_Overview_Apr2.pdf")


Thanks.  I was able to get the path.
Thanks for the question and the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial