Introduction
There are many more articles on Google for sending an email using ASP.NET and C#. This article explains some important functions and notable errors.
Code
- using System.IO;
- using System.Net;
- using System.Net.Mail;
-
-
- string to = "[email protected]";
- string from = "[email protected]";
- MailMessage message = new MailMessage(from, to);
-
- string mailbody = "In this article you will learn how to send a email using Asp.Net & C#";
- message.Subject = "Sending Email Using Asp.Net & 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;
- }
We must add the following namespace:
- using System.Net;
- using System.Net.Mail;
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:
| Sl.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 |
Important SMTP Class Properties:
- Host: Server URL for SMTP (check the preceding table).
- EnableSsl: Check your host accepts SSL Connections (True or False).
- Port: Port Number of the SMTP server (check the preceding table).
- Credentials: Valid login credentials for the SMTP server (the email address and password).
- UseDefaultCredentials: When we set to True in UseDefaultCredentials then that specifies to allow authentication based on the credentials of the account used to send emails.
Common Error
The following error will occur commonly for everyone. How to solve this? Check my previous blog contents.
Tech Blog Reference
Summary
We learned how to send emails using ASP.NET and C#. I hope this article is useful for all .NET beginners.
Suchitra RaoPosted Oct 18, 2022, 4:50 PM
Hi Sir, Is the 'fromAddress' and network credentials are associated each other? Or is it like, we can use any from-mail-id and any other username-password to send a mail. Kindly help me. Thank you.
Ana MatPosted Jun 3, 2021, 1:34 PM
Hello ! I try to find what's the SMTP host for a roundcube /ovh mail, can you help me ? Thanks, ana
Arun DhanaPosted Feb 6, 2021, 5:27 PM
Hi Sir, Im getting this error. System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at'
Pushpam AbhishekPosted Jun 6, 2020, 7:53 AM
Thank you, very helpful articles. Its useful and helpful information for testers…Keep Sharing.Thanks , just learn in trickcode. We can help, please <a href="https://www.trickcode.in/"> Click Here. </a>
Antony KulandaisamyPosted Nov 29, 2019, 6:54 AM
Awesome man, thanks for the code... Easy to understand the way u gave.
Purvesh PatelPosted Feb 22, 2018, 5:32 PM
Take a look at this free service: https://www.paylite.net/communicate.html. A developer can insert 20 lines of code and send email and SMS, get: open tracking, click tracking, and simple method to review history. Just sign up for free account and you can send out email and SMS from single REST API or BYOL (bring your own licenses) for SendGrid, Office 365, Google Gsuite and several other providers. NuGet: https://www.nuget.org/packages/PayLiteCommunication/ Documentation: https://portal.paylite.net/docs/
hello friendsPosted Aug 9, 2017, 12:47 PM
Hi there, im facing issue when sending email using company email smtp config, but the code works in gmail and hotmail smtp config. The company email server and hosting is from same service provider but only domain name is bought from godaddy..please guide me on this..you can view the code in this link..https://stackoverflow.com/q/45520041/5623420 thank you
Delpin Susai RajPosted Aug 28, 2016, 7:24 AM
Nice
Sakshi JainPosted Dec 3, 2015, 8:24 AM
"Server does not support secure connections.". I am getting this exception. I have given correct credentials like : EmailID and Password. Can you tell me y?
Sakshi JainPosted Dec 3, 2015, 8:23 AM
Hey rajeesh, i tried this code but its throwing me this exception.
Susanta RoutPosted Aug 20, 2015, 8:51 AM
speed of sending is very slow
Van LaiPosted Jul 16, 2015, 10:26 PM
I can't wait to have a try. Thank you!
Pankaj Kumar ChoudharyPosted Jul 16, 2015, 6:51 PM
Nice Explain Sir...........
Santhakumar MunuswamyPosted Jul 16, 2015, 3:40 PM
Nice Article! Thanks for sharing
Narasimha Reddy ChennupalliPosted Jul 16, 2015, 8:47 AM
Good info..
Debasis SahaPosted Jul 16, 2015, 6:11 AM
Good one..
Sibeesh VenuPosted Jul 16, 2015, 3:28 AM
Thanks for the information .
Nilesh JadavPosted Jul 16, 2015, 2:48 AM
Nice one sir ! even i tried email to gmail yesterday ! Thank you for sharing.