Referans: http://www.webcheatsheet.com/asp/sending_email_asp.php
Sending a text email
Sending email with CDOSYS is a simple task. First you create a reference to the CDO component. Then fill-in From, Subject and To fields of the headers and the body text. Also you can add Bcc and Cc headers. Then you use the Send method to send the email. That’s all.
<%
Set Mail=CreateObject("CDO.Message")
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.Bcc="someone1@domain.com"
Mail.Cc=someone2@domain.com
Mail.ReplyTo ="replyto@domain.com"
Mail.TextBody="This is an email message."
Mail.Send
set Mail =nothing
%>
Sending an HTML email
The process is quite similar to sending the regular text message.
<%
Set Mail=CreateObject("CDO.Message")
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To=to@domain.com
Mail.ReplyTo ="replyto@domain.com"
Mail.HTMLBody = "<h1>This is an email message.</h1>"
Mail.Send
set Mail=nothing
%>
Sending an email with an Attachment
Please note, when using the .AddAttachment method in your scripts you must use a fully qualified pathname as the argument to the method. Using just a file name or a relative path will produce the error The specified protocol is unknown. By repeating the .AddAttachment method you can attach more than one file.
<%
Set Mail=CreateObject("CDO.Message")
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.TextBody="This is an email message."
Mail.AddAttachment "c:\mydocuments\test.txt"
Mail.Send
set Mail=nothing
%>
Sending an email using a remote server
Sometimes you need to send an email using another server. To accomplish that you need to set the configuration properties to the server.
<%
Set Mail = CreateObject("CDO.Message")
'This section provides the configuration information for the remote SMTP server.
'Send the message using the network (SMTP over the network).
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.yourdomain.com"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (True or False)
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
'If your server requires outgoing authentication, uncomment the lines below and use a valid email address and password.
'Basic (clear-text) authentication
'Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'Your UserID on the SMTP server
'Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="mail@yourdomain.com"
'Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="yourpassword"
Mail.Configuration.Fields.Update
'End of remote SMTP server configuration section
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.TextBody="This is an email message."
Mail.Send
Set Mail = Nothing
%>
Sending an email via Gmail
<%
Set Mail = CreateObject("CDO.Message")
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.gmail.com"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="username@gmail.com"
Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="yourpassword"
Mail.Configuration.Fields.Update
Mail.Subject="Email subject"
Mail.From="from@domain.com"
Mail.To="to@domain.com"
Mail.TextBody="This is an email message."
Mail.Send
Set Mail = Nothing
%>
Load Recipients from a Database
This task may be accomplished in several ways. This is one of many methods.
For example, our database is an MS Access database stored on the local disk. We are interested in Customers table and Name and Email fields.
Set dbConnection = CreateObject("ADODB.Connection")
dbConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=c:\Databases\Customers.mdb"
SQLQuery = "SELECT Name, Email FROM Customers"
Set Result = dbConnection.Execute(SQLQuery)
if Not Result.EOF then
Do While Not Result.EOF
SendMail Result("Name"), Result("Email")
Result.MoveNext
Loop
end if
dbConnection.Close
As you can see the code is simple. We create a database connection object then open the database and query it for the Name and Email fields of each customer. Those values are passed for each customer to the procedure that sends an email to the customer. Note that the recipient's address is formatted as "Name" <email@domain.com> for a more professional look to the recipient.
Sub SendMail(name, address)
Dim Message, SendTo
SendTo = Chr(34) & Name & Chr(34) & "<" & Address & ">"
Set Mail = CreateObject("CDO.Mail")
Mail.Subject = "Email subject"
Mail.From = "from@domain.com"
Mail.To = SendTo
Mail.TextBody="This is an email message."
Mail.Send
Set Mail = Nothing
End Sub
Set the priority/importance of an email and request a read/return receipt
<%
Set Mail = CreateObject("CDO.Message")
' Set the priority/importance of an email
' For Outlook:
Mail.Fields.Item(cdoImportance) = cdoHigh
Mail.Fields.Item(cdoPriority) = cdoPriorityUrgent
' For Outlook Express:
Mail.Fields.Item("urn:schemas:mailheader:X-Priority") = 1
' Request a read/return receipt
' Read receipt
Mail.Fields("urn:schemas:mailheader:disposition-notification-to") = "admin@domain.com"
' Return receipt
Mail.Fields("urn:schemas:mailheader:return-receipt-to") = "admin@domain.com"
Mail.DSNOptions = 14
Mail.Fields.update
Mail.Subject = "Email subject"
Mail.From = "from@domain.com"
Mail.To = "to@domain.com"
Mail.TextBody="This is an email message."
Mail.Send
Set Mail = Nothing
%>