Webmaster Forums Banner Professional Hosting from Just Host
Welcome Guest Search | Active Topics | Members | Log In | Register

sending email with attachments Options
manzor
Posted: Thursday, May 22, 2008 12:05:48 PM
Rank: Advanced Member
Groups: Member

Joined: 1/31/2008
Posts: 32
Points: 96
Location: UK
Hi,

Can anyone help me.

In my application I need to allow the user to send mails with multiple attachments.

For that I have a FileUpload control and a button in my page. After browsing a file from file upload control user will hit the button, so that I am adding FilePath

information in a viewstate.

When user clicks on the sendMail button I am taking the filepaths that are available in viewstate and giving a statement like
DataTable dt = (DataTable)ViewState["AttachedFiles"];

foreach (DataRow drow in dt.Rows)

//Attach the file

message.Attachments.Add(new Attachment(drow["FilePath"].ToString()));

//in the filepath I have the fullpath of the file ex:FileUpload.PostedFile.FileName

when I execute the application I am getting an exception called UnauthrizedAccessException.
I am able to send a single attachment by using

message.Attachments.Add(new Attachment(FileUpload.PostedFile.InputStream,FileUpload.FileName));

but my requirement is allowing the user to attach multiple files.

Any ideas will be appreciated.
Sponsor
Posted: Thursday, May 22, 2008 12:05:48 PM
sachin
Posted: Thursday, May 22, 2008 12:07:15 PM
Rank: Advanced Member
Groups: Member

Joined: 3/3/2008
Posts: 79
Points: 237
Location: India
Sure, I'll put it in VB so that you at least have to translate it into C# :)

You can simply store the file info like so:
Session("FileBytes") = fileUpload.FileBytes()

Session("FileName") = fileUpload.FileName



And then you can access that in a later request like so:
Dim fileName As String = CType(Session("FileName"), String) Dim fileBytes As Byte() = CType(Session("FileBytes"), Byte())

Dim memoryStream As New System.IO.MemoryStream(fileBytes)



And then you can proceed as you did before:

message.Attachments.Add(new Attachment(memoryStream, fileName))



In C#, you'll just have to modify the syntax slightly, and cast the C# way ( which I prefer ).
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by HaqTech.Com
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.911 seconds.