报错信息,如下:
异常详细信息: System.ArgumentException: “gb2312 ”不是受支持的编码名。
参数名: name源错误: 
行 53:         //定义邮件正文,主题的编码方式
行 54:         message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
行 55:         message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312 ");
行 56: 
行 57:         //获取或设置一个值,该值指示电子邮件正文是否为   HTML。   
 源文件: e:\ls\mail2\Default.aspx.cs    行: 55 前台代码如下:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        smtp地址:
        <asp:TextBox ID="txtSmtp" runat="server"></asp:TextBox><br />
        用户名:<asp:TextBox ID="txtUserName" runat="server" Width="166px"></asp:TextBox><br />
        密码:<asp:TextBox ID="txtPwd" runat="server" Width="181px"></asp:TextBox><br />
        发件人地址:<asp:TextBox ID="txtSender" runat="server" Width="272px"></asp:TextBox><br />
        收件人地址:<asp:TextBox ID="txtAddressee" runat="server" Width="270px"></asp:TextBox><br />
        邮件标题:<asp:TextBox ID="txtTitle" runat="server" Width="285px"></asp:TextBox><br />
        邮件正文:<asp:TextBox ID="txtBody" runat="server" Height="69px" TextMode="MultiLine"
            Width="284px"></asp:TextBox><br />
        <br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click1" Text="Button" /></div>
    </form>
</body>
</html>后台代码如下:
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.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }    ///   <summary> 
    ///   发送邮件 
    ///   </summary> 
    ///   <param   name= "strSmtpServer "> smtp地址 </param> 
    ///   <param   name= "UserName "> 用户名 </param> 
    ///   <param   name= "Password "> 密码 </param> 
    ///   <param   name= "strFrom "> 发信人地址 </param> 
    ///   <param   name= "strto "> 收信人地址 </param> 
    ///   <param   name= "strSubject "> 邮件标题 </param> 
    ///   <param   name= "strBody "> 邮件正文 </param> 
    public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
    {
        //生成一个   使用SMTP发送邮件的客户端对象 
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);        //表示以当前登录用户的默认凭据进行身份验证 
        client.UseDefaultCredentials = true;        //包含用户名和密码 
        client.Credentials = new System.Net.NetworkCredential(UserName, Password);        //指定如何发送电子邮件。 
        //Network                                             电子邮件通过网络发送到   SMTP   服务器。     
        //PickupDirectoryFromIis               将电子邮件复制到挑选目录,然后通过本地   Internet   信息服务   (IIS)   传送。     
        //SpecifiedPickupDirectory           将电子邮件复制到   SmtpClient.PickupDirectoryLocation   属性指定的目录,然后由外部应用程序传送。             client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;        //建立邮件对象   
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);        //定义邮件正文,主题的编码方式
        message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
        message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312 ");        //获取或设置一个值,该值指示电子邮件正文是否为   HTML。   
        message.IsBodyHtml = false;        //指定邮件优先级         message.Priority = System.Net.Mail.MailPriority.Normal;        //添加附件 
        //System.Web.Mail.MailAttachment   mailAttachment=new   System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");   
        if (strFileName != " " && strFileName != null)
        {
            Attachment data = new Attachment(strFileName);
            message.Attachments.Add(data);
        }
        //发件人身份验证,否则163   发不了 
        client.Credentials = new System.Net.NetworkCredential(strFrom, Password);        //发送 
        client.Send(message);
    } 
    protected void Button2_Click1(object sender, EventArgs e)
    {
        try
        {
            SendMail(txtSmtp.Text, txtUserName.Text, txtPwd.Text, txtSender.Text, txtAddressee.Text,txtTitle.Text,txtBody.Text,"tempFile");
        }
        catch (Exception ex)
        {            throw new Exception(ex.Message, ex);
        }
    }
}

解决方案 »

  1.   

    message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312 ");
    ---------
    多了个空格...细心一点...
      

  2.   

    很佩服高手的眼力!
    可是一波未平一波又起啊.刚把这个"空格"取掉现在又出现了新状况:
    对路径“d:\Program Files\Microsoft Visual Studio 8\Common7\IDE\tempFile”的访问被拒绝。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.UnauthorizedAccessException: 对路径“d:\Program Files\Microsoft Visual Studio 8\Common7\IDE\tempFile”的访问被拒绝。 ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。源错误: 
    行 66:         if (strFileName != " " && strFileName != null)
    行 67:         {
    行 68:             Attachment data = new Attachment(strFileName);
    行 69:             message.Attachments.Add(data);
    行 70:         }
     源文件: e:\ls\mail2\Default.aspx.cs    行: 68 我给tempFile添加了everyone和network service用户都赋给了"完全控制权限"依旧报这个错,请高手帮我解决,我要加分的.
      

  3.   

    如果是XP系统...试试给tempFile添加ASPNET用户授予“更改”权限...建议你发布网站后再调试或者升级到Web应用程序...
      

  4.   

    我有机器的系统是Win2003
    网站发布后,报错信息还是没变.
      

  5.   

    同志们:   我发现CSDN也有问题了,不能结贴.我在结贴时,4楼我自己跟了贴,但默认就给我分配了6分,我即删不掉也不能改(我偿试改成0,但修改不了),一直提示"不能给自己的给分".
        各位这个问题是不是委奇怪?如果版主看到了,请帮我处理一下.给一楼8分,其他个楼(除了本人),每楼2分.
      

  6.   

    发送邮件的调试成功代码
    .ASPX
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>发送邮件代码--</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <br />
            用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
            密 码:<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox><br />
            <br />
            SMTP服务器:<asp:TextBox ID="txtSmtp" runat="server"></asp:TextBox><br />
            发件地址:<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox><br />
            收件地址:<asp:TextBox ID="txtTo" runat="server"></asp:TextBox><br />
            主题:<asp:TextBox ID="txtSubject" runat="server" Width="321px"></asp:TextBox><br />
            正文:<asp:TextBox ID="txtBody" runat="server" Height="189px" TextMode="MultiLine"
                Width="324px"></asp:TextBox><br />
            附件:<input id="fileUp" runat="server" style="width: 324px" type="file" /><br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
        </form>
    </body>
    </html>.CS代码
    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.Net.Mail;
    using System.IO;public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }    ///   <summary> 
        ///   发送邮件 
        ///   </summary> 
        ///   <param   name= "strSmtpServer "> smtp地址 </param> 
        ///   <param   name= "UserName "> 用户名 </param> 
        ///   <param   name= "Password "> 密码 </param> 
        ///   <param   name= "strFrom "> 发信人地址 </param> 
        ///   <param   name= "strto "> 收信人地址 </param> 
        ///   <param   name= "strSubject "> 邮件标题 </param> 
        ///   <param   name= "strBody "> 邮件正文 </param>                 
        public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
        {
            //生成一个   使用SMTP发送邮件的客户端对象 
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);        //表示以当前登录用户的默认凭据进行身份验证 
            client.UseDefaultCredentials = true;        //包含用户名和密码 
            client.Credentials = new System.Net.NetworkCredential(UserName, Password);        //指定如何发送电子邮件。 
            //Network                                             电子邮件通过网络发送到   SMTP   服务器。     
            //PickupDirectoryFromIis               将电子邮件复制到挑选目录,然后通过本地   Internet   信息服务   (IIS)   传送。     
            //SpecifiedPickupDirectory           将电子邮件复制到   SmtpClient.PickupDirectoryLocation   属性指定的目录,然后由外部应用程序传送。             client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;        //建立邮件对象   
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);        //定义邮件正文,主题的编码方式 
            message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
            message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");        //获取或设置一个值,该值指示电子邮件正文是否为   HTML。   
            message.IsBodyHtml = false;        //指定邮件优先级         message.Priority = System.Net.Mail.MailPriority.Normal;        //添加附件 
            //System.Web.Mail.MailAttachment   mailAttachment=new System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");   
            if (strFileName != "" && strFileName != null)
            {
                System.Net.Mail.Attachment data =new System.Net.Mail.Attachment(strFileName);
                message.Attachments.Add(data);
            }        //发件人身份验证,否则163   发不了 
            client.Credentials = new System.Net.NetworkCredential(strFrom, Password);        //发送 
            client.Send(message);
        }    protected void Button1_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            fileName = fileUp.PostedFile.FileName;
            SendMail(txtSmtp.Text, txtUserName.Text.ToString(), txtPwd.Text.ToString(), txtFrom.Text.ToString(), txtTo.Text.ToString(), txtSubject.Text.ToString(), txtBody.Text.ToString(),fileName);
        }
    }