【问题标题】:web.config configuration Errorweb.config 配置错误
【发布时间】:2012-11-01 12:34:06
【问题描述】:

一切都应该很好,但是我的 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("someone@domain.nl");
        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)中的 send 方法

aspx.cs 本身:

        //  Calls the method at the class
            smtp.MailvCard("user@domain.com", "C:\\local\\vCardGenerator.Website" + "\\" + "FirstName_LastName" + ".vcf");
        }

如果需要,愿意提供任何其他/更多信息。

真诚的

【问题讨论】:

  • 什么是stringliststringlistitem?您是否将这些创建/注册为有效的配置部分?
  • 您是否忘记将字符串列表添加到 configSections 节点?
  • 是的,我将这些注册为有效的配置部分。
  • @Rafael 请向我们展示您将其注册为有效配置部分的代码?

标签: c# asp.net smtp vcf-vcard


【解决方案1】:

您看到错误是因为您在未声明的情况下创建了一个新部分(字符串列表)。您必须首先在 web.config 的 configSections 区域中声明一个部分,如下所示:

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

将你的字符串列表放在配置的根目录下:

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

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

另外,请记住,有许多明显更好的方法可以实现这一目标。比如 Rasedul.Rubel 建议的 mailSettings。

【讨论】:

    【解决方案2】:
    //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="you@yourwebsite.com"/>
      </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);
    

    【讨论】:

    • 感谢您的信息,但它不是一个表格,只是一个文本框,用于发送邮件+附件(自动)和一个按钮(发送)
    【解决方案3】:
    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
    

    【讨论】:

      猜你喜欢
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 2010-11-07
      相关资源
      最近更新 更多