0

我收到以下错误。我试图添加的附件会给出错误。 找不到文件“C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\1.jpg”。

            <div class="nav-collapse collapse">
                <p class="navbar-text pull-right">
                    Logged in as
                    <asp:Label ID="lblUsername" runat="server"></asp:Label>
                    <asp:LinkButton ID="lnkLogout" runat="server">Logout</asp:LinkButton>
                </p>
            </div>
        </div>
    </div>
</div>
    <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
<div class="well"> 
    <div class="control-group">
        <asp:Label ID="Label3" runat="server" Text="To" CssClass="control-label"></asp:Label>
        <div class="controls">
            <asp:TextBox ID="txtTo" runat="server" CssClass="span12"></asp:TextBox>
        </div>
    </div>
    <div class="control-group">
        <asp:Label ID="Label1" runat="server" Text="CC" CssClass="control-label"></asp:Label>
        <div class="controls">
            <asp:TextBox ID="txtCC" runat="server" CssClass="span12"></asp:TextBox>
        </div>
    </div>
    <div class="control-group">
        <asp:Label ID="Label4" runat="server" Text="BCC" CssClass="control-label"></asp:Label>
        <div class="controls">
            <asp:TextBox ID="txtBCC" runat="server" CssClass="span12"></asp:TextBox>
        </div>
    </div>
    <div class="control-group">
        <asp:Label ID="Label2" runat="server" Text="Subject" CssClass="control-label"></asp:Label>
        <div class="controls">
            <asp:TextBox ID="txtSubject" runat="server" CssClass="span12"></asp:TextBox>
        </div>
    </div>

            <div class="control-group">
        <asp:Label ID="Label5" runat="server" Text="Add Attachment" CssClass="control-label"></asp:Label>
        <div class="controls">
            <asp:FileUpload ID="FileUpload1" runat="server" />
        </div>
    </div>


    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <cc1:Editor ID="Editor1" runat="server" Height="500px" />
</div>

        <div class="text-center">
        <asp:Button ID="btnSubmit" runat="server" Text="Send" 
            CssClass="btn btn-primary" onclick="btnSubmit_Click"/>
        <input id="Reset1" type="reset" value="Reset" class="btn" />
    </div>

这是我的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Final_Year_Project_Allocation_System.Admin
{
    public partial class SendMailToUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {

            //send email
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("projectallocationsystem@gmail.com", "Project Allocation System");
            msg.To.Add(new MailAddress(txtTo.Text));
            if (txtCC.Text != "")
            {
                msg.CC.Add(new MailAddress(txtCC.Text));
            }

            if (txtBCC.Text != "")
            {
                msg.Bcc.Add(new MailAddress(txtBCC.Text));
            }


            if (FileUpload1.PostedFile.ContentLength > 0)
            {
                Attachment attachment = new Attachment(Path.GetFullPath(FileUpload1.PostedFile.FileName));
                msg.Attachments.Add(attachment);
            }


            msg.Subject = txtSubject.Text;
            msg.Body = Editor1.Content;
            msg.IsBodyHtml = true;

            SmtpClient setp = new SmtpClient();
            setp.Host = "smtp.gmail.com";
            setp.Port = 587;
            setp.UseDefaultCredentials = true;
            setp.Credentials = new NetworkCredential("projectallocationsystem@gmail.com", "*************");

            setp.EnableSsl = true;

            setp.Send(msg);

            lblMessage.Text = "Please Check your mail.";
            lblMessage.CssClass = "alert alert-success";
        }
        catch (Exception err)
        {
            lblMessage.Text = "Error: " + err.Message;
            lblMessage.CssClass = "alert alert-error";
        }
    }
}

我认为错误是上传文件。虽然想不通

4

2 回答 2

5

emd 的回答是正确的,您没有对实际文件字节做任何事情。但是,我认为我们可以通过绕过文件系统来改进答案。

var attachment = new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName);
于 2013-06-06T20:05:21.653 回答
1

Path.GetFullPath(FileUpload1.PostedFile.FileName)

-- 将指向您的 Web 服务器上不存在的位置,因为它是客户端文件路径。您正在检查文件路径而不是获取文件。

相反,您需要先将文件放到服务器上:

FileUpload1.SaveAs(somepathhere);

然后就可以使用该文件了。

于 2013-06-06T19:41:33.027 回答