Forum Moderators: open

Message Too Old, No Replies

ASP cdo email attachment from user form issue

Been trying for days to get this work...

         

Suraj2031

12:35 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



Hi, I'm trying to create a form in asp using CDOSYS where user can upload small files in an email attachment. The code works absolutely fine and the mail is sent until I add ".AddAttachment attach" statement when there is an error thrown. I even tried storing the file in a temp folder on the server and later delete but in vain. I have been trying for different solutions from past few days. Could someone please get me out of this problem. This is frustrating me. The code is as follows -

Dim iMsg
Set iMsg = Server.CreateObject("CDO.Message")
Dim iBp
Dim Flds
Dim File
Dim FS
Dim attach

Set FS = CreateObject("Scripting.FileSystemObject")
'Get temporary folder
TempFolder = FS.GetSpecialFolder(2) & "\emailtemp"

Dim iConf
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
Flds(cdoSendUsingMethod) = cdoSendUsingPort
Flds(cdoSMTPServer) = "localhost"
Flds(cdoSMTPServerPort) = 25
Flds(cdoSMTPAuthenticate) = cdoAnonymous
Flds.Update

With iMsg
Set .Configuration = iConf
.To = "x@y.com"
.From = email
.Subject = "Form"
.HTMLBody = Body
If Len(File.attach) > 0 Then
attach = TempFolder & "\" & File.attach
File.SaveAs attach
.AddAttachment attach
End If
.Send
End With

' Clean up variables.
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing

'delete temporary files
If Len(File.attach) > 0 Then
attach= TempFolder & "\" & File.attach
FS.DeleteAttachment attach
End If

Response.Write "<br />Email has been sent."

Thanks in advance.

marcel

12:39 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Suraj2031, Welcome to WebmasterWorld
when there is an error thrown

What is the error message?

Suraj2031

12:49 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



Below is the error message that I receive. The email gets sent when I do not include the AddAttachment statement -

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Suraj2031

2:37 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



Hi Marcel,
Thanks a lot for the support. I received a success message when I tried from the link <snip>. I have altered the code a lil bit to suit my server requirements. When I tested I received mails with subject "with attachment" and also "without attachment" but did not find the attachment I had put in. I am not able to figure out where the error might be. Please do the needful. Below is my code -

'--------------------------------------------------------------------
'SendEmail:
'Try to send given email using CDO component.
'Returns empty string in case of success or error message in case of error.
'--------------------------------------------------------------------
Function SendEmail(strEmailTo, strEmailFrom, strEmailSubject, strEmailBody, arrAttachments)
'declare variables
Dim objMessage 'object for sending email
Dim iConf 'object for configuration of email settings
Dim x 'loop iterator
Dim Flds

'define default return value:
SendEmail=""

'create objects:
Set objMessage=Server.CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

'define settings:

Set Flds = iConf.Fields
Flds(cdoSendUsingMethod) = cdoSendUsingPort
Flds(cdoSMTPServer) = "localhost"
Flds(cdoSMTPServerPort) = 25
Flds(cdoSMTPAuthenticate) = cdoAnonymous
Flds.Update

'apply settings:
Set objMessage.Configuration=iConf

'build message details:
objMessage.To=strEmailTo
objMessage.From=strEmailFrom
objMessage.Subject=strEmailSubject
objMessage.HtmlBody=strEmailBody

If IsArray(Attach) Then
For x=0 To UBound(Attach)
objMessage.AddAttachment Attach(x)
Next
End If

'catch errors and try to send:
On Error Resume Next
objMessage.Send
If Err.Number<>0 Then
SendEmail=Err.Description
Err.Clear
End If
On Error Goto 0

'done, clear objects from the memory:
Set objMessage=Nothing
Set iConf=Nothing
End Function

'usage:
Dim strError
strError = SendEmail("suraj.l.shetty@gmail.com", email, "testing without attachments", Body, "")
If Len(strError)=0 Then
Response.Write("success!<br />")
Else
Response.Write("failure: " & strError & "<br />")
End If
strError = SendEmail("suraj.l.shetty@gmail.com", email, "testing with attachments", Body, Array(Server.MapPath("file1.txt"), Server.MapPath("file2.asp")))
If Len(strError)=0 Then
Response.Write("success!")
Else
Response.Write("failure: " & strError & "<br />")
End If

[edited by: marcel at 3:46 pm (utc) on Nov. 20, 2009]
[edit reason] removed broken link [/edit]