CDO Question

JeffPell

n00b
Joined
Aug 24, 2005
Messages
49
I am tyring to setup a form that will create an email. The email is being sent find but the email subject and email body is not being placed into the email.

Here is my current code:

<%
sch = "http://schemas.microsoft.com/cdo/configuration/"

Set cdoConfig = CreateObject("CDO.Configuration")

With cdoConfig.Fields
.Item(sch & "sendusing") = 2 'CDO SEND USING PORT NUMBER
.Item(sch & "smtpserver") = "smtp.server.net"
.update
End With

Set cdoMessage = CreateObject("CDO.Message")

With cdoMessage
Set .Configuration = cdoConfig
.From = "[email protected]"
.To = "[email protected]"
.Subject = Request.Form('subject")
.TextBody = Request.Form("body")
.AddAttachment "c:\access\resume.txt"
.Send
End With

Set cdoMessage = Nothing
Set cdoConfig = Nothing
%>

Thanks in advance for any help.
 
Ok, I figured out the problem with the body and subject lines not being generated in the email. I had an improper enctype set on my form.

I am however having difficulties attaching files through my file field on the form.

Here is my code again. I would appreciate if someone could tell me what I'm doing wrong. :) Also, is there a way to add different parts of my form on seperate lines of my email body? Maybe with some sort of .TextBody = And ?

<%
sch = "http://schemas.microsoft.com/cdo/configuration/"

Set cdoConfig = CreateObject("CDO.Configuration")

With cdoConfig.Fields
.Item(sch & "sendusing") = 2 'CDO SEND USING PORT NUMBER
.Item(sch & "smtpserver") = "smtp.server.net"
.update
End With

Set cdoMessage = CreateObject("CDO.Message")

With cdoMessage
Set .Configuration = cdoConfig
.From = Request.Form("email")
.To = "[email protected]"
.Subject = Request.Form("subject")
.TextBody = Request.Form("body")
.AddAttachment Request.Form("resume")
.Send
End With

Set cdoMessage = Nothing
Set cdoConfig = Nothing
%>
 
Here is my code again. I would appreciate if someone could tell me what I'm doing wrong. Also, is there a way to add different parts of my form on seperate lines of my email body? Maybe with some sort of .TextBody = And ?

How about switching the email to HTML and appending a "<br>" ?? Or vbCrLf
 
I guess I can use vbcrlf. I still need to know how to request.form a file attachment though. Anyone out there know how to do this? .addattachment isnt working for me.

Thanks
 
JeffPell said:
I guess I can use vbcrlf. I still need to know how to request.form a file attachment though. Anyone out there know how to do this? .addattachment isnt working for me.

Thanks

this may sound silly, but if that code was copied verbatum, then maybe you're missing an equal?
Code:
.AddAttachment = Request.Form("resume")

Edit: nevermind my above suggestion.

it looks like you need to first use an upload script that copies the attachment to a folder, then use .AddAttachment "c:\uploads\filename.ext" (assuming the file uload script returns the name of the file that was uploaded)
 
Code:
.AddAttachment(path_of_file)

You can't use the request object.
Use the FileSystemObject in order to write the file to a directory, then recall that file name using the code above.
 
Oh yeah, you'll probably want to delete the file after you've sent the message.
 
Ok, with that said I added FileSystemObject to my code.

Is this code correct for saving the file to a folder in the same directory as the asp page? And how do I call the file name for my .addattachment?

<%
Dim objMail, objMailConf
Dim fs, fname
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fname = Request.Form("resume")
Set fname = fs.CopyFile("files/",true)
Set objMail = Server.CreateObject("CDO.Message")
Set objMailConf = Server.CreateObject("CDO.Configuration")

objMailConf.Fields.item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMailConf.Fields.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server.net"
objMailConf.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMailConf.Fields.item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMailConf.Fields.Update

Set objMail.Configuration = objMailConf
objMail.From = Request.Form("emailAddress")
objMail.To = "[email protected]"

objMail.Subject = Request.Form("emailSubject")
objMail.TextBody = Request.Form("emailText") & vbcrlf & "Home Phone Number: (" & Request.Form("homeArea") & ")" & Request.Form("homeNumber") & vbcrlf & "Cell Phone Number: (" & Request.Form("cellArea") & ")" & Request.Form("cellNumber")
objMail.AddAttachment
objMail.Fields.Update
objMail.Send

Set objMail = Nothing
Set obMailConf = Nothing
Set fname = Nothing
Set fs = Nothing
%>
 
First, check for the file, and follow appropriate logic thereafter - then you write to the file

Code:
dim attachfile ''''the path of the attachment file
attachfile = "C:\MyFiles\mystuff\myfile.ext"

IF NOT fs.FileExists(attachfile)
fs.CreateTextFile(attachfile)
'''write to file logic
ELSE
fs.DeleteFile(attachfile)
fs.CreateTextFile(attachfile)
''writetofilelogic
END IF

then use:
Code:
objMail.AddAttachment(attachfile)
 
Back
Top