Reputation: 89
I am using ASP.NET 3.5 and C# for developing a web application on helpdesk. I am send email to the admin when user lodges a complaint.
My issue is when the mail with attachment(word,excel,jpg,bmp etc) is sent to admin by the user the attachment contains no data.
Here is the code sample I used:
MailMessage mm = new MailMessage();
mm.To.Add([email protected]);
mm.From = new MailAddress("[email protected]");
if (FileUpload1.HasFile) /// for checking if mail has attachment
{
mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, ileUpload1.FileName));
}
mm.body="Test Message";
mm.IsBodyHtml = true;
//neceessary credentials are specified in web.config file
SmtpClient ss = new SmtpClient();
ss.Send(mm);
Upvotes: 2
Views: 621
Reputation: 1759
i suspect you are having this problem because the current location of the stream is at the end.
you could try FileUpload1.PostedFile.InputStream before calling attachment constructor and add.
i haven't actually tried this to verify, but i expect that's the source of the emptiness - the stream is getting read to end, but it's already at the end because reading the content in took it to the end. i have seen things like that before with streams.
Upvotes: 1
Reputation: 2796
To send the mail with attachment, you must first save the file from the fileuploader on your server and then you can send it in mail as an attachment. The current problem over here is, you are directly trying to send the file in the mail from fileupload control.
Once the mail is sent with attachment, then you can delete the saved file from your server.
Upvotes: 2