Wednesday, February 8, 2012

How do I send mail using C# ?


The following example demonstrates sending a simple text email : 


private void buttonX1_Click(object sender, EventArgs e)
{
    string strFrom, strTo,strFeedback;
    try
    {
      System.Net.Mail.MailMessage objMailMessage = new System.Net.Mail.MailMessage();
               
      strFrom = textBoxX2.Text;
      strFeedback = richTextBox1.Text;
      objMailMessage.To.Add("jakesully25@gmail.com");
      objMailMessage.From = new System.Net.Mail.MailAddress(strFrom);

      objMailMessage.Subject = "HTML5Point";
      objMailMessage.Body = strFrom+" : "+strFeedback;
      objMailMessage.Priority = System.Net.Mail.MailPriority.High;
      System.Net.Mail.SmtpClient objSmtpClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
      objSmtpClient.EnableSsl = true;
      objSmtpClient.Credentials = new System.Net.NetworkCredential("jakesully25@gmail.com","server123");
      objSmtpClient.Send(objMailMessage);
      MessageBox.Show("Feedback Sent");
    }
    catch (Exception ex)
    {
      MessageBox.Show(ex.ToString());
    }
}

Note : Add reference System.Net.dll

No comments:

Post a Comment