· Step1:
Open Visual Studio, Create new Asp.net web Application, and name “Email Application”:
Now your
application is created. In solution Explorer, Right click on project and add
new item, Web Form.
Step2:
Now create GUI
of web form, as I created like this:
Drag and drop
labels, textboxes, buttons and validation controls from toolbox.
Then rename
them for ease of use in coding!
And create a
label for checking, either email is sending or not.
Step3:
Now open .cs
file of web form,
Add these
namespaces:
using System;
using System.Configuration;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text
Step4:
In .cs file add new method name sendEmail(), put all logic in it
and just call it on send button, click event.
protected
void sendEmail()
{
///creating new email message instance
///and getting SMTP settings from web.config
MailMessage
MyMailMessage = new MailMessage();
Configuration
configurationFile = WebConfigurationManager.OpenWebConfiguration("~\\Web.config");
MailSettingsSectionGroup
mailSettings =
configurationFile.GetSectionGroup("system.net/mailSettings") as
MailSettingsSectionGroup;
///getting SMTP settings from web.config
int port
= mailSettings.Smtp.Network.Port;
string
host = mailSettings.Smtp.Network.Host;
string
username = mailSettings.Smtp.Network.UserName;
string
password = mailSettings.Smtp.Network.Password;
bool
smtpSSL = true
string
toEmail = ToTextBox.Text.ToString();
string
toName = NameTextBox.Text.ToString();
///Set your default receptient Name
///Set your default receptient Name
///setting up SMTP server with alternate port [587]
///you can use default port [587]
SmtpClient
SMTPServer = new SmtpClient(host, port);
SMTPServer.EnableSsl
= smtpSSL;
///authanticating SMTP server
SMTPServer.Credentials
= new NetworkCredential(username, password);
///adding FROM email info
MyMailMessage.From
= new MailAddress(FromTextBox.Text.ToString());
///adding TO email info
MyMailMessage.To.Add(new
MailAddress(toEmail, toName));
///setting up email subject and message body
MyMailMessage.Subject
= SubTextBox.Text.ToString();
MyMailMessage.Body
= " Name :" + NameTextBox.Text.ToString() + Environment.NewLine +
" From :" + FromTextBox.Text.ToString()
+ Environment.NewLine+ BodyTextBox.Text.ToString() ;
MyMailMessage.IsBodyHtml
= true;
///trying to send email message
try
{
///Email message sent successfully!
SMTPServer.Send(MyMailMessage);
LabelEmail.Visible
= true;
LabelEmail.Text
= "Email sent";
LabelEmail.BackColor
= Color.PowderBlue;
//LabelEmail.ForeColor = Color.White;
}
catch
(SmtpException ex)
{
///there was an error sending email message
LabelEmail.Text
= ex.Message.ToString();
}
}
Step5:
In web.config file, after <system.net> tag, put this code:
In web.config file, after <system.net> tag, put this code:
<mailSettings>
<smtp from="gmail
account id">
<!--<network host="localhost"
password="" userName=""/>-->
<network defaultCredentials="true"
host="smtp.gmail.com"
port="587" userName="ur gmail
acount address" password="password of ur gmail id"
/>
</smtp>
</mailSettings>
Step6: Now run web form, Email is sending!
Now enter to and from email addresses and enjoying email sending!
After sending email u got text in label..