Forum Moderators: open
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.
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.
'--------------------------------------------------------------------
'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]