Sending Email With ASP.Net
Sending email with ASP.Net is an easy task thanks to the classes in System.Net.Mail namespace So, Open your Visual Studio and let's send our first email!
In your ASP.Net page, add some controls and lay them out like the following:
The layout is really easy to understand, give your controls meaningful IDs. For the last TextBox, you need the set the TextMode property to MultiLine for the email body text. Of course in real life applications you will need to add some ASP.Net Validation Controls but we will keep it simple in this tutorial.
Double click on the Send button to write the btnSend_Click code, but first we need to use the System.Net.Mail namespace so type using System.Net.Mail; at the top of your page.In the the btnSend_Click type this simple code:
MailMessage email = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text); SmtpClient smtp = new SmtpClient(); //Replace "MyUserName", "MyPassword" with your real username and Password System.Net.NetworkCredential cred = new System.Net.NetworkCredential("MyUserName", "MyPassword"); //Replace "MySMTPServer" with your real SMTP server smtp.Host = "MySMTPServer"; //Replace 26 with your SMTP port smtp.Port = 26; smtp.Credentials = cred; smtp.Send(email);
Those simple lines are all you need to send an email using ASP.Net! Now let's explain the code line by line.
First, we need to declare a new instance of the MailMessage class, this will hold the From email, the To email, the Subject and the Body.
To be able to send email, you need to have SMTP server (i.e. Simple Mail Transfer Protocol Server) which will send the email for you. You can use the SMTP server provided to you by your host. Most SMTP servers come in the form of mail.servername.com (e.g. mail.gmail.com). The SMTPClient class is the class that will hold the SMTP server data and send the email.
To be able to login to your SMTP server, you need at least a username and password, this is called authentication data or credentials, so we create a System.Net.NetworkCredential object and provide it with the username and password of the SMTP server. Notice that for most of the shared hosting environments, the username is the email that you create using your CPanel or whatever software your hosting provider uses for creating new emai accounts.The other lines are easy to understand, just make sure to type the SMTP port correctly because many hosting providers change the default port (25) because many firewalls and proxies block it due to spam.
- How to Adjust Audio/Video Sync In An AVI File Using VirtualDub
Rating: 3.7/5 Votes: 173
- Dynamic Mask in Actionscript 3
Rating: 3.5/5 Votes: 11
- Fit Text in a Shape
Rating: 3.5/5 Votes: 113
- Show the Url of the Page
Rating: 3.5/5 Votes: 34
- Add Your Logo To A Video File
Rating: 3.5/5 Votes: 69