2012-08-06 71 views
0

我有一個表單的問題,我不能發送它。如果我提出錯誤的細節(安全代碼,電子郵件,空白字段等),我會在屏幕上看到錯誤,這是正確的。聯繫表格不起作用

但是,當我輸入正確的數據,我不能發送的形式,我得到這個錯誤:

There was an error submitting your form. Please check the following:

但列表爲空。

SmtpClient smtpClient = new SmtpClient(); 
    MailMessage message = new MailMessage(); 
    try 
    { 
     MailAddress fromAddress = new MailAddress("[email protected]", "string1"); 
     // decide who message goes to 
     string emailGoesTo = "[email protected]"; 

     MailAddress toAddress = new MailAddress(emailGoesTo.ToString(), "string1"); 

     message.From = fromAddress; 
     message.To.Add(toAddress); 
     message.To.Add("[email protected]"); 
     message.Subject = "string2"; 
     message.Body = "New Website Contact Request"; 
     message.Body += "------------------------------------------\r\n"; 
     message.Body += "Name: " + your_name.Text + "\r\n"; 
     message.Body += "Email: " + your_email.Text + "\r\n"; 
     message.Body += "Telephone: " + your_telephone.Text + "\r\n"; 
     message.Body += "Company: " + your_company.Text + "\r\n"; 
     message.Body += "Address: " + your_address.Text + "\r\n"; 
     message.Body += "Postcode: " + your_zip.Text + "\r\n"; 
     message.Body += "Enquiry: " + your_enquiry.Text + "\r\n"; 

     // smtpClient.Host = "string3"; 
     smtpClient.Host = "string4"; 
     smtpClient.Credentials = new System.Net.NetworkCredential("string5", "string6"); 
     smtpClient.Send(message); 
     Response.Redirect("thankyou.aspx"); 
    } 
    catch (Exception ex) 
    { 
     statusLabel.Text = "Coudn't send the message!"; 
    } 

我noobie,所以有ANY1 string5 & string6好嗎?

還有什麼問題?如何使這個表單工作?

+1

你有沒有在調試過程中通過代碼,看看它在哪裏失敗?我的第一個猜測是格式或smtpclient.host的信息是錯誤的。如果你使用交換,你的電子郵件服務器需要允許從任何生成電子郵件的機器(你的ip進行測試)進行中繼,但通常不會發出錯誤,它只是不發送郵件。我已經添加了一個工作郵件發送的例子作爲答案,您可以從 – Brian 2012-08-06 15:36:00

回答

2

從來沒有試圖使用smtpclient.host,但下面的作品。只需要添加郵件服務器的IP地址/主機名稱,並確保它被設置爲允許從調用代碼的機器進行中繼。並用您想要的文本替換字符串。我建議從簡單的事情開始,而不是從表單字段中提供消除任何外部問題。一旦郵件被髮送,然後開始添加額外的碎片。

public static string mailServer = 'IP address or host name of mail server' 
    public static void Send() 
    { 
     string subject = "test subject"; 
     string address = "[email protected]"; 
     string body = "some mail body"; 
     MailMessage mm = new MailMessage(); 
     mm.From = new MailAddress("[email protected]"); //on behalf of 
     mm.To.Add(new MailAddress(address)); 


     mm.IsBodyHtml = true; 
     mm.Subject = subject; 
     mm.Body = body; 
     SmtpClient server = new SmtpClient(mailServer); 
     server.Send(mm); 
    } 
} 
+0

thx開始回覆,恐怕我無法調試,因爲我只有Visual Studion演示。我在PHP代碼,不知道太多關於ASP,並沒有工具。我認爲所有電子郵件都要交換。我猜客戶會改變一些事情,這就是爲什麼我會得到這個錯誤。有沒有辦法以不同的方式發送聯繫表單/電子郵件? – miszczu 2012-08-06 15:56:40

+0

試試上面的代碼,並將地址設置爲您的電子郵件地址,然後查看它是否有效。如果這樣做,那麼你可以開始從你的代碼中替換你需要的部分。如果沒有,那麼問題就在於如何調用服務器。使用此代碼,您只需要郵件服務器的IP地址或url(即mail.domainname.com)。 – Brian 2012-08-06 16:56:30

1

您需要更新您的郵件服務器的名稱/域名信息您smtpClinet.Host。證書需要用可以訪問郵件服務器的用戶的用戶名和密碼進行更新。有時候這可以省略。

2

string5和string6是郵件服務器(用戶名和密碼)上的電子郵件憑據。你應該更好地瞭解我們;-)

+0

是的,我同意我應該知道,這是在我的腦海裏,但不知道。這個網站是由他的舊供應商的客戶採取的,他把它交給我們。而我在PHP代碼,而不是asp :) thx – miszczu 2012-08-06 15:47:35