| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
||||
|
||||
|
Fully working sub routine to send given email body to given email address:
Code:
<%
Const SMTP_SERVER="smtpserver.domain.com"
'--------------------------------------------------------------------
'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 objConf 'object for configuration of email settings
Dim x 'loop iterator
'define default return value:
SendEmail=""
'create objects:
Set objMessage=Server.CreateObject("CDO.Message")
Set objConf=Server.CreateObject("CDO.Configuration")
'define settings:
If Len(SMTP_SERVER)=0 Then
objConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=1
Else
objConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2 'cdoSendUsingPort
End If
objConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory")=Server.MapPath("/")
If Len(SMTP_SERVER)>0 Then
objConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")=SMTP_SERVER
End If
objConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
objConf.Fields.Update
'apply settings:
Set objMessage.Configuration=objConf
'build message details:
objMessage.To=strEmailTo
objMessage.From=strEmailFrom
objMessage.Subject=strEmailSubject
objMessage.HtmlBody=strEmailBody
If IsArray(arrAttachments) Then
For x=0 To UBound(arrAttachments)
objMessage.AddAttachment arrAttachments(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 objConf=Nothing
End Function
'usage:
Dim strError
strError = SendEmail("someone@somewhere.com", "test <test@test.com>", "testing without attachments", "hello world", "")
If Len(strError)=0 Then
Response.Write("success!<br />")
Else
Response.Write("failure: " & strError & "<br />")
End If
strError = SendEmail("someone@somewhere.com", "test <test@test.com>", "testing with attachments", "hello world", 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
%>
-attached is the same code as text file. Last edited by Shadow Wizard : April 8th, 2008 at 06:30 AM. |
|
#2
|
|||
|
|||
|
Quote:
How bout adding attachment to it? How to edit the script to include attachment in the email as well ? |
|
#3
|
||||
|
||||
|
good point, the code has been changed to support attachments now. also, added
usage samples. |
|
#4
|
|||
|
|||
|
failure: The transport failed to connect to the server.
CDO.Message.1 error '80070002' The system cannot find the file specified. /ssp07/butt/email.asp, line 133 iam geting that errer message |
|
#5
|
||||
|
||||
|
what is line 133?
|
|
#6
|
|||
|
|||
|
Code:
If IsArray(arrAttachments) Then For x=0 To UBound(arrAttachments) objMessage.AddAttachment arrAttachments(x) Next End If the bold line is 133... All i did was copy the hole code and put it in my asp page..do i need to make any changed to it? |
|
#7
|
||||
|
||||
|
the files you tried to attach don't exist on your server.
for example if you have this exact code: Code:
strError = SendEmail("someone@somewhere.com", "test <test@test.com>", "testing with attachments", "hello world", Array(Server.MapPath("file1.txt"), Server.MapPath("file2.asp")))
then you should have file named "file1.txt" and file named "file2.txt" on your server. this was given as example only, sorry if it's not clear enough in the original post. |
|
#8
|
|||
|
|||
|
ok quck quston..
do i need a page b4 this page which the user enters there email address in and text area where they put there email in and when they hit send this asp page does the rest? |
|
#9
|
||||
|
||||
|
yes usually web programmers use forms to let visitor type something then
they use this in their code. |
|
#10
|
|||
|
|||
|
hi, i used your code but without the attachment code, but i were wondering what should i put here
""test <test@test.com>" |
|
#11
|
||||
|
||||
|
Quote:
![]() its the email address of the sender of mail with the name(if u want) |
|
#12
|
|||
|
|||
|
thanks, but it now it giving me the following error:
failure: The server rejected one or more recipient addresses. The server response was: 550 5.7.1 Unable to relay for "my email address" |