Friday, September 22, 2006

How to Send Email in ASP from Windows Server 2003

Sending an email from a web page in ASP on a Windows 2003 server is easily accomplished. Be aware, however, that this method won't work on Windows NT/2000. In Windows Server 2003, mail is sent using CDOSYS. Windows NT/2000 uses CDONTS, but I'll talk about that in another article.

Here's a simple Windows 2003 example that sends a text email. In this example, I constructed it as though I were grabbing information from a form submission:
Set myMail=CreateObject("CDO.Message")
myMail.Subject = "Sending email with CDO"
myMail.From = "mymail@mydomain.com"
myMail.To = "someone@somedomain.com"
myMail.Bcc = "someoneelse@somedomain.com"
myMail.Cc = "someoneelse2@somedomain.com"
myMail.TextBody = "This is a message."
myMail.Send
set myMail=nothing
You can also send HTML formatted emails:
Set myMail=CreateObject("CDO.Message")
myMail.Subject = "Sending email with CDO"
myMail.From = "mymail@mydomain.com"
myMail.To = "someone@somedomain.com"
myMail.HTMLBody = "<h1>This is a message.</h1>"
myMail.Send
set myMail = nothing
You can send both a text version and an HTML versions
Set myMail=CreateObject("CDO.Message")
myMail.Subject ="Sending email with CDO"
myMail.From ="mymail@mydomain.com"
myMail.To ="someone@somedomain.com"
myMail.HTMLBody = "<h1>This is a message.</h1>"
myMail.TextBody ="This is a message."
myMail.Send
set myMail=nothing
You can include CCs and BCCs:
Set myMail=CreateObject("CDO.Message")
myMail.Subject = "Sending email with CDO"
myMail.From = "mymail@mydomain.com"
myMail.To = "someone@somedomain.com"
myMail.Bcc = "someoneelse@somedomain.com"
myMail.Cc = "someoneelse2@somedomain.com"
myMail.TextBody = "This is a message."
myMail.HTMLBody = "<h1>This is a message.</h1>"
myMail.Send
set myMail=nothing
You can easily mail a web page:
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.CreateMHTMLBody "http://www.w3schools.com/asp/"
myMail.Send
set myMail=nothing
You can send an HTML file:
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.CreateMHTMLBody "file://c:/inetpub/wwwroot/mywebsite/test.htm"
myMail.Send
set myMail=nothing
And you can send a mail with an attachment
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.AddAttachment "c:\inetpub\wwwroot\mywebsite\test.txt"
myMail.Send
set myMail=nothing
It's not to difficult to have a form that passes the values to your mail object.

Tip:
Use a properly formatted return address. Many spam filters these days will automatically block emails that don't have a return address. Some block emails where the return address isn't a real address as well.

1 comment:

Anonymous said...

hi. you said in the post: "How to Send Email in ASP from Windows Server 2003" that "It's not to difficult to have a form that passes the values to your mail object."
I have been trying to figure out how to pass the information from a form to one of these asp emails. could you please enlighten me?

iaincohn@comcast.net

thank you