Solved

send email notification in ASP

Posted on 2003-11-18
31
985 Views
Last Modified: 2009-07-29
hi, experts:

i haven't written any codes in ASP to send emails, but i'll need that soon.  Does ASP have built-in function to send email?  If so can someone give me a very simple but complete script to send an email with subject = "Notification" and message body with the following message:

Hello world!

From Mars

just use any dummy email address will be fine.  Thanks!!!
0
Comment
Question by:kingsfan76
  • 14
  • 7
  • 4
  • +3
31 Comments
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 500 total points
Comment Utility
If you are using windows NT, you can use CDONTS, if windows 2000, CDO. In either case, third-party components including ASPEmail (free at www.Persits.com) and JMail are very good. Is this your server or are you using a hosting service?


Here are a few samples:
****************
CDO Complex
******************

<%
Const cdoSendUsingMethod        = _
      "http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort          = 2
Const cdoSMTPServer             = _
      "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort         = _
      "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout  = _
      "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate       = _
      "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic                  = 1
Const cdoSendUserName           = _
      "http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword           = _
      "http://schemas.microsoft.com/cdo/configuration/sendpassword"

Dim objConfig  ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields     ' As ADODB.Fields

' Get a handle on the config object and it's fields
Set objConfig = Server.CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields

' Set config fields we care about
With Fields
      .Item(cdoSendUsingMethod)       = cdoSendUsingPort
      .Item(cdoSMTPServer)            = "smtp_server_name"
      .Item(cdoSMTPServerPort)        = 25
      .Item(cdoSMTPConnectionTimeout) = 10
      .Item(cdoSMTPAuthenticate)      = cdoBasic
      .Item(cdoSendUserName)          = "username"
      .Item(cdoSendPassword)          = "password"

      .Update
End With

Set objMessage = Server.CreateObject("CDO.Message")

Set objMessage.Configuration = objConfig

With objMessage
      .To       = "Display Name <email_address>"
      .From     = "Display Name <email_address>"
      .Subject  = "SMTP Relay Test"
      .TextBody = "SMTP Relay Test Sent @ " & Now()
      .Send
End With

Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
%>




****************
CDO Simple
******************

<%
Dim objMail
Set objMail = Server.CreateObject("CDO.Message")
With objMail
      ' Set message attributes
      .To       =  "Gerald Shoates <gerald.shoates@thismail.com>"
      .From     = "Gerald Shoates <gerald.shoates@thismail.com>"
      .Subject  = "CDO Sample"
      .TextBody = "This is a test email using CDO"

      ' Attachment using known static physical path
      '.AddAttachment "c:\somepath\somefile.txt"
      
      .Send
End With

Set objMail = Nothing
%>


****************
CDONTS
****************
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>Message Request</TITLE>
</HEAD>


<body>

<% Language = VBScript%>

<%
strFirstName = request.form("FirstName")
strEmailAddress = request.form("EmailAddress")

Response.Write("Thank you for your request, " & strFirstName & ".")
Response.Write("<p>Your message has been sent.")


dim objMail
set objMail = CreateObject("CDONTS.Newmail")
    objMail.From = You@YourAddress.com
    objMail.To = strEmailAddress  
    objMail.Subject = "Email Request"
    Msg = "This is the first line of your message" & Chr(10)
    Msg = Msg & "This is the second line of your message... and so forth"
    objMail.Body = Msg
    objMail.Send
    Set objMail = Nothing
%>


</BODY>
</HTML>

********************
JMail
********************

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<%
Set JMail = Server.CreateObject("JMail.SMTPMail")

' This is my local SMTP server
JMail.ServerAddress = "mail.yourdomain.com:25"

' This is me....
JMail.Sender = "myemail@mydomain.net"
JMail.Subject = "Here you go..."
    ' Get the recipients mailbox from a form (note the lack of a equal sign).
    JMail.AddRecipient strEmailAddress

    ' The body property is both read and write.
    ' If you want to append text to the body you can
    ' use JMail.Body = JMail.Body & "Hello world!"
    ' or you can use JMail.AppendText "Hello World!"
    ' which in many cases is easier to use.
    JMail.Body = "Here you go. Your request has been approved" &_
    "and the program is attached to this message"


    ' 1 - highest priority (Urgent)
    ' 3 - normal
    ' 5 - lowest

    JMail.Priority = 1

    JMail.AddHeader "Originating-IP", Request.ServerVariables("REMOTE_ADDR")

    ' Must make sure that IUSR_???? has access to the following files.
    JMail.AppendBodyFromFile "e:\mail\standard_footer.txt"
    JMail.AddAttachment "e:\products\MyProduct.exe"

    ' Send it...
    JMail.Execute
    JMail.ClearRecipients();
set JMail=Nothing
%>

<P>&nbsp;</P>

</BODY>
</HTML>

****************
ASPEmail
****************


<%
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "smtp.smtp-server.com" ' Specify a valid SMTP server
Mail.From = "sales@veryhotcakes.com" ' Specify sender's address
Mail.FromName = "VeryHotCakes Sales" ' Specify sender's name

Mail.AddAddress "andy@andrewscompany.net", "Andrew Johnson, Jr."
Mail.AddAddress "paul@paulscompany.com" ' Name is optional
Mail.AddReplyTo "info@veryhotcakes.com"
Mail.AddAttachment "c:\images\cakes.gif"

Mail.Subject = "Thanks for ordering our hot cakes!"
Mail.Body = "Dear Sir:" & Chr(13) & Chr(10) & _
    "Thank you for your business."

On Error Resume Next
Mail.Send
If Err <> 0 Then
  Response.Write "Error encountered: " & Err.Description
End If
%>


Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
As you can tell by the varying styles, I did not write all of these, but the question gets asked enough that I thought I would cobble a few examples together.

FtB
0
 
LVL 3

Expert Comment

by:barryfandango
Comment Utility
this code will do what you ask.  It uses a windows 2000 (and later) package called CDOSYS.  If you're using pre-windows 2000 you have to use a slightly different package called CDONTS.




<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows 2000 Library" -->
<!--METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" NAME="ADODB Type Library" -->
<%
Dim iMsg
Set iMsg = CreateObject("CDO.Message")
Dim iBp
Dim Flds

Dim iConf
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
Flds(cdoSendUsingMethod)  = cdoSendUsingPort
Flds(cdoSMTPServer)       = "smtp.MyMailServer.com"
Flds(cdoSMTPServerPort)   = 25
Flds(cdoSMTPAuthenticate)     = cdoAnonymous
Flds.Update

With iMsg
  Set .Configuration = iConf
    .To          = "me@mydomain.com"
    .From        = "me@mydomain.com"
    .Subject     = "Notification"
    .TextBody    = "Hello world!" & vbCrLf & vbCrLf & "From Mars"

    .Send
End With
%>
0
 
LVL 3

Expert Comment

by:barryfandango
Comment Utility
damn... took too long... : )
0
 
LVL 3

Expert Comment

by:barryfandango
Comment Utility
although, kingsfan, you should find the metadata lines in my example more convenient than the constant declarations at the top of fritz' example.
0
 
LVL 12

Author Comment

by:kingsfan76
Comment Utility
thanks for the quick responses.

@FtB and barryfandango:

i am using Windows 2000 Server with IIS 5.0
which one would you suggest me to use?  and do I need to download or install any components?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
If you have SMTP running on that server, use the CDO.

FtB
0
 
LVL 12

Author Comment

by:kingsfan76
Comment Utility
@FtB:
I tested your code for the *CDO Simple* and it gives me the following error:

CDO.Message.1 error '80070005'

Access is denied.


it occur on the .send line.
I copy your code exactly for that part, and use my own email address.  am i missing something?

anyway i'm going to test the *complex* and the other codes right now
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
You might want to make sure that the CDO.dll is on your machine. Also, are you running SMTP services on that server?

FtB
0
 
LVL 12

Author Comment

by:kingsfan76
Comment Utility
SMTP server is on a different machine.  I guess i'll need to make sure CDO.dll is on the application server and get the mail server name and port no. first.
I'll get back a little later.  meanwhile i'll increase the points as well.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
If you are using a remote smtp server, the ASPeMail available for free at www.persitis.com is really easy to set up and use. I have had great luck with it.

Fritz the Blank
0
 
LVL 9

Expert Comment

by:CCongdon
Comment Utility
Yeah, I found that out the hard way, if you're going to use CDONTS, you have to make sure that IIS's SMTP server is installed.

Here's a link to the MSDN page that explains the methods that can be used in this object:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_denali_newmail_object_cdonts_library_.asp
0
 
LVL 12

Author Comment

by:kingsfan76
Comment Utility
@CCongdon and @FtB:

I tried CDONTS and it gave me the permission denied error.  So if my SMTP server is on a different server from my application server, then I can't use CDO or CDONTS?  (I have no control over the servers)

@FtB:
I'll probably give ASPeMail a shot since it's free.  but the link is not working.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Sorry, wrong address (type)

www.persits.com

0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
CDONTS cannot work with remote SMTP services.

CDO can, but the configuration can be tricky.

AspEmail works like a charm but lacks some of the functionality in terms of catching bad mail and etc.

FtB
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Sorry, it looks like they changed the address--here it is:

http://www.aspemail.com/


FtB
0
 
LVL 15

Expert Comment

by:gladxml
Comment Utility
try to check out the link might help

http://www.4guysfromrolla.com/webtech/faq/Email/faq2.shtml
0
 
LVL 9

Expert Comment

by:CCongdon
Comment Utility
Are you saying that you have no access at all to installing programs on the server? Because in that case, ASP E-Mail still won't work since you have to register the DLL.

Another possiblity, but it's VERY cludgy and out of date is to use a CGI script. There's a couple out there that do e-mail.

Or... you could use Front Page if your server supports Front Page extensions... Form to e-mail is a behavior natively supported by FrontPage. But as far as I know, it's only for packing up a form and e-mailing it.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
@Glad--

Look further up the thread--that was already covered.

@CCongdon--

I am not sure whether kingsfan can use a third-party component. If not, then we will have to futz with CDO.

FtB
0
 
LVL 12

Author Comment

by:kingsfan76
Comment Utility
I can't install anything on the server, but i can ask and it should be no problem.  but having SMTP server on the server i'm currently working on is out of the question.

i think the free version of ASPmail should be enough for me as I only need to send a notification mail with a subject line and a very simple body message.  

thanks for all the replies.  It'll be probably be a day or two for me to test it out and PAQ this question.

diehard Kingsfan   :-)        (that's NBA Sac Kings)
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
I have used ASPeMail in a number of applications that require the use of remote SMTP servers with very good luck. There is only one downside: the free version doesn't queue up all of your mail in the event that the smtp server does not respond. CDO, and the Professional version of ASPeMail will queue up all of those messages and then send them when the service comes back on line.

What I have done is to write a little routine that captures any mail that didn't get sent and stores the pertinent data into a table so that the messages could be resent later. If you are interested, I can show you what I did for that.

FtB
0
 
LVL 12

Author Comment

by:kingsfan76
Comment Utility
@FtB:

thanks for the suggestion.  but with the current project schedule i probably don't have time to implement that.  i need something as simple as possible.  but i'll keep that in mind in the future (maybe a future expert question).  

just talked to system admin and he said if he set up a virtual SMTP server in IIS then i'll be able to use CDO/CDONTS, but problem is when we upload the app to the client's server it's not going to work.  so i guess i'll stick with ASPmail

kingsfan  
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Fair enough!

FtB
0
 
LVL 9

Expert Comment

by:apirnia
Comment Utility
here is a good one: go to www.aspemail.com
Version 4 is free.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
@apirnia:

Please look further up the thread--that has been suggested already.

FtB
0
 
LVL 9

Expert Comment

by:apirnia
Comment Utility
fritz_the_blank

Whats your point here?
0
 
LVL 9

Expert Comment

by:apirnia
Comment Utility
Personaly I think if more than one person suggest the same thing, it means that it must be a working soloution.......and  maybe I should give it a try first.....
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
That it is not particularly constructive to repeat what has already been posted in the thread: the idea of using ASPeMail has already been proposed along with sample code and the URL where the code can be downloaded.

FtB
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Oh, I see what you mean. Ordinarily, then, one would say something like "I agree that using ASPeMail is a good approach" or something like that.

FtB
0
 
LVL 3

Expert Comment

by:barryfandango
Comment Utility
hello unsubscribe button!
0
 
LVL 12

Author Comment

by:kingsfan76
Comment Utility
sorry about the delays on this question.  currently I'm not able to test the ASPMail object in our project server.  I'll try to install it in my own server at home and give it a test, and hopefully i'll be able to close this question soon.

kingsfan
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now