简体   繁体   English

来自ASP.NET页的“发送带有附件的邮件”

[英]“Send mail with attachment” from ASP.NET Page

i am getting the following error. 我收到以下错误。 Its will the attachment that i am trying to put that it gives the error. 我尝试放入的附件会给出错误。 Could not find file 'C:\\Program Files\\Common Files\\Microsoft Shared\\DevServer\\10.0\\1.jpg'. 找不到文件“ 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>

here is my code 这是我的代码

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

I think the error is the upload file. 我认为错误是上传文件。 cant figure it out though . 虽然无法弄清楚

emd's answer is correct that you are not doing anything with the actual file bytes. emd的回答是正确的,即您没有对实际文件字节做任何事情。 However, I think we can improve on the answer by bypassing the File System. 但是,我认为我们可以绕过文件系统来改善答案。

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

Path.GetFullPath(FileUpload1.PostedFile.FileName)

-- would point to a location on your web server that doesn't exist since it is the client side file path. -会指向Web服务器上不存在的位置,因为它是客户端文件路径。 You're examining the file path rather than getting the file. 您正在检查文件路径,而不是获取文件。

Instead, you need to get the file onto the server first: 相反,您需要先将文件放入服务器:

FileUpload1.SaveAs(somepathhere);

Then you can use the file. 然后,您可以使用该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM