Thursday, March 29, 2012

MailTo question (How to send an email to an address from a textbox field)

First, thx for any assistance anyone can give me, and sry for the amateur question, I am learning asp.net 2.0, vb, and sql 2000 at the same time while doing my projects, so I am trying to draw on every post, and book that I can find, and it definitly feels like I am over my head at times. Ill post the code I am using below, but the basic summary of what I do is following:

1)The user enters the following information into text fields: (email, company, lastname, firstname, phone).

2)this information is collected, and first sent to another person, then a password is generated and (email, company, lastname, firstname, password) is entered into a database.

3)Then the password is emailed to the textfield(email) variable. And that is where the current problem is, I cannot seem to get the MailTo to accept a variable, it wants a static line of text as an address.

<%@dotnet.itags.org. Page Explicit="true" debug="true" language="vb" %>
<%@dotnet.itags.org. Import Namespace="System.Data" %>
<%@dotnet.itags.org. Import Namespace="System.Data.SqlClient" %>
<%@dotnet.itags.org. Import Namespace="System.Data.Mail" %>
<%@dotnet.itags.org. Import Namespace="System.Net.Mail" %>

<script runat="server">

Sub submitClick(Source as Object, E as EventArgs)

Dim eMsg as String

eMsg +="Customer Information" & vbcrlf
eMsg +="Email: " & txtEmail.Text & vbcrlf
eMsg +="Company: " & txtCompany.Text & vbcrlf
eMsg +="Last Name: " & txtLastName.Text & vbcrlf
eMsg +="First Name: " & txtFirstName.Text & vbcrlf
eMsg +="Phone Number: " & txtPhoneNumber.Text & vbcrlf

Dim MailClient = New SmtpClient("11.111.111.11")
Dim MailFrom As String =kittens@dotnet.itags.org.kittens.com
Dim MailTo As String =puppies@dotnet.itags.org.kittens.com
Dim MailSubject As String = "New Customer Information"
Dim MailBody As String = eMsg

If (Page.IsValid = True)

MailClient.Send(MailFrom, MailTo, MailSubject, MailBody)


Dim guidResult as String = System.Guid.NewGuid().ToString()
Dim SQLConn As SqlConnection = New SqlConnection()
SQLConn.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
SQLConn.Open()
Dim cmd as new SqlCommand("INSERT INTO custinfo(email,company,lastname,firstname,phone,password) VALUES (@dotnet.itags.org.email,@dotnet.itags.org.company,@dotnet.itags.org.lastname,@dotnet.itags.org.firstname,@dotnet.itags.org.phone,@dotnet.itags.org.password)",SQLConn)

cmd.Parameters.Add("@dotnet.itags.org.email",sqldbtype.Varchar).Value=txtEmail.Text
cmd.Parameters.Add("@dotnet.itags.org.company",sqldbtype.Varchar).Value=txtCompany.Text
cmd.Parameters.Add("@dotnet.itags.org.lastname",sqldbtype.Varchar).Value=txtLastName.Text
cmd.Parameters.Add("@dotnet.itags.org.firstname",sqldbtype.Varchar).Value=txtFirstName.Text
cmd.Parameters.Add("@dotnet.itags.org.phone",sqldbtype.Varchar).Value=txtPhoneNumber.Text
cmd.Parameters.Add("@dotnet.itags.org.password",sqldbtype.Varchar).Value=guidResult

cmd.ExecuteNonQuery

Dim ePwd as String

ePwd +="The following is your password to enter the customer login. Make sure you keep this email in a secure place. If you lose this password please contact purple@dotnet.itags.org.monkey.com for assistance." & vbcrlf
ePwd +="Password: " & guidResult

MailClient = New SmtpClient("11.111.111.11")
MailFrom = "kittens@dotnet.itags.org.kittens.com"
MailTo = txtEmail.Text ' This is problem spot! '
MailSubject = "Your password to Alebra Customer Login"
MailBody = ePwd

MailClient.Send(MailFrom, MailTo, MailSubject, MailBody)

SQLConn.Close

End If
End Sub

</script>

Any help is much appreciated. I am using ASP2.0, VB, Windows Server 2003, and SQL 2000 as assets. Any more info needed give a shout.

Hi,

I compiled your code and it gave no errors. Can you let me know what error message you get? Does it occur at runtime only?

-Vivek


Well I'm not exactly sure how, but the code works now :b

I had tore apart the code a couple of times to try different methods and when I put it back together it seems to work now! I apologize for wasting your time testing code that did work, at the time I was getting the error that required me to finish the statement, basically the error that wants var = "something", and that was where my problem began, still not really sure how this got fixed unless there was just something in my system cache that it never refreshed.

Thx for the assistance and I apologize again for wasting your time

Now I'm off to the next Problem!

0 comments:

Post a Comment