Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I use file upload control for attaching file for email in asp.net?
currently I am using the hard-coded path to send attachments which is :

VB
Dim MyAttachment As Attachment = New Attachment("C:\Users\Admin\Desktop\EAIPJ(Coloré-Rachel,Peiwen,Sherry,Fiona)\Coloré\Images\BLACK SHATTER.jpg")
            mail.Attachments.Add(MyAttachment)



I want to change it to be able to use file upload control to select the path of the file. How do I do that?

Urgent help is needed
Posted

you can you below code from your problem
C#
if (FileUpload1.HasFile)
{
  string toAddress = "you@yourprovider.com";
  string fromAddress = "you@yourprovider.com";
  string mailServer = "smtp.yourprovider.com";

  MailMessage myMailMessage = new MailMessage();

  myMailMessage.To.Add(toAddress);
  myMailMessage.From = new MailAddress(fromAddress);
  myMailMessage.Subject = "Test Message";

  string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
  Attachment myAttachment = 
                 new Attachment(FileUpload1.FileContent, fileName);
  myMailMessage.Attachments.Add(myAttachment);

  SmtpClient mySmtpClient = new SmtpClient(mailServer);

  mySmtpClient.Send(myMailMessage);
}
 
Share this answer
 
v2
Comments
Fiona Tan 7-Jun-12 13:45pm    
It gives an error after I add in the code. Here is my code

Protected Sub SendMail_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SendMail.Click

Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("fionatan06@gmail.com", "bananaboat")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress("fionatan06@gmail.com")
mail.To.Add(TextBox2.Text)
mail.Subject = TextBox3.Text
mail.IsBodyHtml = True
mail.Body = TextBox4.Text + "<br></br><br></br>This is an auto-generated message. Please do not reply. Thank you.<br></br><br></br>Colore"

string fileName = Path.GetFileName(FileUpload.PostedFile.FileName);
Attachment myAttachment = new Attachment(FileUpload.FileContent, fileName);
myMailMessage.Attachments.Add(myAttachment);
SmtpServer.Send(mail)
MsgBox("Mail Send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try


End Sub
Sandeep Mewara 7-Jun-12 14:41pm    
What error?
Fiona Tan 7-Jun-12 14:54pm    
is it because i didn't add in this code ?

if (FileUpload1.HasFile)
{

Fiona Tan 7-Jun-12 14:51pm    
all the attachment codes are underlined as blue, they never really say what error is that. just said that it is not declared and so.
Try this:
VB
Dim MyAttachment As Attachment = New Attachment(fileupload1.PostedFile.FileName)
mail.Attachments.Add(MyAttachment)


And also make sure that your upload control is not inside update panel.

      --Amit
 
Share this answer
 
am trying this code but i dont know the namespaces used in this? can somebody share? thanks a lot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900