Mail message

  • Thread starter Thread starter vinnie
  • Start date Start date
V

vinnie

To create a Mail Message, i can use the MailMessage(from, to) and so
send the mail. But how should i change the code in case there is also
a file to attach in the form? (let's say that i have already placed
the upload-file control on the page).

Thanks
~V
 
Vinnie,

You could just create instances of the Attachment class, passing in the
InputStream property from the HttpPostedFile instance that corresponds to
the uploaded file (which you get from the collection exposed by the Files
property on the HttpRequest instance).
 
Vinnie,

You could just create instances of the Attachment class, passing in the
InputStream property from the HttpPostedFile instance that corresponds to
the uploaded file (which you get from the collection exposed by the Files
property on the HttpRequest instance).

Hi Nicholais, thanks for your help.

since it's my first time in writing a mail massege code, do you think
you can post a sample code that i can study and so apply to the form
i'm working on?

This is what's on the page:

To, From, Subject, Body of a mail message;
Upload-file field;
Submit button.

Thanks
 
Hi Nicholais, thanks for your help.

since it's my first time in writing a mail massege code, do you think
you can post a sample code that i can study and so apply to the form
i'm working on?

This is what's on the page:

To, From, Subject, Body of a mail message;
Upload-file field;
Submit button.

Thanks

What do you think about this:

/ get the name of the file, Upload is a control name
string fileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(@"C:\Website\" + fileName);

// then attach it
MailMessage msgMail = new MailMessage();
msgMail.Attachments.Add(new MailAttachment(@"C:\Website\" +
fileName));

Now, what happen if the webpage is on a ISP and not on my pc(is
published on internet)? how do i save the file on a virtual directory?

Thanks
 
vinnie,

You don't. You should get the file from the Files collection exposed by
the Request property on your page. Then, you can use the InputStream that
is returned form the HttpPostedFile instance, and pass that to the
constructor of your Attachment instance.
 
vinnie,

You don't. You should get the file from the Files collection exposed by
the Request property on your page. Then, you can use the InputStream that
is returned form the HttpPostedFile instance, and pass that to the
constructor of your Attachment instance.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


On Oct 8, 2:06 pm, "Nicholas Paldino [.NET/C# MVP]"
Vinnie,
You could just create instances of the Attachment class, passing in
the
InputStream property from the HttpPostedFile instance that corresponds
to
the uploaded file (which you get from the collection exposed by the
Files
property on the HttpRequest instance).
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi Nicholais, thanks for your help.
since it's my first time in writing a mail massege code, do you think
you can post a sample code that i can study and so apply to the form
i'm working on?
This is what's on the page:
To, From, Subject, Body of a mail message;
Upload-file field;
Submit button.
Thanks
What do you think about this:
/ get the name of the file, Upload is a control name
string fileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(@"C:\Website\" + fileName);
// then attach it
MailMessage msgMail = new MailMessage();
msgMail.Attachments.Add(new MailAttachment(@"C:\Website\" +
fileName));
Now, what happen if the webpage is on a ISP and not on my pc(is
published on internet)? how do i save the file on a virtual directory?

Well, the code i told you about is the only one i found in internet.
And it doens;t work, actually.

Do you have a sample code i can study and so apply to the form?

Thanks
 
Have you looked at the documentation for the constructor for the
Attachment class?

http://msdn2.microsoft.com/en-us/library/ab7hb4y5.aspx

And the InputStream property on the HttpPostedFile class?

http://msdn2.microsoft.com/en-us/library/system.web.httppostedfile.inputstream(VS.71).aspx


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

vinnie said:
vinnie,

You don't. You should get the file from the Files collection exposed
by
the Request property on your page. Then, you can use the InputStream
that
is returned form the HttpPostedFile instance, and pass that to the
constructor of your Attachment instance.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


On Oct 8, 2:06 pm, "Nicholas Paldino [.NET/C# MVP]"
You could just create instances of the Attachment class, passing
in
the
InputStream property from the HttpPostedFile instance that
corresponds
to
the uploaded file (which you get from the collection exposed by the
Files
property on the HttpRequest instance).
Hi Nicholais, thanks for your help.
since it's my first time in writing a mail massege code, do you think
you can post a sample code that i can study and so apply to the form
i'm working on?
This is what's on the page:
To, From, Subject, Body of a mail message;
Upload-file field;
Submit button.

What do you think about this:
/ get the name of the file, Upload is a control name
string fileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(@"C:\Website\" + fileName);
// then attach it
MailMessage msgMail = new MailMessage();
msgMail.Attachments.Add(new MailAttachment(@"C:\Website\" +
fileName));
Now, what happen if the webpage is on a ISP and not on my pc(is
published on internet)? how do i save the file on a virtual directory?

Well, the code i told you about is the only one i found in internet.
And it doens;t work, actually.

Do you have a sample code i can study and so apply to the form?

Thanks
 
Hi,

A little addition.

Be aware that what Nicholas write is for the System.Net.Mail class which
started with version Net 2.0. For System.Web.Mail which is obsolete, however
the only one in Net 1.1, there is no property to use a stream in an
attachment. However you can do it using a temporaly file.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top