Sending E-mails from your applications using Gmail
This article provides steps how to send emails from your applications running on websites.
ASP.NET / .NET 8 / .NET 9 Free hosting
If you don't already have our ASP.NET / .NET Free hosting, sign up for FREE at https://MonsterASP.net/.
Important: Outgoing SMTP for sending emails from hosted applications is only available for Premium plans.
1. Configuring Gmail SMTP in your hosted applications
If you need sending e-mails from applications running on your website, you can use Gmail as SMTP server. Here is basic setup:
SMTP Server: smtp.gmail.com
SMTP Port: 587
Username: your-mail-address@gmail.com
Password: your-app-password
EnableSsl: true
Don't use your regular Gmail password! Instead, create App Password in your Google account!
2. Create Gmail App Password
Don't use your regular password for your Gmail account. Instead, create App Password in your Google account that's designed for this solution and is more secure.
There you can create your Gmail App Password:
https://myaccount.google.com/apppasswords
3. Example .NET script
This is simple example script for sending emails from your hosted application using Gmail SMTP server.
// Gmail SMTP server settings
var smtpServer = "smtp.gmail.com";
var smtpPort = 587;
var enableSsl = true;
// Gmail login credentials
var gmailUsername = "your-mail-address@gmail.com";
var gmailPassword = "your-app-password"; // Use app-specific password, not your regular password
// Email message settings
var fromEmail = "your-mail-address@gmail.com"; // Must match Gmail username
var toEmail = "recipient@example.com";
var subject = "Test Email via Gmail SMTP";
var body = "This is a test email sent using Gmail SMTP and .NET.";
// Create the email message
MailMessage mail = new MailMessage(fromEmail, toEmail, subject, body);
// Set up the SMTP client
SmtpClient smtp = new SmtpClient(smtpServer, smtpPort);
smtp.EnableSsl = enableSsl;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(gmailUsername, gmailPassword);
try
{
smtp.Send(mail);
Response.Write("Email sent successfully.");
}
catch (Exception ex)
{
Response.Write("Error sending email: " + ex.Message);
}
4. Securing Contact Form Against Bots and Abuse
Important: Never launch your contact form without protection!
If you're building contact form, always make sure to secure it against spam and abuse. Without protection, bots (automated scripts) can fill out your form thousands of times, sending spam, wasting server resources or even causing real problems.
To stop this, use tools like:
Google reCAPTCHA – Easy to add and blocks most spam bots.
Cloudflare Turnstile – A privacy-friendly and free alternative to CAPTCHA.
or other similar anti-bot solutions.