从ASP.NET页面“发送带附件的邮件”

时间:2013-06-06 19:39:58

标签: c# asp.net email

我收到以下错误。它将附加我试图把它给出错误。 无法找到文件'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";
        }
    }
}

我认为错误是上传文件。虽然无法解决这个问题。

2 个答案:

答案 0 :(得分:5)

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

var attachment = new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName);

答案 1 :(得分:1)

Path.GetFullPath(FileUpload1.PostedFile.FileName)

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

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

FileUpload1.SaveAs(somepathhere);

然后你可以使用该文件。