Saturday, March 24, 2012

The Dumbest Question from a REAL dummy

i have struggled for six months to learn asp.net. I am completely frustrated at this point because i can not find a single resource (book, web, forum or otherwise) that explains the most common applications for idiots like me. I STILL have no clue how to design a simple email form in asp.net 2.0. So here I am asking yet another stupid question. Note: I have searched throughout the forums here and visited all the recommended web sites and have failed to find a single one that actually shows a complete example.

Can some kind, compassionate soul PLEASE enlighten me and provide a real world example of an email form in asp.net 2.0 VB that has the following simple properties:

name input
email input
drop down 1
drop down 2
textarea
submit button

to/cc/bcc fields

uses smtp

and when you click the submit button and the email is sent the form panel hides and a new panel appears in its place saying the message has been sent?

PLEASE!

ty

-The Idiot

You may check this great article by Bipin Joshi here:

Everything about sending Emails in ASP.NET 2.0- Part 1

Hope this helps,

Regards


Don't be too hard on yourself we learn everyday.
Below is the simple that is used to send emails in ASP.NET 2.0. Replace the localhost with your email server and it should work. If it does not work let us know and the members of this forum will help you.

MailMessage mail =newMailMessage();

string emailFrom = "someone@.somewhere.com";

string hostAddress = "localhost";

string body ="This is the message body";

mail.From =newMailAddress(emailFrom);

mail.To.Add(email);

//set the content

mail.Subject ="This is a test email.";

mail.Body = body;

mail.IsBodyHtml =true;

//send the message

SmtpClient smtp =newSmtpClient(hostAddress);

smtp.Send(mail);


jf1 wrote:

i have struggled for six months to learn asp.net. I am completely frustrated at this point because i can not find a single resource (book, web, forum or otherwise) that explains the most common applications for idiots like me. I STILL have no clue how to design a simple email form in asp.net 2.0. So here I am asking yet another stupid question. Note: I have searched throughout the forums here and visited all the recommended web sites and have failed to find a single one that actually shows a complete example.

Can some kind, compassionate soul PLEASE enlighten me and provide a real world example of an email form in asp.net 2.0 VB that has the following simple properties:

name input
email input
drop down 1
drop down 2
textarea
submit button

to/cc/bcc fields

uses smtp

and when you click the submit button and the email is sent the form panel hides and a new panel appears in its place saying the message has been sent?

PLEASE!

ty

-The Idiot

6 months to figure out how to send a simple email? hopefully you get paid by the hour :)

joking aside, if you have indeed "visited the recommended site", then surely you had to have seen a link tohttp://www.systemnetmail.com, which is recommended over and over and over and over and over again (and rightfully so), and if you missed that link, there is no way that "have failed to find a single one that actually shows a complete example" is true

because that site is loaded with pages like this:
http://www.systemnetmail.com/faq/3.1.1.aspx

which give you the basis to plug in data from your form

so instead of

mail.Subject = "This is an email";

you'd have:

mail.Subject = TextBox1.Text;
or
mail.Subject = DropDownList1.SelectedValue


jf1 wrote:

i have struggled for six months to learn asp.net. I am completely frustrated at this point because i can not find a single resource (book, web, forum or otherwise) that explains the most common applications


I'm not sure what books you have, but you should strongly consider ASP.NET Unleashed by Stephen Walther. This is the most recommended book on ASP.NET. The ASP.NET 2.0 version is coming out next month, and if it's as any good as the original, it's a must-have.

I've thrown together a simple working example of everything you were hoping to see demonstrated, which could use some bells and whistles (such as validation, and some styling), but I thought that keeping it to a minimum would help you see the basics.

This is the form,EmailExample.aspx:

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="EmailExample.aspx.vb" Inherits="EmailExample" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Simple Email Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="FirstPanel" runat="server" Visible="true">
Name: <asp:TextBox ID="Name" runat="server" /><br />
Email: <asp:TextBox ID="Email" runat="server" /><br />
Dropdown1:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>List1 Item1</asp:ListItem>
<asp:ListItem>List1 Item2</asp:ListItem>
<asp:ListItem>List1 Item3</asp:ListItem>
</asp:DropDownList><br />
Dropdown2:
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>List2 Item1</asp:ListItem>
<asp:ListItem>List2 Item2</asp:ListItem>
<asp:ListItem>List2 Item3</asp:ListItem>
</asp:DropDownList><br />
TextArea: <asp:TextBox ID="TextArea" TextMode="MultiLine" runat="server" /><br />
<asp:Button UseSubmitBehavior="true" ID="Button1" Text="Submit" runat="server" OnClick="Button_Click" />
</asp:Panel>

<asp:Panel ID="SecondPanel" runat="server" Visible="false">
<asp:Label ID="EmailResult" runat="server" />
</asp:Panel>
</div>
</form>
</body>
</html>

And this is the code file,EmailExample.aspx.vb:
Imports System.Net.Mail
Imports System.Net

Partial Class EmailExample
Inherits System.Web.UI.Page

Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim myEmail As MailMessage = New MailMessage

Dim mailClient As New SmtpClient("mail.mydomain.com")
mailClient.Credentials = New System.Net.NetworkCredential("terri@.mydomain.com", "mysecretpassword")
Dim EmailMessage As New StringBuilder

myEmail.To.Add("terri@.mydomain.com")
myEmail.Bcc.Add("someone@.mydomain.com")
myEmail.CC.Add("someone2@.mydomain.com")
myEmail.From = New MailAddress(Email.Text, Name.Text)
myEmail.Subject = "Test email"
EmailMessage.AppendLine("This is my test message.")
EmailMessage.Append("The value of dropdown1 is: ")
EmailMessage.AppendLine(DropDownList1.SelectedValue)
EmailMessage.Append("The value of dropdown2 is: ")
EmailMessage.AppendLine(DropDownList2.SelectedValue)
EmailMessage.Append("The value of textarea is: ")
EmailMessage.AppendLine(TextArea.Text)

myEmail.Body = EmailMessage.ToString

Try
mailClient.Send(myEmail)
EmailResult.Text = "Your email was sent"
Catch MailException As SmtpException
EmailResult.Text = "Your email was not sent." & MailException.ToString()
Finally
SecondPanel.Visible = True
FirstPanel.Visible = False
End Try
End Sub
End Class

I should tell you that I was only able to get the CC and Bcc to work if the recipients were also at mydomain.com. I believe this is due to the way the smtp server is set up at my host, but I am honestly not 100% sure there.

I hope this helps you at least a little.

Have you checked outASPNET101.com?

http://aspnet101.com/aspnet101/tutorials.aspx?id=59


Thanks everyone - especially Terri. Terri, yes I have Mr. Walther's book and while I agree it is pretty good, it's all based on ASP.NET 1.1 - which in many ways is completely worthless to me now on my ASP.NET 2.0 apps. At least, as far as I can see, I can't use some 1.1 namespaces (such as system.web.mail) in 2.0 apps...? Unless you know something I don't (which is very likely :)

Thank you for the code! Someone needs to make this a sticky in this forum!
OK, tried to bring the code in. here are the list of errors VS 2005 is displaying:

Error 2 'Title' is not a member of 'ASP.company_aspx'. C:\Inetpub\activelife\company.aspx 1
Error 3 'InitializeCulture' is not a member of 'ASP.company_aspx'. C:\Inetpub\activelife\company.aspx 1
Error 4 'MasterPageFile' is not a member of 'ASP.company_aspx'. C:\Inetpub\activelife\company.aspx 1
Error 5 'CreateResourceBasedLiteralControl' is not a member of 'ASP.company_aspx'. C:\Inetpub\activelife\company.aspx 1
Error 6 'Button_Click' is not a member of 'ASP.company_aspx'. C:\Inetpub\activelife\company.aspx 46
Error 7 'Context' is not a member of 'company'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 8 'Context' is not a member of 'company'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 9 Class 'company_aspx' must implement 'ReadOnly Property IsReusable() As Boolean' for interface 'System.Web.IHttpHandler'. Implementing property must have matching 'ReadOnly' or 'WriteOnly' specifiers. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 10 Class 'company_aspx' must implement 'Sub ProcessRequest(context As HttpContext)' for interface 'System.Web.IHttpHandler'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 11 'ReadStringResource' is not a member of 'ASP.company_aspx'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 12 'GetWrappedFileDependencies' is not a member of 'ASP.company_aspx'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 13 property 'SupportAutoEvents' cannot be declared 'Overrides' because it does not override a property in a base class. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 14 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 15 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 16 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 17 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 18 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 19 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 20 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 21 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 22 Value of type 'ASP.company_aspx' cannot be converted to 'System.Web.UI.Page'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 23 function 'GetTypeHashCode' cannot be declared 'Overrides' because it does not override a function in a base class. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 24 sub 'ProcessRequest' cannot be declared 'Overrides' because it does not override a sub in a base class. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 25 'ProcessRequest' is not a member of 'company'. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\activelife\8d4d820d\fb1308b8\App_Web_company.aspx.cdcab7d2.-3amqrui.0.vb
Error 26 'Context' is not a member of 'company'. C:\Inetpub\activelife\company.aspx 1 1 C:\Inetpub\activelife\
Error 27 'Context' is not a member of 'company'. C:\Inetpub\activelife\company.aspx 1 1 C:\Inetpub\activelife\
Error 28 Name 'Email' is not declared. C:\Inetpub\activelife\company.aspx.vb 17 40 C:\Inetpub\activelife\
Error 29 'Name' is not declared. File I/O functionality is available in the 'Microsoft.VisualBasic' namespace. C:\Inetpub\activelife\company.aspx.vb 17 52 C:\Inetpub\activelife\
Error 30 Name 'DropDownList1' is not declared. C:\Inetpub\activelife\company.aspx.vb 21 33 C:\Inetpub\activelife\
Error 31 Name 'DropDownList2' is not declared. C:\Inetpub\activelife\company.aspx.vb 23 33 C:\Inetpub\activelife\
Error 32 Name 'TextArea' is not declared. C:\Inetpub\activelife\company.aspx.vb 25 33 C:\Inetpub\activelife\
Error 33 Name 'EmailResult' is not declared. C:\Inetpub\activelife\company.aspx.vb 31 13 C:\Inetpub\activelife\
Error 34 Name 'EmailResult' is not declared. C:\Inetpub\activelife\company.aspx.vb 33 13 C:\Inetpub\activelife\
Error 35 Name 'SecondPanel' is not declared. C:\Inetpub\activelife\company.aspx.vb 35 13 C:\Inetpub\activelife\
Error 36 Name 'FirstPanel' is not declared. C:\Inetpub\activelife\company.aspx.vb 36 13 C:\Inetpub\activelife\
Yes I've read the asp101 stuff.. I'm not interested in ASP.NET 1.1 - which the article you referred to is based on. 90% of the asp.net sites are completely outdated now.

jf1 wrote:

yes I have Mr. Walther's book and while I agree it is pretty good, it's all based on ASP.NET 1.1 - which in many ways is completely worthless to me now on my ASP.NET 2.0 apps. At least, as far as I can see, I can't use some 1.1 namespaces (such as system.web.mail) in 2.0 apps...?

It's still an awesome book. Stephen Walther has a 2.0 version that should be out soon calledASP 2005 Unleashed. You can still use those .NET 1.1 namespaces, as they are there for backwords compatibility, but VS 2005 will complain about it informing you of the replacement. As far as email in .NET 2.0 goes,SystemNetMail.com is an awesome resource.

HTH,
Ryan


Ryan, you saved the day. I had no idea I could still use the old namespaces. I thought since VS 2005 was complaining that the app would not work. But it does. So thank you for stating this.

So... how can i now convert it to asp.net 2.0? Here is the code-behind

thank you!

Imports System.Web.Mail
Partial Class company
Inherits System.Web.UI.Page
Sub btnSendFeedback_Click(ByVal sender As Object, ByVal e As EventArgs)

'Create an instance of the MailMessage class
Dim objMM As New MailMessage()

'Set the properties - send the email to the person who filled out the
'feedback form.
objMM.To = "me@.gmail.com"
objMM.From = txtEmail.Text

'If you want to CC this email to someone else, uncomment the line below
objMM.Cc = "me@.somewebsite.com"

'If you want to BCC this email to someone else, uncomment the line below
'objMM.Bcc = "me@.gmail.com"

'Send the email in text format
objMM.BodyFormat = MailFormat.Text
'(to send HTML format, change MailFormat.Text to MailFormat.Html)

'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal

'Set the subject
objMM.Subject = "Web Site Email"

'Set the body
objMM.Body = "At " + DateTime.Now + " feedback was sent from the web page. Below you will find the feedback message " & _
"sent by " & txtName.Text & "." & vbCrLf & vbCrLf & _
"regarding: " & Regarding.SelectedItem.Text & "." & vbCrLf & vbCrLf & _
"referred by: " & Referral.SelectedItem.Text & "." & vbCrLf & vbCrLf & _
"------------" & vbCrLf & vbCrLf & _
txtMessage.Text & vbCrLf

'Set the body
'objMM.Body = "At " + DateTime.Now + " feedback was sent from the Active Life " & _
'"web page. Below you will find the feedback message " & _
'"sent by " & txtName.Text & "." & vbCrLf & vbCrLf & _
'"------------" & vbCrLf & vbCrLf & _
' txtMessage.Text & vbCrLf

'Specify to use the default Smtp Server
SmtpMail.SmtpServer = "smtp.website.com"

'Now, to send the message, use the Send method of the SmtpMail class
SmtpMail.Send(objMM)

panelSendEmail.Visible = False
panelMailSent.Visible = True
End Sub
End Class

The System.Net.Mail version is something like this:

Imports System.Net.Mail

Partial Class company
Inherits System.Web.UI.Page
Sub btnSendFeedback_Click(ByVal sender As Object, ByVal e As EventArgs)

'Create an instance of the MailMessage class
Dim objMM As New MailMessage()

'Set the properties - send the email to the person who filled out the
'feedback form.
objMM.To.Add(New MailAddress("me@.gmail.com"))
objMM.From = New MailAddress(txtEmail.Text)

'If you want to CC this email to someone else, uncomment the line below
objMM.CC.Add(New MailAddress("me@.somewebsite.com"))

'If you want to BCC this email to someone else, uncomment the line below
'objMM.Bcc = "me@.gmail.com"

'Send the email in text format
objMM.IsBodyHtml = Boolean.Parse(MailFormat.Text)
'(to send HTML format, change MailFormat.Text to MailFormat.Html)

'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal

'Set the subject
objMM.Subject = "Web Site Email"

'Set the body
objMM.Body = "At " + DateTime.Now + " feedback was sent from the web page. Below you will find the feedback message " & _
"sent by " & txtName.Text & "." & vbCrLf & vbCrLf & _
"regarding: " & Regarding.SelectedItem.Text & "." & vbCrLf & vbCrLf & _
"referred by: " & Referral.SelectedItem.Text & "." & vbCrLf & vbCrLf & _
"------------" & vbCrLf & vbCrLf & _
txtMessage.Text & vbCrLf

'Set the body
'objMM.Body = "At " + DateTime.Now + " feedback was sent from the Active Life " & _
'"web page. Below you will find the feedback message " & _
'"sent by " & txtName.Text & "." & vbCrLf & vbCrLf & _
'"------------" & vbCrLf & vbCrLf & _
' txtMessage.Text & vbCrLf


'Specify to use the default Smtp Server
Dim m_SmtpClient As New SmtpClient("smtp.website.com")

'Now, to send the message, use the Send method of the SmtpMail class
m_SmtpClient.Send(objMM)

panelSendEmail.Visible = False
panelMailSent.Visible = True
End Sub
End Class

0 comments:

Post a Comment