ASP - How to send a simple email in asp?

Asked By kunal mukherjee on 18-Dec-06 01:05 PM

Hi All I am trying to send a simple mail using the following code. Its showing no error but at the to address I am getting no mails. Can you please pont out whats wrong in it.

<%
set Mail = Server.CreateObject("CDONTS.NewMail")
Mail.To = "kunalmukherjii@gmail.com"
Mail.From = "test@test.com"
Mail.Subject = "QUICK QUOTE"
Mail.Body = "JUST A TEST EMAIL"

Mail.Send
set Mail = nothing

response.write "<center><h2>Your query has been sent.</h2><center><br>Click <a href=index.html>here</a> to return to the Home Page."
%>


Thanks in advance,

Kunal

CDOSYS

F Cali replied to kunal mukherjee on 18-Dec-06 01:07 PM

Here's an example from the following link:

http://www.w3schools.com/asp/asp_send_email.asp

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.Send
set myMail=nothing

send email from an Active Server Page using CDONTS

Senthil T. replied to F Cali on 18-Dec-06 01:13 PM
In this link, we will learn how to send email from an Active Server Page using CDONTS (Microsoft Collaboration Data Objects for Windows NT Server).

http://www.devguru.com/features/tutorials/CDONTS/cdonts.html

ASP Sending e-mail with CDOSYS

K Pravin Kumar Reddy replied to kunal mukherjee on 18-Dec-06 01:16 PM

Sending e-mail with CDOSYS

CDO (Collaboration Data Objects) is a Microsoft technology that is designed to simplify the creation of messaging applications.

CDOSYS is a built-in component in ASP. We will show you how to use this component to send e-mail with ASP.

How about CDONTs?

Microsoft has discontinued the use of CDONTs on Windows 2000, Windows XP and Windows 2003. If you have used CDONTs in your ASP applications, you should update the code and use the new CDO technology.

Examples using CDOSYS

Sending a text e-mail:

<%
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.Send
set myMail=nothing
%>

Sending a text e-mail with Bcc and CC fields:

<%
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
%>

Sending an HTML e-mail:

<%
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
%>

Sending an HTML e-mail that sends a webpage from a website:

<%
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
%>

Sending an HTML e-mail that sends a webpage from a file on your computer:

<%
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:/mydocuments/test.htm" 
myMail.Send
set myMail=nothing
%>

Sending a text e-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:\mydocuments\test.txt"
myMail.Send
set myMail=nothing
%>

Sending a text e-mail using a remote server:

<%
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.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
'Name or IP of remote SMTP server
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
="smtp.server.com"
'Server port
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") _
=25 
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing
%>

reference

http://www.w3schools.com/asp/asp_send_email.asp

Response
F Cali replied to Senthil T. on 18-Dec-06 01:17 PM

Also based on that link, if you are using Windows 2000 or Windows XP, you should use CDOSYS.  Please refer below which is also taken from the same link you've provided:

http://www.devguru.com/features/tutorials/CDONTS/cdonts.html

"If you are using CDOSYS for Windows 2000 or Windows XP, the code will be be slightly different, as shown below:

<%
Dim MyMail
Set MyMail = Server.CreateObject("CDO.Message")
MyMail.From = "justme@myaddress.com"
MyMail.To = "friend1@address1.com;friend2@address2.com"
MyMail.Cc = "friend3@address3.com;friend4@address4.com"
MyMail.Bcc = "friend5@address5.com;friend6@address6.com"
MyMail.Subject = "Sending Mail via CDOSYS for Windows 2000/XP"
MyMail.TextBody = "Sending email with CDOSYS Message " &_
      "objects is easy! Try it!"
MyMail.AddAttachment "c:\path\smiley.gif"
MyMail.Fields("urn:schemas:httpmail:importance").Value = 2;
MyMail.Fields.Update()
MyMail.Send()
Set MyMail = Nothing
%> "

http://www.sql-server-helper.com/forums/default.asp

Why is e-mail to certain domains being rejected?
K Pravin Kumar Reddy replied to kunal mukherjee on 18-Dec-06 01:32 PM
Whether you're using CDO, XP_SMTP_SendMail, or a 3rd party COM object to send mail, you may have experienced this problem. Your mail works some of the time — but doesn't work either when the mail is intended for your own domain, or it only works when the mail is intended for your own domain. Here are some things to check that will hopefully help resolve the issue. Many of these suggestions will require some input, and possibly some work, from your network and/or Exchange administrator.
  • Make sure that the FROM address associated with your messages is a valid address on your domain. Check your SMTP logs, the event log, and the drop / badmail folders. You might find something like this: 
     
    The server rejected the sender address. The server response was: 501 5.1.8 Domain of sender address <foo>@<blat>.com does not exist
     
    Even if you don't see this message, it might be that Exchange or your SMTP server are willing to just forward along mail from any address if the destination is outside of your network, but will only retain internal mail if the sender address is valid. 
     
  • Make sure DNS is not an issue. For example, depending on how your network is set up, the SMTP relayer within your domain may be unable to externally resolve yourdomain.com. You might also check that if you are using an external SMTP server, that your firewall or proxy is not somehow blocking mail that is going out and coming back in. 
     
  • If mail only works when sent to your domain, it might be that your firewall or proxy is blocking such mail from leaving your network (e.g. the firewall might be blocking outbound traffic on port 25, in general or only from specific machines). 
     
  • If you are using an Exchange server, make sure that it has not disabled all external relaying regardless of sender address. Check with your Exchange administrator for potential permissions, firewall, proxy, or domain-based issues. Consider using the local SMTP server instead of forwarding on to Exchange for processing. 
     
  • If you are using the SMTP virtual server within IIS, make sure that you have not set an explicit include/exclude list. In Internet Services Manager, right-click the SMTP server, hit Properties, and on the Access tab, check Connection... and Relay... 
     
    Also, consider configuring IIS to use a remote SMTP server as a "smart host" relayer, instead of handling all the mail internally. See the Delivery tab, Advanced. For further information, see http://support.microsoft.com/default.aspx/kb/293800 for Windows 2000, and http://support.microsoft.com/default.aspx/kb/324272 and http://support.microsoft.com/default.aspx/kb/816121 for Windows Server 2003. 
     
  • If Exchange or your SMTP server are set to try local delivery and then forward on to an external SMTP relayer for addresses that aren't in your domain, make sure that the external relayer does not require SMTP authentication. If so, you may need to handle this correctly — see http://www.aspfaq.com/show.asp?id=2026#auth for a code sample that uses CDO.Message to send to an SMTP server that requires outgoing authentication. 
     
  • Another option to bypass problems from within your network is to forward all mail to an external SMTP server, such as your ISP. This will avoid problems with a local SMTP server, and Exchange server configurations... but if you can't get outbound on port 25, you'll have to fix that issue first. 
     
  • For information on testing your SMTP service in Windows 2000, see http://support.microsoft.com/default.aspx/kb/286421. To test SMTP services in Windows Server 2003, see http://support.microsoft.com/default.aspx/kb/323350.

reference

http://classicasp.aspfaq.com/email/why-is-e-mail-to-certain-domains-being-rejected.html