Register Now

Login


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

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);

Comments ( 3 )

  1. Default Ports:Server:Authentication:Port:SMTP Server (Outgoing Messages)Non-EncryptedAUTH25 (or 587) Secure (TLS)StartTLS587 Secure (SSL)SSL465POP3 Server (Incoming Messages)Non-EncryptedAUTH110 Secure (SSL)SSL995

    Googlemail – GmailServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.gmail.comSSL465
    We can send mail from other mail servers …

    Outlook.comServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.live.comStartTLS587POP3 Server (Incoming Messages)pop3.live.comSSL995

    Office365.comServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.office365.comStartTLS587POP3 Server (Incoming Messages)outlook.office365.comSSL995 Note: If the above settings are not working for your account, then login to the outlook web app, go to the “Settings” > “Options” > “Account” > “My Account” > “Settings for POP and IMAP Access”.

    Yahoo MailServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.mail.yahoo.comSSL465POP3 Server (Incoming Messages)pop.mail.yahoo.comSSL995

    Yahoo Mail PlusServer:Authentication:Port:SMTP Server (Outgoing Messages)plus.smtp.mail.yahoo.comSSL465POP3 Server (Incoming Messages)plus.pop.mail.yahoo.comSSL995

    Yahoo UKServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.mail.yahoo.co.ukSSL465POP3 Server (Incoming Messages)pop.mail.yahoo.co.ukSSL995

    Yahoo DeutschlandServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.mail.yahoo.comSSL465POP3 Server (Incoming Messages)pop.mail.yahoo.comSSL995

    Yahoo AU/NZServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.mail.yahoo.com.auSSL465POP3 Server (Incoming Messages)pop.mail.yahoo.com.auSSL995

    O2Server:Authentication:Port:SMTP Server (Outgoing Messages)smtp.o2.ie 25POP3 Server (Incoming Messages)pop3.o2.ie 110

    O2.ukServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.o2.co.uk 25POP3 Server (Incoming Messages)pop3.o2.co.uk 110

    AT&TServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.att.yahoo.comSSL465POP3 Server (Incoming Messages)pop.att.yahoo.comSSL995

    NTL @ntlworld.comServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.ntlworld.comSSL465POP3 Server (Incoming Messages)pop.ntlworld.comSSL995

    BT ConnectServer:Authentication:Port:SMTP Server (Outgoing Messages)pop3.btconnect.com 25POP3 Server (Incoming Messages)mail.btconnect.com 110

    BT OpenworldServer:Authentication:Port:SMTP Server (Outgoing Messages)mail.btopenworld.com 25POP3 Server (Incoming Messages)mail.btopenworld.com 110

    BT InternetServer:Authentication:Port:SMTP Server (Outgoing Messages)mail.btinternet.com 25POP3 Server (Incoming Messages)mail.btinternet.com 110

    OrangeServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.orange.net 25POP3 Server (Incoming Messages)pop.orange.net 110

    Orange.ukServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.orange.co.uk 25POP3 Server (Incoming Messages)pop.orange.co.uk 110

    Wanadoo UKServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.wanadoo.co.uk 25POP3 Server (Incoming Messages)pop.wanadoo.co.uk 110

    HotmailServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.live.comSSL465POP3 Server (Incoming Messages)pop3.live.comSSL995

    O2 Online DeutschlandServer:Authentication:Port:SMTP Server (Outgoing Messages)mail.o2online.de 25POP3 Server (Incoming Messages)pop.o2online.de 110

    T-Online DeutschlandServer:Authentication:Port:SMTP Server (Outgoing Messages)securesmtp.t-online.deStartTLS587POP3 Server (Incoming Messages)securepop.t-online.deSSL995

    1&1 (1and1)Server:Authentication:Port:SMTP Server (Outgoing Messages)smtp.1and1.comStartTLS587POP3 Server (Incoming Messages)pop.1and1.comSSL995

    1&1 DeutschlandServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.1und1.deStartTLS587POP3 Server (Incoming Messages)pop.1und1.deSSL995

    ComcastServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.comcast.net 587POP3 Server (Incoming Messages)mail.comcast.net 110

    VerizonServer:Authentication:Port:SMTP Server (Outgoing Messages)outgoing.verizon.netSSL465POP3 Server (Incoming Messages)incoming.verizon.netSSL995

    Verizon (Yahoo hosted)Server:Authentication:Port:SMTP Server (Outgoing Messages)outgoing.yahoo.verizon.net 587POP3 Server (Incoming Messages)incoming.yahoo.verizon.net 110

    zoho MailServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.zoho.comSSL465IMAP Server (Incoming Messages)pop.zoho.comSSL995

    Mail.comServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.mail.comSSL465IMAP Server (Incoming Messages)pop.mail.comSSL995

    GMX.comServer:Authentication:Port:SMTP Server (Outgoing Messages)smtp.gmx.comSSL465IMAP Server (Incoming Messages)pop.gmx.comSSL995

Leave a reply