using System.Net.Mail; using System.Windows.Forms; namespace email { public partial class Form1 : Form { // creating the objects of smtpclient class and mailmessage class //smtpclient class is used to setup a smtp server client i am using gmail. SmtpClient client = new SmtpClient(); MailMessage msg = new MailMessage(); System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("your_email@gmail.com", "Your Password"); public Form1() { InitializeComponent(); } // we create a method name sendemail when we want to send email we just pass arguments public void sendemail(string sendto ,string sendfrom, string subject, string body) { try { // setting up smtp client, port other details client.Host = "smtp.gmail.com"; client.Port = 587; client.UseDefaultCredentials = false; client.Credentials = credentials; // you can skip this line it is for html emails if you want to send html emails then set msg.isbodyhtml = true msg.IsBodyHtml = true; // convert strings to mail address MailAddress to = new MailAddress(sendto); MailAddress from = new MailAddress(sendfrom); msg.Subject = subject; msg.Body = body; msg.To.Add(to); msg.From = from; client.Send(msg); MessageBox.Show("Email Sent"); } catch (Exception ex) { MessageBox.Show(ex.Message + "Error"); } } // on button click event we called sendemail method private void btnsendmail_Click(object sender, EventArgs e) { sendemail(txtto.Text.ToString(), "nishantbcaproject@gmail.com", txtsubject.Text.ToString(), txtbody.Text.ToString()); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)