Can't attach attachments in email using ASP CDO

T

tabonni

Hi

I try to grab the checked files from HTML page and then send those PDF
files as attachments. It can just send email, there are no PDF files
attached. Can anybody point out my error?

My idea is:
When people check the check boxes in HTML page for the PDF files, it
will transfer the files' name to ASP page. Then, it will attach it in
the email.

ASP file - myMail.asp
---------------------

<%@ Language=VBScript %>
<!--METADATA TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows
Library" -->
<!--METADATA TYPE="typelib"
UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library"
-->

<%
SUB sendmail( toWho, Subject, Body )
Dim objCDO
Dim iConf
Dim Flds

Const cdoSendUsingPort = 2

Set objCDO = Server.CreateObject("CDO.Message")
Set iConf = Server.CreateObject("CDO.Configuration")

Set Flds = iConf.Fields
With Flds
..Item(cdoSendUsingMethod) = cdoSendUsingPort
..Item(cdoSMTPServer) = "newserver"
..Item(cdoSMTPServerPort) = 25
..Item(cdoSMTPconnectiontimeout) = 10
..Update
End With

Set objCDO.Configuration = iConf

objCDO.From = "(e-mail address removed)"
objCDO.To = toWho
objCDO.Subject = Subject

courseOutline = Request.Form( "Bus" )
courseOutline = Split( courseOutline, "," )

For Each member in courseOutline
filepath = Server.Path("/") & member
objCDO.AddAttachment filepath
Next

objCDO.TextBody = Body
objCDO.Send
END SUB

toWho = TRIM( Request.Form("To") )
Subject = TRIM( Request.Form("Subject") )
Body = TRIM( Request.Form("Body") )
IF toWho <> "" THEN
sendMail toWho, Subject, Body

'Cleanup
Set ObjCDO = Nothing
Set iConf = Nothing
Set Flds = Nothing
Response.Write "Email Sent"
END IF
%>

<html>
<body>
<form action="mymail.asp" method="post" ID=Form1>
<table cellspacing=0 cellpadding=2>
<tr>
<td> From: (e-mail address removed) </td>
</tr>
<tr>
<td> To: <input type="text" name="To" size="20" ID=Text1></td>
</tr>
<tr>
<td> Subject: <input type="text" name="Subject" size="20"
ID=Text2></td>
</tr>
<tr>
<td> Attachments: </td>
<td>
<%
courseOutline = Request.Form( "Bus" )
courseOutline = Split( courseOutline, "," )
For Each member in courseOutline
response.write member & "<br>"
Next
%>
</td>
</tr>
<tr>
<td>Message: </td>
</tr>
<tr>
<td><textarea rows="10" name="Body" cols="40"
ID=Textarea1></textarea></td>
</tr>
</tr>
<tr>
<td>
<%
msm = Request.Form("Test")
response.write msm
%>
</td>
</tr>
</table>
</form>
</body>
</html>


HTML file - mailform.htm
---------
<html>
<head>
<title>Request for More info</title>
</head>
<body>
<form action="mymail.asp" method="post">
<p>
MS Outlook PDF Document
<input type="checkbox" value="outlook.pdf" name="Bus" />
</p>
<p>
MS Word PDF Document
<input type="checkbox" value="word.pdf" name="Bus" />
</p>
<p>
MS Excel PDF Document
<input type="checkbox" value="excel.pdf" name="Bus" />
</p>
<p>
<input type="submit" value="Submit" name="Submit" />
</p>
</form>
</body>
</html>
 
R

Ray Costanzo [MVP]

Have you confirmed that your code thinks that some file should be attached?


<%
'...
courseOutline = Request.Form( "Bus" )
courseOutline = Split( courseOutline, "," )

RESPONSE.WRITE UBOUND(courseOutline)
RESPONSE.END

For Each member in courseOutline
filepath = Server.Path("/") & member
objCDO.AddAttachment filepath
Next
'...
%>

Ray at work
 
T

tabonni

I tested the courseOutline array using For Each..Next Loop
All items I selected in the HTML page are in the courseOutline Array.
I don't know why I still can't attach those files as attachments in
email. There are no error pop out either. Any idea?

Cheers
 
R

Ray Costanzo [MVP]

Well, now is when you debug, by creating a test asp page that just tries to
attach one small text file to a mail object and is as simple as possible.
Try to narrow down where the problem may be until you find it.

Ray at work
 
T

tabonni

Hello Ray

I used Response.Write to print out every single elements inside
outlines array. All the elements are what I selected in HTML Page. I'm
even printed out the filepath after using Server.MapPath(). They are
correct either. I really don't where are the bugs.

But, the strange thing is if I'm hard coding the courseOutlines array
elements. Like:

Dim outlines(3)
outlines(0) = "outlook.txt"
outlines(1) = "word.txt"
outlines(2) = "excel.txt"

For i=0 to UBound(outlines)
filepath = Server.MapPath(".") & TRIM("\") & TRIM(outlines(i))
objCDO.AddAttachment filepath
Next

It works. It can attach all documents in the email.

But, when I change it to:

outlines = Request.Form("Bus")
Outlines = Split( Outlines, "," )

For i=0 to UBound(outlines)
filepath = Server.MapPath(".") & TRIM("\") & TRIM(outlines(i))
objCDO.AddAttachment filepath
Next

It doesn't work.

I printed out the filepath and elements in outlines. They are fine as
well.
Please Help. I'm feeling hopeless.

Cheers
Bon
 
R

Ray Costanzo [MVP]

Okay, the problem is that you are doing this in a two step process, and
you're not carrying through the values from the first page in which the user
checks the boxes for the files. When the user does that and submits the
form, he must then submit a second form to get the e-mail sending initiated.
At that second submission, your Request.Form("bus") is no longer valid, as
it's gone. You have to carry that through.

Check out your form in action here.
http://www.lane34.com/tabonni/mailform.htm Note the "View Code" link on the
"mymail.asp" page.

In your final version of this thing you're working on, you may want to
response.redirect your users after the mail is sent. Like,
response.redirect them to a "thankyou.htm" page or something. Otherwise,
they can just sit there and refresh/resubmit repeatedly.

Ray at home
 
T

tabonni

Hello Ray

Thank you very very very much. I had that problems for 2 weeks. I
didn't know where were the bugs.

May I ask you how did you find the bugs? What methods did you use?
Because I did print out all elements in the outlines array and checked
the filepaths. All are correct. I had no idea what's wrong. So, how
did you do that?

Thank you anyway. You save my life. :D
 
R

Ray Costanzo [MVP]

It wasn't until I copied and pasted the code that you posted in your
original message and tried it that I saw something wrong. I first through
in the RESPONSE.WRITE filepath & "<br>" lines. When I did that, I saw that
the file paths were valid, but then when I submitted the second form and
didn't see them, I realized that there was no array of file paths. So,
where was that array supposed to come from? I saw that it was supposed to
come from Request.Form("BUS"). So, I did a view-source and searched for a
form element named BUS, and did not see one. I guess that's how I could
have discovered the issue anyway!

Ray at work
 
T

tabonni

Anyway, I have to thanks you again.

May I ask you one more question? I don't know why the program generate
a tmp file everytime I submit the email form. For example, After I
selected all the documents I required and send an email, a tmp file
will be generated and be placed in the same directory of the
myMail.asp and the directories I put all the PDF files. I opened the
tmp file. There is all myMail.asp coding. How come?

Cheers
Bon
 
R

Ray Costanzo [MVP]

That is odd. I've never heard of that happening before. What's the name of
the file?

Ray at hom
 
T

tabonni

Hello Ray

Those files' name are ve-6F47 (the myMail.asp) and ve-6F4A
(mailForm.htm). But, every time I send an email, they will be
generated and the name will change as well. Like, ve-6F48, ve-6F49...
etc ve6F4A, ve6F4B... etc. Inside those TMP files are the coding of
myMail.asp and mailForm.htm.

Cheers
Bon
 
R

Ray Costanzo [MVP]

That is interesting. I honestly have no idea what could be on your server
that is generating .tmp files of your ASP pages. ??

Ray at work
 
T

tabonni

It's ok. Thank you for your help

By the way, how often you check the google newsgroup? If I have
problem later on, may I ask you again?

Cheers
Bon
 
R

Ray Costanzo [MVP]

Just post your questions to the group. Somebody'll probably answer them.

Ray at work
 
T

tabonni

If I want to make my ASP CDO email application to have Reply & Forward
functions, how can I do that? Do I need to use Outlook.Application
object? If so, do you know any website about it?

Cheers
Bon
 
R

Ray Costanzo [MVP]

You're new to newsgroups, aren't you. :] When you have a new topic, please
start a new thread. If you just keep using the same thread, the number of
eyes that read your question will be pretty low, as this just appears to be
part of a thread that is already in progress!

One thing that would make things a bit easier is if you started using a news
client to post to the newsgroups instead of Google's web-based interface.
If interested, see here for setup instructions of common usenet
applications. http://www.aspfaq.com/5007 http://www.aspfaq.com/2081 is
another good article.

Ray at home
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top