Send Email from c#

This post shows how to send email from C# using various protocols available in the .Net framework.The System.Net and System.Net.Sockets are the two namespaces used for managed implementation of internet protocols that application can use to send and receive data over the internet.


Open Visual Studio and Create New C# Window Application then design a form like this.


Switch to code editor from design windows by pressing "F7" and add "System.Net.Mail;" namespace. Let's bind code to the windows form we designed.

Source Code

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 Emailwithcsharp
{
    public partial class Form1 : Form
    {
        string suserName = "Enter your email address here";
        string spassWord = "Enter your password here";
        //Instance for smpt
        MailMessage mailMessage;
        SmtpClient smtpClient;
        public Form1()
        {
            InitializeComponent();
            mailMessage = new MailMessage();
            smtpClient = new SmtpClient("smtp.gmail.com");
            txtFromemail.Text = suserName;
            txtFromPassword.Text = spassWord;
            txtFromemail.Enabled = false;
            txtFromPassword.Enabled = false;

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                mailMessage.From = new MailAddress(txtFromemail.Text);
                mailMessage.To.Add(txtTo.Text);
                mailMessage.Subject = txtSub.Text;
                mailMessage.Body = txtbody.Text;
                System.Net.Mail.Attachment attachment= new         System.Net.Mail.Attachment(txtattachment.Text);
                mailMessage.Attachments.Add(attachment);
                smtpClient.Port = 587;
                smtpClient.Credentials = new System.Net.NetworkCredential(txtFromemail.Text, txtFromPassword.Text);
                smtpClient.EnableSsl = true;
                //send the mail
                if (txtTo.Text != null)
                {
                    smtpClient.Send(mailMessage);
                }
                else
                {
                    MessageBox.Show("Enter the destination address...");
                }
                MessageBox.Show("Mail sent sucessfully ....");
            }
            catch (Exception exception)
            {
                MessageBox.Show("Exception found:" + exception.ToString());
            }
           
        }
       //send mail
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.FileName = "Jpeg Files|*.jpeg";
                openFileDialog1.FileName = string.Empty;
                DialogResult result = openFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtattachment.Text = openFileDialog1.FileName;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }
       //clear all the fileds
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtTo.Text = "";
            txtSub.Text = "";
            txtbody.Text = "";
            txtattachment.Text = "";
        }
      
        private void bntChange_Click(object sender, EventArgs e)
        {
            txtFromemail.Enabled = true;
            txtFromPassword.Enabled = true;
        }
        //update the from address and password
        private void btnSave_Click(object sender, EventArgs e)
        {
            txtFromemail.Enabled = false;
            txtFromPassword.Enabled = false;
        }
    }
}

Screenshot:

Input:

Fill the required fields and then click the send button.


Output:


Download source code



No comments :

Post a Comment