2012-02-16 161 views
0

我從w3school抓取了這個示例,但是當我添加if語句來檢查電子郵件是否已發送時,即使我收到電子郵件,也會顯示錯誤代碼。CDO - 即使發送電子郵件,發送電子郵件也會返回false

我不知道asp如何工作,但我假設myMail返回一個布爾值?或者不是?如何檢查電子郵件是否已發送。

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

回答

1

的。發送method只是簡單地將消息發送而不返回的響應。

您可以處理因故障引發錯誤發送的消息類似下面的代碼:

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 
+0

+1,但也請您正確使用不當'AddHeader'的使用'ContentType'屬性。 – AnthonyWJones 2012-02-17 08:46:15

+0

@AnthonyWJones Doh,當然,剛剛複製了OP代碼並粘貼了! – 2012-02-17 09:25:06