tags:

views:

161

answers:

2

Hi all,

I have given one HTML file. When someone fills the form & sends it, it should send email to their mail id. how to write code for this in ASP?

Your Name:* Email:* Phone No: Your Message:*
  • Gnaniyar Zubair
+1  A: 

I assume you are using ASP 3.0, you can use Cdonts.dll

The code:

cBody = Request.Form("Body") 
cPara = = Request.Form("to") 

<%@ Language=VBScript%> 
<html> 

<head> 
   <title>Email Sending</title> 

</head> 
<body>
<% 
Dim cBody, n 

For Each n In Request.Form 
    cBody = cBody & n & ": " & Request.Form(n) & chr(13) 
Next 

Set oCDO = Server.CreateObject("CDONTS.NewMail") 

oCDO.From = "[email protected]" 
oCDO.To = "[email protected]" 
oCDO.Subject = "Subject" 
oCDO.Body = cBody 
'oCDO.Cc = "[email protected];[email protected]" 
oCDO.Bcc = "[email protected]" 
'oCDO.MailFormat = 0 

oCDO.Send 

Set oCDO = Nothing 
Response.Write "¡Email Sent!!" 

%> 
</body> 
</html>
MRFerocius
hi MRFerocius,Thanks for your immediate response.when user click the submit button, the message should sent to admin "[email protected]" not user's mail id..how to write for that? pls cdlarify
Gnaniyar Zubair
I think I dont understand the user's id thing. Can you explain a little more? Its a table with key/email address? If that is the case will be simple.
MRFerocius
no not like that. I meant, user is client who is filling the form. It is like a feedback form. when anybody filling it, then message should sent to some static email id like "[email protected]".
Gnaniyar Zubair
+2  A: 

Use CDOSYS like this:-

Dim oMsg : Set oMsg = CreateObject("CDO.Message")

oMsg.From = "Me <[email protected]>"
oMsg.To = "Bloke <[email protected]>"
oMsg.Subject = "Test"
oMsg.HTMLBody = "<html><body>Hello World</body></html>"

oMsg.Send

Of course you need to get the To field from some persistent store where you store the users profile and supply the Subject and protions of the body from the posted fields.

You also need to configure the mail settings on the IIS application to supply a default configuration for the CDO.Message object. Failing that you need to configure the mail settings yourself using a function like this:-

Function GetConfiguration()
 Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
 Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
 Const cdoSMTPServerPickupDirectory = "http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory"
 Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"

 Const cdoSendUsingPickup = 1
 Const cdoSendUsingPort = 2

 Dim GetConfiguration : SetGetConfiguration = CreateObject("CDO.Configuration")

 With GetConfiguration.Fields
  .Item(cdoSendUsingMethod) = cdoSendUsingPort
  .Item(cdoSMTPServer) = "mysmtp.myserver.com"
  .Item(cdoSMTPServerPort) = 25
  .Update
 End With

End Function

Then add this line to the main body of code before calling send:-

Set oMsg.Configuration = GetConfiguration()

Just tweak the the GetConfiguration content to use your SMTP servers host name.

Note don't use CDONTS its deprecated.

AnthonyWJones
That is correct, in Windows 2003 and 2008 CDONTS.dll is no longer available. The CDO.sys approach will work for Windows 2000 and up.
jwmiller5