4
    <%
Dim sent
Dim YourName
Dim YourEmail
Dim YourMessage
Set myMail2=CreateObject("CDO.Message")
YourName = Trim(Request.Form("Name")) 
YourEmail = Trim(Request.Form("Email")) 
YourMessage = Trim(Request.Form("Message")) 

Dim Body
Dim body2
Body = Body & "Their Name: " & VbCrLf & YourName & VbCrLf & VbCrLf
Body = Body & "Their Email: " & VbCrLf & YourEmail & VbCrLf & VbCrLf
Body = Body & "Their Message: " & VbCrLf & YourMessage & VbCrLf & VbCrLf

Set myMail=CreateObject("CDO.Message")
myMail.Subject="A New Enquiry!"
myMail.From="admin@musicalmatters.co.uk"
myMail.To="james@devine.eu"
myMail.TextBody=Body
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.1and1.com"
'Server port
myMail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
myMail.Configuration.Fields.Update
myMail.Send
set myMail=nothing



body2="Thank you for contacting us!" & VbCrLf & "This is just a brief message to let you know your form was submitted successfully!"& VbCrLf & VbCrLf & "You may reply to this address, but you may not necessarily receive a reply, "& "you should receive a reply in 1-2 business day(s)!"& VbCrLf & "Thank you very much,"& VbCrLf & VbCrLf & "Musical Matters."& VbCrLf & "admin@musicalmatters.co.uk"

Set myMail2=CreateObject("CDO.Message")
myMail2.Subject="Thanks for Contacting Us!"
myMail2.From="admin@musicalmatters.co.uk"
myMail2.To=YourEmail
myMail2.TextBody=body2 

myMail2.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
'Name or IP of remote SMTP server
myMail2.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.1and1.com"
'Server port
myMail2.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25
myMail2.Configuration.Fields.Update
myMail2.Send

If myMail2.Send="" Then 
Response.Redirect("http://www.musicalmatters.co.uk/success.htm")
set myMail2=nothing

Else
Response.Redirect("http://www.musicalmatters.co.uk/error.htm")
set myMail2=nothing
End If

The problem with my asp script is that I need to check whether the emails were sent or not and redirect them to an error or success page depending on the result.

    If myMail2.Send="" Then 
Response.Redirect("http://www.musicalmatters.co.uk/success.htm")
set myMail2=nothing

Else
Response.Redirect("http://www.musicalmatters.co.uk/error.htm")
set myMail2=nothing
End If

in the code above mymail2.Send="" because i was testing something, i know i have to change the value to true or false, please be hasty with your answers!

Thanks in advance!

Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
James
  • 349
  • 1
  • 6
  • 17

2 Answers2

2

Seems to require using On Error statement.

On Error Resume Next
myMail2.Send
If Err.Number = 0 Then
    set myMail2 = Nothing
    Response.Redirect("http://www.musicalmatters.co.uk/success.htm")
Else
    set myMail2 = Nothing
    Response.Redirect("http://www.musicalmatters.co.uk/error.htm")
End If
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • you need to destroy the object before redirecting or it won't happen. – Dee Oct 09 '11 at 20:20
  • Yes you're right! I copied the question. I'll update. Thanks. – Kul-Tigin Oct 09 '11 at 20:22
  • @AnthonyWJones: I should clarify, it won't happen in a timely manner. Eventually it will get cleaned up, but it is better to destroy then move on. This is because scripts run from top to bottom in the page. – Dee Nov 22 '11 at 08:41
  • @Dee: A `Response.Redirect` will kill the current script so the script won't continue to run to the end in this case. – AnthonyWJones Nov 22 '11 at 10:13
  • I could be wrong...but I don't think the connection dies immediately with the script upon a redirect. – Dee Nov 27 '11 at 05:25
  • I think that this should be marked as the correct answer. Regardless of the redirect (I just display the error on the page using `Response.Write(Err.Description)` ), this allows me to see why it's not sending: `The "SendUsing" configuration value is invalid. ` +1 – James Jul 30 '13 at 14:33
2

If the email address has valid syntax and the SMTP server is up and running, the Send method will never throw error, even if the email address does not exist.

There is no way to know 100% if the email reached its destination - one thing I can think is to check (using FSO) the BadMail and Queue folders in the SMTP root after few seconds from sending, and if they contain new entry it means something went wrong.

However as you're using external mail service, you'll have to contact them and ask for a way to get notified somehow when the delivery fails.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208