You'll be able to tell by this question, but I am not a programmer by profession and I recently started attempting to build a user input form using asp.net 2.0 VB.
When I publish the form everything looks good and an email is sent after I hit the "submit" button.
Bbut only the From, To, Subject, and body are coming to me in the email.
There is obviously a mailmessage property of "Body", "Subject", "From", "To", and every single example I've seen while searching the we (since 4am this morning) only uses these four fields to show how an email is constructed.
In the form I built, I also have:
First Name
Last Name
Email
Confirm Email
Zip Code
The question is how do I get the input from the form to me in the email AND how do I add a the field labe in that email.
Here's an example of what I'm hoping to get in the body of the email that's sent to me:
Partial Class _Default
Inherits System.Web.UI.Page
''' <summary>
''' Actions when the Send button is clicked.
''' </summary>
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
'Create instance of main mail message class.
Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
'Configure mail mesage
'Set the From address with user input
' mailMessage.From = New System.Net.Mail.MailAddress(txtFromAddress.Text.Trim())
'Get From address in web.config
mailMessage.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("fromEmailAddress"))
'Set additinal addresses
mailMessage.To.Add(New System.Net.Mail.MailAddress(txtToAddress.Text.Trim()))
'Set additional options
mailMessage.Priority = Net.Mail.MailPriority.High
'Text/HTML
mailMessage.IsBodyHtml = False
'Set the subject and body text
mailMessage.Subject = "Form Request"
"FirstName:"
mailMessage.Body = txtFirstName.Text.Trim()
mailMessage.Body = txtLastName.Text.Trim()
mailMessage.Body = txtToAddress.Text.Trim()
mailMessage.Body = txtzip.Text.Trim()
mailMessage.Body = txtBody.Text.Trim()
'Create an instance of the SmtpClient class for sending the email
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
'Use a Try/Catch block to trap sending errors
'Especially useful when looping through multiple sends
Try
smtpClient.Send(mailMessage)
Catch smtpExc As System.Net.Mail.SmtpException
'Log error information on which email failed.
Catch ex As Exception
'Log general errors
End Try
Thank you...BTW, do you know of a good web reference for this type back-end coding.
There is other stuff I'd like to do and modify. I'm not a big fan of asking for help until I've exhausted all my resources. Believe it or not I've been working on this all day and after your post, I had the solution in no time (mucho appreciated).
Again, I'm not a coder by trade, but would certainly like to learn enough to keep myself out of trouble.
Thanks/Lew
Marked as answer by PensacolaLew on Dec 09, 2012 12:34 PM
PensacolaLew
Member
1 Points
10 Posts
Pull input data from asp.net form
Dec 05, 2012 08:23 PM|LINK
I appreciate your help!
You'll be able to tell by this question, but I am not a programmer by profession and I recently started attempting to build a user input form using asp.net 2.0 VB.
When I publish the form everything looks good and an email is sent after I hit the "submit" button.
Bbut only the From, To, Subject, and body are coming to me in the email.
There is obviously a mailmessage property of "Body", "Subject", "From", "To", and every single example I've seen while searching the we (since 4am this morning) only uses these four fields to show how an email is constructed.
In the form I built, I also have:
First Name
Last Name
Email
Confirm Email
Zip Code
The question is how do I get the input from the form to me in the email AND how do I add a the field labe in that email.
Here's an example of what I'm hoping to get in the body of the email that's sent to me:
First Name: John
Last Name: Smith
Email: whatever@gmail.com
Zip: 17604
Default.aspx and default.aspx.vb ----Follow:
Default.aspx----------
<p> First Name:<br /> <asp:TextBox ID="txtFirstName" runat="server" Columns="35" /> </p> <p> Last Name:<br /> <asp:TextBox ID="txtLastName" runat="server" Columns="35" /> </p> Email Address<br /> <asp:TextBox ID="txtToAddress" runat="server" Columns="35" /> <asp:RegularExpressionValidator runat="server" ErrorMessage="Please Enter a Valid Email Address" id="RegularExpressionValidator1" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtToAddress"> </asp:RegularExpressionValidator> <p> Confirm Email Address<br /> <asp:TextBox runat="server" id="txtConfirm"></asp:TextBox> <asp:CompareValidator runat="server" ErrorMessage="Addresses Must Match" id="CompareValidator1" ControlToCompare="txtConfirm" ControlToValidate="txtToAddress"> </asp:CompareValidator> </p> <p> Your Current Zip Code:<br /> <asp:TextBox ID="txtzip" runat="server" Columns="50" /></p> <p> Comments:<br /> <asp:TextBox ID="txtBody" runat="server" Columns="75" TextMode="MultiLine" Rows="6" Width="411px" /></p> <p> <asp:Button ID="btnSend" runat="server" Text="Send Mail" /></p> </div> </form> </body> </html>DEFAULT.ASPX.VB
Partial Class _Default Inherits System.Web.UI.Page ''' <summary> ''' Actions when the Send button is clicked. ''' </summary> Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click 'Create instance of main mail message class. Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage() 'Configure mail mesage 'Set the From address with user input ' mailMessage.From = New System.Net.Mail.MailAddress(txtFromAddress.Text.Trim()) 'Get From address in web.config mailMessage.From = New System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings("fromEmailAddress")) 'Set additinal addresses mailMessage.To.Add(New System.Net.Mail.MailAddress(txtToAddress.Text.Trim())) 'Set additional options mailMessage.Priority = Net.Mail.MailPriority.High 'Text/HTML mailMessage.IsBodyHtml = False 'Set the subject and body text mailMessage.Subject = "Form Request" "FirstName:" mailMessage.Body = txtFirstName.Text.Trim() mailMessage.Body = txtLastName.Text.Trim() mailMessage.Body = txtToAddress.Text.Trim() mailMessage.Body = txtzip.Text.Trim() mailMessage.Body = txtBody.Text.Trim() 'Create an instance of the SmtpClient class for sending the email Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient() 'Use a Try/Catch block to trap sending errors 'Especially useful when looping through multiple sends Try smtpClient.Send(mailMessage) Catch smtpExc As System.Net.Mail.SmtpException 'Log error information on which email failed. Catch ex As Exception 'Log general errors End TryUstesG
Contributor
2098 Points
449 Posts
Re: Pull input data from asp.net form
Dec 05, 2012 09:19 PM|LINK
But don't expect me to do your job!
PensacolaLew
Member
1 Points
10 Posts
Re: Pull input data from asp.net form
Dec 05, 2012 10:07 PM|LINK
Thank you UstesG...I'm almost there.
The body of that email produced (exactly as written):
JohnSmithsanta@gmail.com32533this is a test
How do I get:
First Name: John
Last Name: Smith
Email Address: santa@gmail.com
Zip: 32533
Comments: This is a test
UstesG
Contributor
2098 Points
449 Posts
Re: Pull input data from asp.net form
Dec 05, 2012 10:09 PM|LINK
add & vbCrLf at the end of all the body append statements.
But don't expect me to do your job!
PensacolaLew
Member
1 Points
10 Posts
Re: Pull input data from asp.net form
Dec 05, 2012 10:26 PM|LINK
I get the following now. Close enough for now buddy...Thank you very much.
john
smith
santa@gmail.com
32533
This is test two
UstesG
Contributor
2098 Points
449 Posts
Re: Pull input data from asp.net form
Dec 05, 2012 10:30 PM|LINK
try this:
mailMessage.Body = String.Format("First Name: {0}" & vbCrLf, txtFirstName.Text.Trim()) mailMessage.Body = String.Format("LastName: {0}" & vbCrLf, txtLastName.Text.Trim()) mailMessage.Body = String.Format("Address: {0}" & vbCrLf, txtToAddress.Text.Trim()) mailMessage.Body = String.Format("Zip: {0}" & vbCrLf, txtzip.Text.Trim()) mailMessage.Body = String.Format("Message: {0}" & vbCrLf, txtBody.Text.Trim())But don't expect me to do your job!
PensacolaLew
Member
1 Points
10 Posts
Re: Pull input data from asp.net form
Dec 05, 2012 10:56 PM|LINK
Thank you...BTW, do you know of a good web reference for this type back-end coding.
There is other stuff I'd like to do and modify. I'm not a big fan of asking for help until I've exhausted all my resources. Believe it or not I've been working on this all day and after your post, I had the solution in no time (mucho appreciated).
Again, I'm not a coder by trade, but would certainly like to learn enough to keep myself out of trouble.
Thanks/Lew
UstesG
Contributor
2098 Points
449 Posts
Re: Pull input data from asp.net form
Dec 06, 2012 09:57 PM|LINK
No prob...start here:
http://www.asp.net/web-pages/tutorials
shoot me a message if you ever need help!
Also, can you mark As Answer please.
But don't expect me to do your job!