2012-11-01 105 views
1

一切都应该很好,但我的web.config给了我这个奇怪的错误。web.config配置错误

如果我确定web.config中的字符串列表很好,我该如何得到这个错误?

错误:
配置错误 说明:该请求提供服务所需的配置文件的处理过程中发生了错误。请查看下面的具体错误细节并适当修改您的配置文件。

Parser Error Message: Unrecognized configuration section stringlist. 

Source Error: 


Line 6:  <connectionStrings/> 
Line 7:  
Line 8:   <stringlist key="SmtpServers"> 
Line 9:    <stringlistItem value="smtp.transip.nl" /> 
Line 10:   <stringlistItem value="localhost" /> 


Source File: C:\local\vCardGenerator.Website\vCardGenerator.Website\web.config Line: 8 

Web.config文件:

<stringlist key="SmtpServers"> 
     <stringlistItem value="smtp.transip.nl" /> 
     <stringlistItem value="localhost" /> 
    </stringlist> 

类:

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Web.Configuration; 
using System.Net.Mail; 
using System.Net.Configuration; 
using Compulutions.Net; 
using Compulutions.Web; 
using System.IO; 
using System.Web.Mail; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Web.Services.Description; 

// System.Web.Mail.SmtpMail.SmtpServer 
// using System.Web.Mail.SmtpClient; 

namespace vCardGenerator.Website.Masterpage 
{ 
    public class SendvCard 
    { // 
     public void MailvCard(string recipient, string filename) 
     { 
     Mailer smtp = new Mailer("smtpServers"); 

     /* SMTP - port 25 */ 

     smtp.AddAttachment(filename); //.vcf file Path 
     smtp.FromAddress = new MailAddress("[email protected]"); 
     smtp.Subject = "vCard"; 
     smtp.MailBody = "There is a vCard waiting for you in the attachment!"; 
     smtp.AddRecipient(recipient); 

#if !DEBUG 
     try 
     { 
#endif 
      smtp.SendMail(); 
#if !DEBUG 
     } 

     catch (Exception ex) 
     { 
      Response.Write("Exception Occured: " + ex); 
       //Responds.Write("Sending vCard Failed, Please try again!") 
     } 
#endif 
     } 
    } 
} 

类似的问题不适合我的描述。 我已经创建SendvCard.cs的实例,并调用在类(MailvCard)发送方法

aspx.cs本身:如果需要的话

 // Calls the method at the class 
      smtp.MailvCard("[email protected]", "C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf"); 
     } 

愿意提供任何其他/更多信息。

真诚

+2

什么是'stringlist'和'stringlistitem'?你是否创建/注册这些有效的配置部分? –

+0

您忘了将字符串列表添加到configSections节点吗? –

+0

是的,我将它们注册为有效的配置部分。 – Lobato

回答

2

您所看到的错误,因为你已经创建了一个新科(StringList的),而不必宣布它。首先必须声明在你的web.config的configSections面积的部分,像这样:

<configSections> 
    <section name="stringlist" type="System.Configuration.NameValueSectionHandler,System"/> 
</configSections> 

把你的StringList低于配置的根:

<stringlist key="SmtpServers"> 
    <stringlistItem value="smtp.transip.nl" /> 
    <stringlistItem value="localhost" /> 
    </stringlist> 

请参阅此链接的例子,以及如何访问这些值:Customizing SectionGroups and Sections in Web.config

另外,请记住有很多更好的方法来实现这一点。比如像Rashedul.Rubel建议的mailSettings。

1
//you can try by using the following smtp configuration in web.config 

<system.net> 
    <mailSettings> 
     <smtp> 
     <network host="SMTPServer" port="" userName="username" password="password" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 
    <appSettings> 
    <add key="Email" value="[email protected]"/> 
    </appSettings> 

//where host=your server name, port=server machines port number 

//and in code behind write the code as follows: 

     string fromEmail = ConfigurationManager.AppSettings["Email"].ToString(); 
    string toEmail = txtEmail.Text; 
    MailMessage message = new MailMessage(fromEmailAddress, toEmailAddress); 
    message.Subject = txtSubject.Text; 
    message.Body = txtBody.Text; 

    SmtpClient smtp = new SmtpClient(); 
    smtp.Send(message); 
+0

感谢您的信息,但它不是一个窗体,只是一个文本框从哪里发送邮件+附件(自动)和一个按钮(发送) – Lobato

0
Action Mailer can be used to send email. email can be sent with attachment without using form. 
web config configuration for the smtp: 

    <system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network"> 
     <network host="smtp.gmail.com" port="25" userName="username" password="password" defaultCredentials="false" enableSsl="true"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

configure smtp in IIS 

attachment can be done using Attachments.Inline["" + fileName] = System.IO.File.ReadAllBytes(filePath); 

for more information see the link below: 
http://ratiyaranmal.blogspot.com/2012/06/send-email-in-mvc-using-actionmailer.html