Configuring <mailSettings> in web.config to Send Emails
ASP.NET applications can send emails (such as enquiry form submissions) using the
<mailSettings> section of web.config.
This allows you to define SMTP server details directly in your configuration file.
Sample Configuration
Insert the following into your web.config file:
<configuration>
<system.net>
<mailSettings>
<smtp from="email@yourdomain.com">
<network host="mail.yourdomain.com"
port="25"
userName="email@yourdomain.com"
password="youremailpassword" />
</smtp>
</mailSettings>
</system.net>
</configuration>
Replace the blue placeholders with your actual domain and email account details:
- from: The email address that will appear as the sender.
- host: Your mail server (e.g.,
mail.yourdomain.com). - port: Typically
25, but may be587or465depending on your provider. - userName: Your full email address.
- password: The password for your email account.
Sending an Email in ASP.NET
Once configured, you can send emails using the SmtpClient class in your ASP.NET code:
using System.Net.Mail;
MailMessage mail = new MailMessage();
mail.To.Add("recipient@domain.com");
mail.From = new MailAddress("email@yourdomain.com");
mail.Subject = "Website Enquiry";
mail.Body = "This is a test enquiry form email.";
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
The SmtpClient will automatically use the settings defined in web.config.
Best Practices
- Use authenticated SMTP (username + password) for reliable delivery.
- Check with your hosting provider if SSL/TLS is required (ports 587 or 465).
- Keep your web.config secure — do not expose passwords in public repositories.
- Test your configuration with a simple enquiry form before deploying live.
✔ Tip: If emails are not sending, verify your SMTP host, port, and authentication details.
Also check firewall rules to ensure outbound SMTP traffic is allowed.