2012-11-02 126 views
0

我試圖發送按鈕上的郵件點擊。在本地pc上代碼正在工作,但在服務器上代碼無法工作。它提供了錯誤 「無法連接到遠程serverSource:-SystemData: - System.Collections.ListDictionaryInternal」 請幫助發送按鈕上的郵件點擊

home.aspx

public partial class About : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     SendContactUsEmail(); 
     SendAutoResponseEmail(); 

    } 
    private bool SendContactUsEmail() 
    { 
     MailMessage MailObject = new MailMessage(); 

     MailObject.From = new MailAddress(ConfigurationSettings.AppSettings["EmailFrom"].ToString()); 
     MailObject.To.Add(new MailAddress("[email protected]")); 
     MailObject.Subject = " technosys Contact Alert Subject : "; 
     string ErrorHandling_Message = "Dear Manager," + " " + "<br/>" + "A customer want to contact you with following details:"; 
     ErrorHandling_Message += "<br><br>Name: vesh"; 
     ErrorHandling_Message += "<br><br>Email: [email protected]"; 
     ErrorHandling_Message += "<br><br>Company: technosys"; 
     ErrorHandling_Message += "<br><br>Phone: 123456789"; 
     ErrorHandling_Message += "<br><br>Message: Test mail on live server"; 
     ErrorHandling_Message += "<br><br>Sincerely" + "<br>" + "Web Controller" + "<br>" + "<b>Technosys</b>"; 
     try 
     { 
      MailObject.Body = ErrorHandling_Message; 
      MailObject.IsBodyHtml = true; 
      MailObject.Priority = MailPriority.High; 
      SmtpClient smtp = new SmtpClient(); 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 
      smtp.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["EmailFrom"].ToString(), ConfigurationSettings.AppSettings["Password"].ToString()); 
      smtp.EnableSsl = true; 
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      smtp.Send(MailObject); 
      Label1.Text = "Done"; 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = ex.Message; 
      return false; 
     } 
    } 
    private bool SendAutoResponseEmail() 
    { 
     MailMessage MailObject = new MailMessage(); 

     MailObject.From = new MailAddress(ConfigurationSettings.AppSettings["EmailFrom"].ToString()); 
     MailObject.To.Add(new MailAddress("[email protected]")); 
     MailObject.Subject = "Thanks for your request at technosys"; 
     string ErrorHandling_Message = "Dear, " + "<br/></br>" + "Thanks for your email. You can expect to hear back from us with an answer to your inquiry within : 24 hours."; 
     ErrorHandling_Message += "<br><br>We look forward to helping you."; 
     ErrorHandling_Message += "<br><br>Sincerely" + "<br>" + "<br>" + "<b>Technosys</b>" + "<br><img alt=\"\" src=\"http:/images/logomain.jpg\" >"; 
     try 
     { 
      MailObject.Body = ErrorHandling_Message; 
      MailObject.IsBodyHtml = true; 
      MailObject.Priority = MailPriority.High; 
      SmtpClient smtp = new SmtpClient(); 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 
      smtp.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["EmailFrom"].ToString(), ConfigurationSettings.AppSettings["Password"].ToString()); 
      smtp.EnableSsl = true; 
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      smtp.Send(MailObject); 
      Label1.Text = "Done"; 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = ex.Message; 
      return false; 
     } 
    } 
} 

的web.config

<?xml version="1.0"?> 
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use 
    the Website->Asp.Net Configuration option in Visual Studio. 
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\v2.x\Config 
--> 
<configuration> 
    <appSettings> 
     <add key="EmailFrom" value="[email protected]"/> 
     <add key="Password" value="best"/> 
    </appSettings> 
    <connectionStrings/> 
    <system.web> 
    <customErrors mode="Off"> 

    </customErrors> 
<!-- 
      Set compilation debug="true" to insert debugging 
      symbols into the compiled page. Because this 
      affects performance, set this value to true only 
      during development. 
     --> 
     <compilation debug="true" targetFramework="4.0"> 
     </compilation> 
     <!-- 
      The <authentication> section enables configuration 
      of the security authentication mode used by 
      ASP.NET to identify an incoming user. 
     --> 
     <authentication mode="Windows"/> 
     <!-- 
      The <customErrors> section enables configuration 
      of what to do if/when an unhandled error occurs 
      during the execution of a request. Specifically, 
      it enables developers to configure html error pages 
      to be displayed in place of a error stack trace. 

     <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 
      <error statusCode="403" redirect="NoAccess.htm" /> 
      <error statusCode="404" redirect="FileNotFound.htm" /> 
     </customErrors> 
     --> 
     <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web> 
    <!-- 
     The system.webServer section is required for running ASP.NET AJAX under Internet 
     Information Services 7.0. It is not necessary for previous version of IIS. 
    --> 
</configuration> 
+2

哪一行代碼產生錯誤?顯示完整的堆棧跟蹤。 – David

+0

線我找不到,但使用標籤我得到了這個錯誤「無法連接到遠程服務器」。此代碼完美地在本地工作,但是在我的主機服務器上傳之後,它無法正常工作。 –

+0

作爲一個完整的附註,您可以通過將ErrorHandling_Message字符串更改爲StringBuilder來提高性能。字符串並不實際連接,它們會在每次更改時被破壞並重建。沒有真正幫助你的問題,只是一個想法:) – Darren

回答

0

何不你只是用這個:

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com",587); 
smtpClient.EnableSsl = true; 
System.Net.NetworkCredential credentials = new 
        System.Net.NetworkCredential(
         ConfigurationSettings.AppSettings["EmailFrom"].ToString(), 
         ConfigurationSettings.AppSettings["Password"].ToString() 
        ); 
smtpClient.Credentials = credentials; 

string[] tos; 
this.to=this.to.Replace(',',' '); 
//this.Bcc = this.bcc; 
tos=this.to.Split(); 
for (int i = 0; i < tos.Length; i++) 
{ 
    MailMessage ml = new MailMessage(this.from, tos[i].ToString(), this.sub, 
            this.msg); 
    ml.IsBodyHtml = true; 
    smtpClient.Send(ml); 
} 
+0

這意味着什麼? –

+0

'到,從,子,味精等...'是變量..你可以在同一個頁面聲明... @ user1724912 –

+0

爲什麼使用arrey? –

相關問題