Saturday, May 10, 2014

Simple Email Sending Application Using C# Windows Forms Application.

Open Visual Studio.
New Project -> Windows Form Application.
Design form as shown below :-

In the Code file :-


using System.Net;
using System.Net.Mail;

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

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                SmtpClient s = new SmtpClient("smtp.gmail.com");
                s.Port = 587;
                s.EnableSsl = true;
                s.Timeout = 100000;
                s.DeliveryMethod = SmtpDeliveryMethod.Network;
                s.UseDefaultCredentials = false;
                s.Credentials = new NetworkCredential("yourID@gmail.com", "YourPassword");
                MailMessage mm = new MailMessage();
                mm.To.Add(textBox1.Text);
                mm.From = new MailAddress("YourID@gmail.com");
                mm.Subject = textBox2.Text;
                mm.Body = richTextBox1.Text;
                Attachment a = new Attachment(textBox3.Text);
                mm.Attachments.Add(a);
                s.Send(mm);
                MessageBox.Show("Your email sent successfully.");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();
            if (d.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = d.FileName.ToString();
            }
        }
    }
}

Run file and send email.... Enjoy Have a good day!

No comments:

Post a Comment