Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending email with C#

Status
Not open for further replies.

Dronealone

IS-IT--Management
Mar 13, 2002
64
GB
Hi,

I'm new to ASP.NET. I need to send an email from a form which is already built, posting variables to my aspx C# page.

When an ASP.NET page is called, can you not procedurally execute the code on it? The page to which the form is posted is:

<%@ Import Namespace=&quot;System.Web.Mail&quot; %>
<script language=&quot;c#&quot; runat=&quot;server&quot;>

MailMessage msg = new MailMessage();
msg.To = &quot;abc@abc.co.uk&quot;;
msg.From = &quot;abc@abc.co.uk&quot;;
msg.Subject = &quot;Subject&quot;;
msg.Body = &quot;Body&quot;;
SmtpMail.SmtpServer = &quot;mail.domain.co.uk&quot;;
SmtpMail.Send(msg);

</script>

When this page is loaded I get errors (on a shared server so don't know what these are!!! web.config needs to be changed).

Can somebody also tell me how I will access the posted form variables and get these into my email?

Thank you!
 
Request.Form[&quot;var&quot;] will get variables from an http post

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
thanks for that.

Is there a reason why the above code won't execute when the page is submitted to?
 
Yup it's not in any event. Remember that no matter what language you use ASP.NET is OO and event driven.

From the look of your code your using spaggetti code also known as inline code. May I suggest that you look into converting to the code-behind module. I've found this method to be alot better as far as readability/reusability. It makes your life so much easier.

But enough of the ranting, you've asked a question. Is your form page an aspx page? If it is there is no reason for you to post to another page just to send the data. I know that this was common practice in old school asp but with .NET's ability to deal with post backs this is no longer a problem.

You have your form page so simply add a server side button, then in the event handler for that button write your email code.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top