I am using SmtpClient to send emails from my Web apps since a long time. But only recently I realized that you can configure mail sending options (the SMTP server address, port number, from address, username, password, etc.) from the Web.config file instead of using constructor arguments and/or setting the SmtpClient properties. The Web.config approach allows administrators to change the configuration later without having to recompile.
To configure mail sending add to your Web.config file a <mailSettings> element under <system.net> under the root <configuration> element.
<configuration>
<system.net>
<mailSettings>
Here is the configuration I used to send emails from my Gmail account.
<mailSettings>
<smtpfrom="bassem.mf@gmail.com">
<network host="smtp.gmail.com" port="587"
userName="bassem.mf@gmail.com" password="•••••••"
enableSsl="true" />
</smtp>
</mailSettings>
SmtpClient’s default constructor initializes the new instance from the values specified in the mailSettings section
MailMessage mailMessage = new MailMessage()
{
Subject = "Sent by My Code",
Body = "This email was sent by my code.",
IsBodyHtml = false
};
mailMessage.To.Add(new MailAddress("bassem.mf@hotmail.com"));
SmtpClient smtpClient = new SmtpClient();
smtpClient.SendAsync(mailMessage, null);