Sending Email From the windows application using C# code

Karthikreddy
Posted by Karthikreddy under C# category on | Points: 40 | Views : 19382
1.Design a windows form with 3 labels , 3 text boxes and one Button

2. write the following code on button click event


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace Email
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
string to=textBox1.Text;
MailMessage ms = new MailMessage();
ms.To.Add(to);
ms.From = new MailAddress("rkarthik@gmail.com");
ms.Subject = textBox2.Text;
ms.Body = textBox3.Text;
ms.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("localhost",25);
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential("rkarthik@augustasoftsol.com", "Your_password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;
smtp.Send(ms);
MessageBox.Show("mail Send");

}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

}
}
}

Comments or Responses

Login to post response