1

一切都应该很好,但是我的 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 

网络配置:

    <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");
        }

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

真挚地

4

3 回答 3

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

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

于 2012-11-01T18:10:29.120 回答
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);
于 2012-11-01T12:53:21.433 回答
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
于 2012-11-01T17:24:08.200 回答