Sending emails in asp.net using SMTP server settings from web config Part 78
C#, SQL Server, WCF, MVC and ASP .NET video tutorials for beginners
http://www.youtube.com/user/kudvenkat...
In the previous session, we discussed about sending emails using asp.net. All the SMTP server settings were configured in code. In this session, we will discuss about specifying these settings in web.config file.
Since the SMTP server settings are now configured in web.config. In code all you have to do is
1. Create an instance of the MailMessage class. Specify the FROM & TO email address, subject and Body
2. Create an instance of SmtpClient class, and send the email using Send() method. The SMTP settings will be automatically picked up from web.config file, when sending email.
SendEmail() method code
public static void SendEmail(string emailbody)
{ // Specify the from and to email address MailMessage mailMessage = new MailMessage ("from_email@gmail.com", "to_email@gmail.com"); // Specify the email body mailMessage.Body = emailbody; // Specify the email Subject mailMessage.Subject = "Exception"; // No need to specify the SMTP settings as these // are already specified in web.config SmtpClient smtpClient = new SmtpClient(); // Finall send the email message using Send() method smtpClient.Send(mailMessage);
}
Closed Caption:
hello welcome technologies I invented
this is part 78 of asp.net video series
in this session will discuss about
sending emails in asp.net using smtp
server settings from web.config file
before continuing with the session
i strongly recommend to watch part 75 76
and 77 of this video series in part 77
of this video series we discuss about
sending emails using asp.net all the
smtp server settings were actually
configured in core in this session we
will discuss about specifying these
settings in web.config file
let's quickly recap what we have seen in
the previous session now before
continuing with the session i strongly
recommend to watch the previous sessions
of this video series because this is a
continuation to that
now here i have a fun one . aspx on this
i have this review control and this went
from one loser appearing an instance of
the data set object and then working the
read xml method which lies to read the
contents of countries . xml and load the
data into the data set which in turn is
used as data source for the grid control
and finally be in work the data bind
method now we don't have this country's
to xml file within this project so
obviously we get a file not found
exception
and since that exception is not handle
anywhere on this page
it's going to get rolled up all the way
to the application level and at the
application level with in global.asax
file we have application underscore edit
event handler which relieves the
exception using silver don't get lost
era and then be passed that exception to
the log method of the lot of class and
this loc method of the law class will
actually log that exception to the
database table and to the event viewer
and then it also sends a notification
email to the development team or the
administrator in the previous session we
have seen how to send that email so if
you look at the log of class which is
present in this log of 5 longer . CS
so i have this log method which is a
static method is taking in the exception
object and what we're doing here we are
retrieving the exception type message
and stacked raised for all the
exceptions
you know for all the inner exceptions if
any
and then we are actually logging that
exception once we lock the exception
we are actually sending an email ok
and if you look at you know the send
email method
this is what is the send email matter
and we have discussed about this in
great detail in the previous session and
actually there are two simple steps to
send an email
first you compose your mail message
object and next you specify you are
smtpclient credentials that said so when
you're composing your mail message space
for your from address - address and then
the subject and body
so that's your mail message and then we
specify the smtpclient credentials so
smtpclient you specify the smtpclient
you know the smtp server name or IP
address here since we are using google
I mean gmail to send our emails i'm
going to use the googles smtp mail
server
smtp.gmail.com by the smtp stands for
simple mail transfer protocol and then
smt be gmail smtp server uses port
number 5 87 and then obviously we will
have to specify the username and
password which we did here using the
credentials property of the smtpclient
object
ok and then Gmail uses ssl secure socket
layer so that's why
look at that look at the URL it uses
https so we have to set enable ssl too
true
so all these smtp settings here are
specified in court now it's also
possible to specify these settings in
web.config file now you might be
thinking what is the advantage of
storing these settings in web.config
file or were called in fact the scent of
your question as well
the advantages tomorrow if the smtp
settings has to change then all you have
to change your web.config file that
rented here is that when you change web
. country plan
you don't have to recompile and redeploy
your application
obviously it's going to save a lot of
time and effort but if its specified in
the code you'll have to change your cord
once you change your code you'll have to
recompile and redeploy that
ok so that's why anytime you have some
settings within your application
it's always a preferred approach to have
those settings in some kind of a
configuration file could be a bloody
conflict
I could be an XML fire now remember when
web conflict you know changes the
application automatically restart
so it's even better if you store them in
some kind of an XML five
ok so that when that xml file changes
the the web application doesn't restart
ok but again it depends on on the
requirements of your project
but anyhow up the bottom line is that
storing the configuration settings in a
configuration file is much better than
storing them in the cord
ok and finally we send the email using
the same method now let's see how to
specify these smtpclient settings within
web.config fire so we look at the slide
here now i use system.net element here
now if you look at the mail message and
smtp class client class
both of these classes are actually
present in system.net too . mail and
namespace
ok so maybe if you look at the name
the element named here with in
web.config file its system.net and
within that we have mail settings and
within mail settings
smtp simple mail transfer protocol
delivery method is equal to network and
then we specify the network credentials
network the hostname port number
username password and since Gmail uses
ssl enable SSL is equal to true
so it's a simple as that so let's
quickly specify these settings within
web . country five
so let's go to web config file and
system.net you specify that directly
underneath the configuration elements or
system.net and here you specify mail
settings because we want to send an
email and within mail settings specify
smtp and delivery method is going to be
network
and we specify the network credentials
so network
first thing is the host name so host is
equal to smtp.gmail.com and port number
so gmail smtp server uses port number 5
87 and we need to specify username
so I'm going to specify that as my
username good when Kurt @ gmail.com and
you specify the password
ok so I'm going to specify that as tests
and since Gmail uses ssl i'm going to
specify enable SSL is equal to true
okay so since we have specified these
settings within the web.config file
I don't have to have these settings
anymore with in my code
so let's go to our code now with a lot
of class so i don't have to have these
credentials anymore so smtpclient
credentials i can get rid of that I can
also get rid of this enable ssl true and
i don't even have to specify they smtp
server name and the port number
ok so all we are doing here is
smtpclient you know we're creating an
instance of that and we are sending the
email message
ok so what is going to happen when we
actually run this
ok prepares this mail message object and
then it looks at this smtpclient all
right i don't have the setting specified
within cord so obviously it's going to
look at web.config file
okay all right these settings are
specified here it will read the settings
from here and then send us an email
you know to whomever we have specified
so here if you look at the log of class
we are actually asking it to send an
email from me to myself okay
and obviously you will you will find
that the email since i'm sending it to
myself you know this is an example email
that i have sent it to myself
so if you look at this i have sent from
this email address to this email address
ok now this quarter
you know you cannot send unlimited
number of emails from gmail
programmatically like this i think they
have set some limits there are only so
many number of emails that you can send
using gmail smtp so it's not available
for a full commercial purpose so you can
only send a specific number of emails
using gmail smtp server so since the
smtp server settings are now configured
in web.config fire and called all we
have to do is create an instance of the
mail message class especially the from
and to email address subject and body so
we prepare the arm in the mail message
object as usual but then when we create
an instance of the smtpclient and then
send the email using sand method that
said we don't have to specify the host
port number username and password and of
course since Gmail uses ssl we also have
to set enable ssl and all that is
specified within web.config part
obviously the advantage of that is that
whenever the smtp settings changes we
only have to change your web.config file
we don't have to change chords are big
and because of that we don't have to
become violently deploy our application
which is going to save a lot amount of
time and effort
we don't even have to test the
application because it's just going to
be the settings change on the slide you
can find resources place we boarded see
sharp and sequence ever interview
questions
that's it for today thank you for
listening have a great day
Video Length: 10:11
Uploaded By: kudvenkat
View Count: 58,686