2013-07-21 57 views
1

嗨,我使用下面的代碼發送預訂形式,但它在本地工作,但是當我把它活了失敗,不知道爲什麼:發送帶有C#本地工作,但靜靜地失敗過

保護無效的Page_Load(對象發件人,EventArgs的){

} 
protected void SendMail() 
{ 
    var sDate = startDate.Text; 
    var eDate = endDate.Text; 

    // Gmail Address from where you send the mail 
    var fromAddress = "[email protected]"; 
    // any address where the email will be sending 
    var toAddress = "[email protected]"; 
    //Password of your gmail address 
    const string fromPassword = "xxx"; 
    // Passing the values and make a email formate to display 
    string subject = YourSubject.Text.ToString(); 
    string body = "From: " + YourName.Text + "\n"; 
    body += "Email: " + YourEmail.Text + "\n"; 
    body += "Contact Number: " + txtContactNumber.Text + "\n"; 
    body += "Subject: " + YourSubject.Text + "\n"; 
    body += "Question: \n" + Comments.Text + "\n"; 
    body += "Pet Name(s): \n" + txtPetName.Text + "\n"; 
    body += "Start Date: \n" + sDate + "\n"; 
    body += "End Date: \n" + eDate + "\n"; 

    // smtp settings 
    var smtp = new System.Net.Mail.SmtpClient(); 
    { 
     smtp.Host = "smtp.gmail.com"; 
     smtp.Port = 587; 
     smtp.EnableSsl = true; 
     smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
     smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); 
     smtp.Timeout = 20000; 
    } 
    // Passing values to smtp object 
    smtp.Send(fromAddress, toAddress, subject, body); 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     //here on button click what will done 
     SendMail(); 
     DisplayMessage.Text = "Thank you for contacting us we will get back to you shortly. If you would like to talk to us, please call 0208 236 9572"; 
     DisplayMessage.Visible = true; 
     YourSubject.Text = ""; 
     YourEmail.Text = ""; 
     YourName.Text = ""; 
     Comments.Text = ""; 
    } 
    catch (Exception) { } 
} 

它不發送消息或顯示「感謝您聯繫......」

這是網站www.silverhillcattery.co .uk

任何幫助將不勝感激!

更新:

我加入了錯誤信息的建議,我現在得到以下錯誤:

「SMTP服務器要求安全連接或客戶端未通過身份驗證」

我將研究這個問題。

回答

2

我只能猜測您的憑據在您的本地環境中工作,但不在您的實時環境中。如果你的代碼在發送時發生異常,它將繞過顯示代碼並直接進入你的「catch」代碼。

爲了調試它,我會在顯示屏之後放置發送以查看會發生什麼,或者更合適地修改catch(exception)以獲取錯誤並顯示該錯誤。

catch (Exception ex) 
{ 
      DisplayMessage.Text = "Failed to send email. Error = " + ex.message; 
     DisplayMessage.Visible = true; 
} 
相關問題