- Active internet connection.
- Email id of any provider such as Gmail, Yahoo or your organization to send emails.
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty web site" (to avoid adding a master page).
- Provide the project a name, such as "Sending Emails" or another as you wish and specify the location.
- Then right-click on Solution Explorer and seelct "Add New Item" - "Default.aspx" page and one class file.
- Drag and drop three Text Boxes and two buttons.
To send Emails in ASP.Net, the following namespaces must be added:
To learn about the two namespaces above refer to the MSDN.
Now, move to the web.config file and add the email credentials in the app setting section so in the future, if your email credentials are changed then their is no need to change them in your code file since you can simply the change values in your app settings of the web.config file. After adding the credentials, the app setting section of the web.config file looks as in the following:
In the preceding app settings section, we are adding the sender email details credentials such as email id and word, so we can send the emails and later on we will read these values in the .cs file.
In the preceding app Settings section add your valid email and word so emails can be sent from your email id to others.
Now add the separate class file by right-clicking on the Solution Explorer, however this is not necessary since you can also write this code in the default.aspx.cs file but for flexibility we are adding it in a separate code file.
After writing the email sending logic code, the class file will look
as in the following:
In the preceding class file, we have created one simple static function to send the emails that takes three parameters that are:
string ToEmail: recipient email id
string Subj: subject of the email
string Message: Email body or message
Now call the preceding static function on the button click of the "default.aspx.cs" page
as in the following:
I hope you have done all the preceding steps, now run the application and add the email details in all the text boxes and click on the send button as:
From the preceding example we learned how to send emails, I hope you have done it.
- Don't forget to update the "Web.config" file for your email credential details.
We learned how to send emails in ASP.Net. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.