web.config配置错误

时间:2012-11-01 12:34:06

标签: c# asp.net smtp vcard

一切都应该不错,但是我的web.config给了我这个奇怪的错误。

如果我确定web.config中的stringlist是好的,我怎么能得到这个错误?

错误:
    配置错误     描述:处理为此请求提供服务所需的配置文件时发生错误。请查看下面的具体错误详细信息并相应地修改配置文件。

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实例,并在类中调用了send方法(MailvCard)

aspx.cs本身:

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

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

此致

3 个答案:

答案 0 :(得分:2)

您看到了错误,因为您创建了一个新的Section(stringlist)而没有声明它。您必须首先在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

另外,请记住,有许多明显更好的方法可以实现这一目标。比如邮件设置为Rashedul.Rubel建议。

答案 1 :(得分: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="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);

答案 2 :(得分: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