简体   繁体   English

在ASP中发送邮件代码

[英]Sending Mail code in ASP

I have given one HTML file. 我给了一个HTML文件。 When someone fills the form & sends it, it should send email to their mail id. 当某人填写并发送表格时,它应该将电子邮件发送到其邮件ID。 how to write code for this in ASP? 如何在ASP中为此编写代码?

Your Name:* Email:* Phone No: Your Message:* 您的姓名:* 电子邮件:* 电话号码: 您的消息:*
  • Gnaniyar Zubair 纳尼亚·祖拜尔

Use CDOSYS like this:- 像这样使用CDOSYS:-

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

oMsg.From = "Me <me@mymail.myserver.com>"
oMsg.To = "Bloke <bloke@somewere.com>"
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. 您还需要在IIS应用程序上配置邮件设置,以为CDO.Message对象提供默认配置。 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:- 然后在调用send之前将此行添加到代码主体中:

Set oMsg.Configuration = GetConfiguration()

Just tweak the the GetConfiguration content to use your SMTP servers host name. 只需调整GetConfiguration内容即可使用您的SMTP服务器主机名。

Note don't use CDONTS its deprecated. 请注意,不要使用CDONTS不推荐使用的方法。

I assume you are using ASP 3.0, you can use Cdonts.dll 我假设您使用的是ASP 3.0,则可以使用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 = "fernan@tudominio.com" 
oCDO.To = "foc@tudominio.com" 
oCDO.Subject = "Subject" 
oCDO.Body = cBody 
'oCDO.Cc = "resal@tudominio.com;webmaster@tudominio.com" 
oCDO.Bcc = "quinqui@tudominio.com" 
'oCDO.MailFormat = 0 

oCDO.Send 

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

%> 
</body> 
</html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM