s

C# how to send email from console application using smtp from gmail zoho outlook edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 11 September 2020 | 4080

In C# .net use can use the SmtpClient in System.Net.Mail namespace to send mail using SMTP. I have written a console application to send mail using Gmail, outlook & Zoho. The sample code and details about settings are provided in this article. For sending mail from C# you use the SmtpClient Class from the System.Net.Mail namespace.

Gmail SMTP settings


Server Address: smtp.gmail.com
Username: Your Gmail Email Address (e.g. yourmail@gmail.com)
Password: Your Gmail Password
Port Number: 587 (With TLS)

Gmail C# code to send mail using SMTP

 MailMessage MailMessage = new MailMessage();
 MailMessage.From = new MailAddress("yourmail@gmail.com");
 MailMessage.To.Add("tomail@gmail.com");
 MailMessage.Subject = "SMTP test mail";
 MailMessage.Body = "Hello Mail Message";
 SmtpClient SmtpClient = new SmtpClient();
 SmtpClient.Host = "smtp.gmail.com";
 SmtpClient.EnableSsl = false;
 SmtpClient.Port = 587;
 SmtpClient.Credentials = new System.Net.NetworkCredential("yourmail@gmail.com", "yourpassword");
 try
 {
     SmtpClient.Send(MailMessage);
 }catch(Exception ex)
 {
     throw ex;
 }
       
                    

check this article "gmail SMTP mail The SMTP server requires a secure connection or the client was not authenticated" if you are finding errors while sending mail.

Zoho SMTP Settings


Server Address: smtp.zoho.in
Username: Your Zoho Email Address (e.g. yourmail@gmail.com)
Password: Your Zoho Password
Port Number: 587 (With TLS)

Code to send mail using Zoho

MailMessage MailMessage = new MailMessage();
MailMessage.From = new MailAddress("yourmailid@yourdomain.com");
MailMessage.To.Add("recepientmailid@recepientdomain.com");
MailMessage.Subject = "Test Mail from Zoho";
MailMessage.Body = "Hello world this is a test";
SmtpClient SmtpClient = new SmtpClient();
SmtpClient.Host = "smtp.zoho.in";
SmtpClient.EnableSsl = true;
SmtpClient.Port = 587;
SmtpClient.Credentials = new System.Net.NetworkCredential("frommailid@domainname.com", "yourpassword");
try
{
    SmtpClient.Send(MailMessage);
}catch(Exception ex)
{
    Console.WriteLine(ex.ToString());
    Console.ReadLine();
    throw ex;
}

            
                    

Outlook SMTP Settings


Host: smtp.office365.com
Username: Your Outlook Email Address (e.g. yourmail@gmail.com)
Password: Your Outlook Email Password
Port Number: 587 (With TLS)

Code to send mail using Outlook

MailMessage MailMessage = new MailMessage();
MailMessage.From = new MailAddress("yourmailid@yourdomain.com");
MailMessage.To.Add("recepientmailid@recepientdomain.com");
MailMessage.Subject = "Test Mail from Zoho";
MailMessage.Body = "Hello world this is a test";
SmtpClient SmtpClient = new SmtpClient();
SmtpClient.Host = "smtp.office365.com";
SmtpClient.EnableSsl = true;
SmtpClient.Port = 587;
SmtpClient.Credentials = new System.Net.NetworkCredential("frommailid@domainname.com", "yourpassword");
try
{
    SmtpClient.Send(MailMessage);
}catch(Exception ex)
{
    Console.WriteLine(ex.ToString());
    Console.ReadLine();
    throw ex;
}