Jump to content
Kaoru

Email form

Recommended Posts

I have the following code at the beginning of my page:

<%@ Page Language="VB" %><%@ Import Namespace="System.Web.Mail" %>	Sub doEmail(Source as Object, E as EventArgs)		Dim sMsg as String		sMsg+="Name :" & txtName.Text & vbcrlf		sMsg+="Email :" & txtEmail.Text & vbcrlf		Dim objEmail as New MailMessage		objEmail.To=logan_young@hotmail.com		objEmail.From=txtEmail.Text		objEmail.Subject=txtSub.Text		objEmail.Body=sMsg		objEmail.BodyFormat = MailFormat.Text		SmtpMail.SmtpServer ="smtp.gmail.com		SmtpMail.Send(objEmail)	End Sub

After that, it's the html... here's the code for my form:

<form id="frmMail" runat="server">	 <table width="50%" border="0" align="center">		  <tr>			   <td width="10%" align="right" valign="middle"><asp:Label id="lblName" runat="server" text="Name:"></asp:Label></td>			   <td colspan="2" valign="top"><asp:TextBox id="txtName" runat="server"></asp:TextBox></td>		  </tr>		  <tr>			   <td width="10%" align="right" valign="middle"><asp:Label id="lblEmail" runat="server" text="E-mail:"></asp:Label></td>			   <td colspan="2" valign="top"><asp:TextBox id="txtEmail" runat="server"></asp:TextBox></td>		  </tr>		  <tr>			   <td width="10%" align="right" valign="middle"><asp:Label id="lblSub" runat="server" text="Subject:"></asp:Label></td>			   <td colspan="2" valign="top"><asp:TextBox id="txtSub" runat="server"></asp:TextBox></td>		   </tr>		   <tr>			   <td valign="middle" align="right" width="10%"><asp:Label id="lblMessage" runat="server" text="Message:"></asp:Label></td>			   <td colspan="2" valign="top"><asp:TextBox id="txtMessage" runat="server" height="100px" width="300px"></asp:TextBox></td>			</tr>			<tr>			   <td width="10%" align="right">  </td>			   <td colspan="2"><asp:ImageButton id="btnSend" runat="server" command="doEmail" ImageUrl="Images/submit.gif" /></td>			</tr>	 </table></form>

I don't get any errors, but it doesn't work when I click the submit button... What DOES happen, is the subroutine is written at the top of the page.What's wrong here???

Share this post


Link to post
Share on other sites

Try using "OnClick"<td width="10%" align="right">  </td> <td colspan="2"><asp:ImageButton id="btnSend" runat="server" OnClick="doEmail" ImageUrl="Images/submit.gif" />

Share this post


Link to post
Share on other sites

OK, done that, now I'm getting the following:

Compiler Error Message: BC30456: 'doEmail' is not a member of 'ASP.myweb_astral_contact_aspx'.Source Error:Line 57: <tr>Line 58: <td width="10%" align="right">  </td>Line 59: <td colspan="2"><asp:ImageButton id="btnSend" runat="server" onClick="doEmail" ImageUrl="Images/submit.gif" /></td>Line 60: </tr>Line 61: </table>
I had this before, replacing onClick with command is what fixed it... all the forums I go to say to check the behind code or something like that... but I can't remember...

Share this post


Link to post
Share on other sites

onClick is a client side event but OnClick is the server side event you want. Adjust your code and it should work fine.

Share this post


Link to post
Share on other sites

OK, that's done, but I'm still getting the compilation error:

Compiler Error Message: BC30456: 'doEmail' is not a member of 'ASP.myweb_astral_contact_aspx'.Source Error:Line 57:		<tr>Line 58:			<td width="10%" align="right">  </td>Line 59:			<td colspan="2"><asp:ImageButton id="btnSend" runat="server" OnClick="doEmail" ImageUrl="Images/submit.gif" /></td>Line 60:		</tr>Line 61:	</table>

I've checked on microsoft website, all that's on there has something to do with DataGrids... I'm not using DataGrids...

Share this post


Link to post
Share on other sites

Add this code and remove your subroutine script....

<script runat="server">	private Sub doEmail(sender As Object, e As System.Web.UI.ImageClickEventArgs)Dim sMsg as StringsMsg+="Name :" & txtName.Text & vbcrlfsMsg+="Email :" & txtEmail.Text & vbcrlfDim objEmail as New MailMessage		objEmail.To=logan_young@hotmail.com		objEmail.From=txtEmail.Text		objEmail.Subject=txtSub.Text		objEmail.Body=sMsg		objEmail.BodyFormat = MailFormat.Text		SmtpMail.SmtpServer ="smtp.gmail.com		SmtpMail.Send(objEmail)	End Sub	</script>

Share this post


Link to post
Share on other sites

OK, that got more results now...About the smtp server being specified in the code... is it the one on the server that's receiving the email? I'm assuming so, but please confirm..I want to know because when I tried with my the gmail smtp server, I got this:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Runtime.InteropServices.COMException: The server rejected the sender address. The server response was: 530 5.7.0 Must issue a STARTTLS command first n45sm10434330pyh

Share this post


Link to post
Share on other sites

I'm happy to report that this morning when I tried using the form, it worked 100% correctly !!!!!!!!!!!!What I'm posting for now, is a problem that seemed to materialise out of thin air...When I try to view the page that contains the form, I get the following:

Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
In the web.config file, I have set <customErrors mode="Off"/> and I still don't get any change... the owner of the server maintains that there's a problem with the code, but that hasn't changed since it was working...

Share this post


Link to post
Share on other sites
In the web.config file, I have set <customErrors mode="Off"/> and I still don't get any change...
Do you mean to say that you have changed the Custom Errors mode to "Off" in the Web.config file on the server and you are still getting that error message that says you can't view the error unless you change the Custom Errors mode to "Off"?

Share this post


Link to post
Share on other sites
That's exactly what I mean, wierd isn't it...
Heh, yeah.So, the tag, is it in the correct place in your Web.config file?ASP.NET 2.0
<?xml version="1.0"?><configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">	<appSettings />	<system.web>		<customErrors mode="Off" />	</system.web></configuration>

And, is this website a root level website? That is to say, is this website a stand-alone website or is it a sub-website of a parent website? If it is a website that exists within another website, it could be that the Web.config file for the parent website is overriding the settings for the child website.Lastly, perhaps the server is set up with a machine.config file at the server level which is setting the Custom Errors mode to "RemoteOnly". You might check with the owner/operator of the server to see if this is the case.

Share this post


Link to post
Share on other sites

The site is (as far as I know) a root level site. http://astralwd.co.zaBecause I'm having problems, I've the error page from public eyes with a js alert, but if you include contact.aspx after the domain (i.e. http://www.astralwd.co.za/contact.aspx) you'll see the error. I will be contacting the server admin this evening to check the machine.config file on the server, but it looks like there's errors in the code:At the moment, I've been tweaking the code in Visual Web Developer 2005 - here's the code I have at the moment:

1|	<%@ Page Language="VB" Debug="true" %> 2|	<%@ Import Namespace="System.Net.Mail" %> 3|	<script runat="server"> 4|		 Private Sub doEmail(Source as Object, E as System.Web.UI.ImageClickEventArgs) 5|			Dim sMsg as String 6|			sMsg+="Name :" & txtName.Text & vbcrlf 7|			sMsg+="Message :" & txtMessage.Text & vbcrlf 8| 9|			Dim objEmail as New MailMessage10|			objEmail.To="support@astralwd.co.za"11|			objEmail.From=txtEmail.Text12|			objEmail.Subject=txtSub.Text13|			objEmail.Body=sMsg14|				objEmail.BodyFormat = MailMessage.IsBodyHtml15|	 		   SmtpClient.SmtpServer = "smtp.astralwd.co.za"16|				SmtpClient.Send(objEmail)17|		End Sub18|	</script>

I'm getting the following errors:

1) Line 14 - Column 9 'BodyFormat' is not a member of 'System.Net.Mail.MailMessage'.2) Line 15 - Column 9 'SmtpServer' is not a member of 'System.Net.Mail.SmtpClient'.3) Line 11 - Column 17 Name 'txtEmail' is not declared.4) Line 7 - Column 23 Name 'txtMessage' is not declared.5) Line 6 - Column 20 Name 'txtName' is not declared.6) Line 12 - Column 20 Name 'txtSub' is not declared.7) Line 10 - 3 Property 'To' is 'ReadOnly'.8) Line 14 - Column 31 Reference to a non-shared member requires an object reference.9) Line 16 - Column 9 Reference to a non-shared member requires an object reference.

Share this post


Link to post
Share on other sites

Try this

Private Sub doEmail(Source as Object, E as System.Web.UI.ImageClickEventArgs)5|			Dim sMsg as String6|			sMsg+="Name :" & txtName.Text & vbcrlf7|			sMsg+="Message :" & txtMessage.Text & vbcrlf8|9|			Dim objEmail as New MailMessage10|			objEmail.To="support@astralwd.co.za"11|			objEmail.From=txtEmail.Text12|			objEmail.Subject=txtSub.Text13|			objEmail.Body=sMsg14|				objEmail.BodyFormat = MailFormat.Html15|				SmtpMail.SmtpServer = "smtp.astralwd.co.za"16|				SmtpMail.Send(objEmail)17|		End Sub

Are those text boxes in your code? It says it can't find them.

Share this post


Link to post
Share on other sites

Yes, txtName, txtEmail, txtSub, and txtMessage are all text elements on the page.I just had a thought that it's saying it can't find them because I'm using <input type="text" id="txtName> as opposed to <asp:TextBox id="txtName"></asp:TextBox>...Why am I using <input...> you ask?? When I saw that I can't use any html controls to duplicate <textarea>, I reverted back to <input>If you can show me how to duplicate <textarea>, I'd really appreciate it.

Share this post


Link to post
Share on other sites

Right... I replaced all the elements with asp controls (asp:TextBox... etc.) and it now finds the elements, but I'm still getting the following errors:

'BodyFormat' is not a member of 'System.Net.Mail.MailMessage'. (Line 14 - Column 9)Name 'MailFormat' is not declared. (Line 14 - Column 31)Name 'SmtpMail' is not declared. (Line 15 - Column 9)Name 'SmtpMail'is not declared. (Line 16 - Column 9)Property 'To' is 'ReadOnly'. (Line 10 - Column 3)Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'. (Line 11 - Column 17)

Share this post


Link to post
Share on other sites

First off, to tell the MailMessage object that it is HTML, you do the following:

objEmail.IsBodyHtml = true

As for the SmtpMail object, you aren't declaring it anywhere as far as I can tell.

Dim SmtpMail as SmtpClientSet SmtpMail = new SmtpClient("smtp.astralwd.co.za")

With the MailMessage object, the To property isn't a string - it's a MailAddress object. So, to add a To address, you can do the following:

objEmail.To.Add(new MailAddress("support@astralwd.co.za"))

Finally, to use an <asp:TextBox to emulate a textarea, you can set the TextMode property to MultiLine:

<asp:TextBox id="myTextarea" TextMode="MultiLine" runat="server" />

Share this post


Link to post
Share on other sites

Right, now that's done, now I'm getting other errors... this CANNOT be this hard :)

Compiler Error Message: BC30311: Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'.Line 12: objEmail.From=txtEmail.Text
Here's my code yet again for reference:
<script runat="server">	Sub doEmail(Source as Object, E as System.Web.UI.ImageClickEventArgs)		Dim sMsg as String		sMsg+="Name :" & txtName.Text & vbcrlf		sMsg+="Message :" & txtMessage.Text & vbcrlf		Dim objEmail as New MailMessage		objEmail.IsBodyHtml = true		objEmail.To.Add(new MailAddress("support@astralwd.co.za"))		objEmail.From=txtEmail.Text		objEmail.Subject=txtSub.Text		objEmail.Body=sMsg		objEmail.BodyFormat = MailFormat.Html		Dim SmtpMail as SMtpClient		Set SmtpMail = new SmtpClient("smtp.astralwd.co.za")		SmtpMail.Send(objEmail)	End Sub</script>

I'm also getting these errors in Visual Web Developer 2005:

Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'. (Line 12 - Col 17)'BodyFormat' is not a member of 'System.Net.Mail.MailMessage'. (Line 15 - Col 3)Name 'MailFormat' is not declared. (Line 15 - Col 25)'SmtpClient' is not a member of 'System.Net.Mail.SmtpClient'. (Line 18 - Col 9)

Share this post


Link to post
Share on other sites

The error messages are telling you what is wrong.1) 'BodyFormat' is not a member of 'System.Net.Mail.MailMessage'. This is telling you that there is no property of the MailMessage class called "BodyFormat". Remove that line. It should have been replaced by that objEmail.isBodyHtml = true line.2) Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddress'. And "objEmail.From=txtEmail.Text". This is telling you that the "From" member of the MailMessage object is expecting a MailAddress object rather than a string. Change that line to this:

objEmail.From.Add(new MailAddress(txtEmail.Text))

3) 'SmtpClient' is not a member of 'System.Net.Mail.SmtpClient'. OK, this one is a little vague :). I don't write VB, so the error may be in the way that I wrote the code. In C# it'd look like this:

SmtpClient SmtpMail = new SmtpClient("smtp.astralwd.co.za");

It could be that you have Dim SmtpMail as SMtpClient - with a capital M rather than a lowercase m, but I could be mistaken. Maybe you could also try changing the code to look like this:

Dim SmtpMailSet SmtpMail = new SmtpClient("smtp.astralwd.co.za")

Perhaps a VB expert'll come along and help with that.EDIT: I looked on MSDN, they say to do it like this:

Dim SmtpMail As New SmtpClient("smtp.astralwd.co.za")

<mutters>Dims, Sets, bah! :)</mutters>

Share this post


Link to post
Share on other sites

Well, we seem to getting there, only now (to my complete and utter confusion), I'm getting another compilation error:

Compiler Error Message: BC30456: 'Add' is not a member of 'System.Net.Mail.MailAddress'.Line 12: objEmail.From.Add(new MailAddress(txtEmail.Text))
What confuses me about this, is that it didn't have any problems with Add being used objEmail.To ...jesh, you mentioned the following:
Dim SmtpMailSet SmtpMail = new SmtpClient("smtp.astralwd.co.za")

I'm assuming that's how it's done is C#, but just for interest, here's how it's done in VB:

Dim SmtpMail as new SmtpClientSmtpMail.SmtpClient = "smtp.astralwd.co.za"

One of the things I've learned so far... having hassled with this for around a week...

Share this post


Link to post
Share on other sites

what version of .Net are you using. You seem to be using the 1.1 (System.Web.Mail) namespace yet using the 2.0 syntax.If you are using 2.0 change it to System.Net.Mail

Share this post


Link to post
Share on other sites

I'm using the 2.0 framework, and I have been using the System.Net.Mail namespace for quite some time now...

<%@ Import Namespace="System.Net.Mail" %>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...