Adam
Adam

Reputation: 9049

CDO - Sending email returns false even though the email is sent

I grabbed this example from w3school, however when I add an if statement to check if the email has been sent, it displays the false code even though I receive the email.

Im not sure how asp works, but I'm assuming myMail returns a boolean? Or does it not? How do I check if the email has been sent.

<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="[email protected]"
myMail.To="[email protected]"
myMail.HTMLBody = "<h1>This is a message.</h1>"
If myMail.Send Then
    Response.AddHeader "Content-type", "application/json"
    Response.Write "{ request: 'success'}"
Else
    Response.AddHeader "Content-type", "application/json"
    Response.Write "{ request: 'failed'}"
End If

set myMail=nothing
%>

Upvotes: 0

Views: 2062

Answers (1)

Nicholas Murray
Nicholas Murray

Reputation: 13533

The .Send method just simply sends the message without returning a response.

You can handle an error raised by a failure to send the message something like the code below:

On Error Resume Next
myMail.Send
If Err.Number = 0 then
    Response.ContentType="application/json"
    Response.Write "{ request: 'success'}"
Else
    Response.ContentType="application/json"
    Response.Write "{ request: 'failed'}"
End If

Upvotes: 2

Related Questions