Tuesday, 27 August 2013

UploadControl not adding file to HttpPostedFile so that it can be sent via email

UploadControl not adding file to HttpPostedFile so that it can be sent via
email

The rest of the form works as intended, on button click, the email is sent
but the attachments never make it. the code is posted below.
//FileUpload Control on applicant.aspx
<td><asp:FileUpload ID="FileUpload3" runat="server" /></td>
Here is the Server side code I"m using to send the email:
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
string mailBody = File.ReadAllText(fileName);
mailBody = mailBody.Replace("##firstName##", firstName.Text);
mailBody = mailBody.Replace("##lastName##", lastName.Text);
mailBody = mailBody.Replace("##Email##", emailAddress.Text);
mailBody = mailBody.Replace("##streetAddress##", streetAddress.Text);
mailBody = mailBody.Replace("##City##", City.Text);
mailBody = mailBody.Replace("##State##", State.Text);
mailBody = mailBody.Replace("##zipCode##", zipCode.Text);
mailBody = mailBody.Replace("##Phone##", phoneNumber.Text);
mailBody = mailBody.Replace("##birthDay##", birthDate.Text);
mailBody = mailBody.Replace("##Hobbies##", Hobbies.Text);
mailBody = mailBody.Replace("##Interests##", Interests.Text);
mailBody = mailBody.Replace("##aboutYourSelf##", aboutYourSelf.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Response from web site";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("oarango30@gmail.com", "Oscar
Arango");
myMessage.To.Add(new MailAddress("oscar@1000wattsmagazine.com",
"Oscar Budz"));
//myMessage.ReplyToList.Add(new MailAddress(EmailAddress.Text));
//Not sure if this is needed!
this.FileUpload1.SaveAs("c:\\" + this.FileUpload1.FileName);
foreach (string file in Request.Files)
{
HttpPostedFile hFile = Request.Files[file] as HttpPostedFile;
if (hFile.ContentLength > 0)
{
//Build an array with the file path, so we can get the
file name later.
string[] strAttachname = hFile.FileName.Split('\\');
//Create a new attachment object from the posted data and
the file name
Attachment mailAttach = new Attachment(
hFile.InputStream,
strAttachname[strAttachname.Length - 1] //Filename
(from end of our array)
);
//Add the attachment to our mail object
myMessage.Attachments.Add(mailAttach);
}
}
//if (FileUpload1.HasFile)
//{
// string headshot =
Path.GetFileName(FileUpload1.PostedFile.FileName);
// Attachment myAttachment = new
Attachment(FileUpload1.FileContent, headshot);
// myMessage.Attachments.Add(myAttachment);
//}
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
Message.Visible = true;
basicinfo.Visible = false;
personalinfo.Visible = false;
FormTable1.Visible = false;
FormTable2.Visible = false;
System.Threading.Thread.Sleep(5000);
thanks.Visible = true;
}
}
Any input on this is much appreciated, I"ve spent hours and kind any
reason why this code shouldn't work, but apparently it doesn't.
thanks in advance.

No comments:

Post a Comment