Site icon Student Projects Live

Sending Mail in ASP.NET

Sending mail in ASP.NET is easy with the use of the MailMessage class. To send an email, you need to create an instance of the MailMessage class and provide the sender, recipient, subject, and email body as parameters. You can then use the SmtpClient class to connect to an SMTP server and send the message. You can also use an email API such as Mailtrap to send emails, which provides additional features such as actionable analytics and deliverability alerts.

Here we explain how to Sending Mail in ASP.NET. You can implement this code to your ASP.NET projects for mail features. This article will explain how to send mail using Gmail SMTP Server in ASP.Net.  You can download the source code of sending mail application and please add your gmail id and its password to send mail. ASP.NET is a web development framework that allows developers to build dynamic web applications. One common task in web development is sending email, and there are a few different options for sending mail in ASP.NET.


Sending Mail in ASP.NET

One option is to use the System.Net.Mail namespace, which provides classes for sending email. Here’s an example of how you might use this namespace to send a simple email using a Gmail account:

using System.Net;
using System.Net.Mail;

// Replace with your own email address and password
const string FROM_ADDRESS = "your@gmail.com";
const string FROM_PASSWORD = "yourpassword";

// Replace with the recipient's email address
const string TO_ADDRESS = "recipient@example.com";

// Set the subject and body of the email
const string SUBJECT = "Hello from ASP.NET";
const string BODY = "This is a test email sent from ASP.NET.";

// Create a new mail message object
var message = new MailMessage();

// Set the sender and recipient addresses
message.From = new MailAddress(FROM_ADDRESS);
message.To.Add(TO_ADDRESS);

// Set the subject and body of the message
message.Subject = SUBJECT;
message.Body = BODY;

// Set up the SMTP client and send the message
using (var client = new SmtpClient())
{
    // Use Gmail's SMTP server
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;

    // Set the credentials for the sender's email address
    client.Credentials = new NetworkCredential(FROM_ADDRESS, FROM_PASSWORD);

    // Send the email
    client.Send(message);
}

Another option is to use a third-party email service, such as SendGrid or Mailgun. These services provide APIs that can be used to send email from your ASP.NET application. For example, here’s how you might use the SendGrid API to send an email:

using System;
using System.Net;
using SendGrid;
using SendGrid.Helpers.Mail;

// Replace with your own SendGrid API key
const string API_KEY = "your_api_key";

// Replace with the recipient's email address
const string TO_ADDRESS = "recipient@example.com";

// Set the subject and body of the email
const string SUBJECT = "Hello from ASP.NET";
const string BODY = "This is a test email sent from ASP.NET.";

// Create a new mail message object
var message = new SendGridMessage();

// Set the sender and recipient addresses
message.SetFrom(new EmailAddress("your@gmail.com", "Your Name"));
message.AddTo(TO_ADDRESS);

// Set the subject and body of the message
message.SetSubject(SUBJECT);
message.AddContent(MimeType.Text, BODY);

// Send the email using the SendGrid API
var client = new SendGridClient(API_KEY);
var response = await client.SendEmailAsync(message);
Exit mobile version