Reputation: 57
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'.
<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("[email protected]", "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("[email protected]", "*************");
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.
Upvotes: 0
Views: 4043
Reputation: 4244
emd's answer is correct that you are not doing anything with the actual file bytes. However, I think we can improve on the answer by bypassing the File System.
var attachment = new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.PostedFile.FileName);
Upvotes: 5
Reputation: 1223
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. 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.
Upvotes: 1