Happy New Year

Send Email Using ASP.Net With C#

Sending email is a very common task in any web application for many purposes. We can send OTP as an email to email addresses to confirm users account in case of login functionality in asp.net project. Therefore in this article, I will be explaining how an email can be send using ASP.NET with C#. I will be working on asp.net & MVC web application.I will be demonstrating how to use asp.net to build web application to send an email.

CODE

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


string to = "toaddress@gmail.com";
string from = "fromaddress@gmail.com";
MailMessage message = new MailMessage(from, to);
string mailbody = "In this article you will learn how to send email using asp.net with C#";
message.Subject = "Send email using asp.net with C#";
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential basicCredential1 = new
System.Net.NetworkCredential("yourmail id", "Password");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = basicCredential1;
try
{
 client.Send(message);
}
catch (Exception ex)
{
 throw ex;
}

Simple Mail Transfer Protocol (SMTP)

Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used in sending and receiving e-mail. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another. The messages can then be retrieved with an e-mail client using either POP or IMAP.

The following is a list of SMTP Server and Port Numbers:

No# Mail Server SMTP Server(Host) Port Number
1 Gmail smtp.gmail.com 587
2 Outlook smtp.live.com 587
3 Yahoo Mail smtp.mail.yahoo.com 465
4 Yahoo Mail Plus plus.smtp.mail.yahoo.com 465
5 Hotmail smtp.live.com 465
6 Office365.com smtp.office365.com 587
7 zoho Mail smtp.zoho.com 465