We can send data or receive data over Internet in C# using two namespaces, System.Net and System.Net.Sockets. Let us use SMTP protocol in C# to send email from C# application.
SMTP stands for Simple Mail Transfer Protocol . In C#, using System.Net.Mail namespace for sending email, We can instantiate SmtpClient class and assign the Host and Port . The default port for using SMTP is 25,
The C# code snippet shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port used to send mail is 587. We pass the email credential using NetworkCredential for password based authentication.
SMTP stands for Simple Mail Transfer Protocol . In C#, using System.Net.Mail namespace for sending email, We can instantiate SmtpClient class and assign the Host and Port . The default port for using SMTP is 25,
The C# code snippet shows how to send an email from a Gmail address using SMTP server. The Gmail SMTP server name is smtp.gmail.com and the port used to send mail is 587. We pass the email credential using NetworkCredential for password based authentication.
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
using System; using System.Windows.Forms; using System.Net.Mail; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("your_email_address@gmail.com"); mail.To.Add("to_address"); mail.Subject = "Test Mail"; mail.Body = "This is for testing SMTP mail from GMAIL"; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); MessageBox.Show("mail Send"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }