Thursday 18 October 2012

How to send a mail with attachment from windows form using C#.Net. || Send an Email using corporate or business email address in C#.Net.

To implement the Email Sending in Windows Application  you need to follow the below steps :

Step1 :
On the File menu, click New Project. Select Windows Forms Application as your project type.
Design the Form using controls from Toolbox.

Step2:
Now open the Form.cs page and write the following source code.

Form.cs 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;
using System.Net.Mail;
using System.Text.RegularExpressions;


namespace Sample
{
    public partial class Sample_MailSend : Form
    {

        public AmirAds_MailSend()
        {
            InitializeComponent();
        }
  
        private void Cancel()
        {
            txtFromMail.Text = string.Empty;
            txtMailPassword.Text = string.Empty;
            txtToMail.Text = string.Empty;
            txtCC.Text = string.Empty;
            txtSubject.Text = string.Empty;
            txtAttachments.Text = string.Empty;
            txtBody.Text = string.Empty;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtFromMail.Text == string.Empty || txtMailPassword.Text == string.Empty || txtToMail.Text == string.Empty || txtSubject.Text == string.Empty)
            {
                MessageBox.Show("Please fill all the fields.", "Alert!!");
            }

            else
            {
                string pattern = null;
                pattern = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";

                if (Regex.IsMatch(txtFromMail.Text, pattern) || Regex.IsMatch(txtToMail.Text, pattern) || Regex.IsMatch(txtCC.Text, pattern))
                {
                    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                    msg.From = new MailAddress(txtFromMail.Text);
                    msg.To.Add(txtToMail.Text);//Text Box for To Address

                    if (txtCC.Text != string.Empty)
                    {
                        msg.CC.Add(new MailAddress(txtCC.Text));
                    }

                    msg.Subject = txtSubject.Text; //Text Box for subject
                    msg.IsBodyHtml = true;
                    msg.Body = txtBody.Text;//Text Box for body

                    if (txtAttachments.Text != string.Empty)
                    {
                        System.Net.Mail.Attachment attachment;
                        attachment = new System.Net.Mail.Attachment(txtAttachments.Text);
                        msg.Attachments.Add(attachment);
                    }

                    //msg.Priority = MailPriority.High;
                    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("relay-hosting.secureserver.net", 26);
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential(txtFromMail.Text, txtMailPassword.Text);
                    client.Port = 26;
                    client.Host = "mail.corporatemail.com ";
                    client.EnableSsl = false;
                    object userstate = msg;
                   
                    try
                    {
                        client.Send(msg);
                        MyMessageBox.ShowBox("Your mail has been sent sucessfully.");
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        MyMessageBox.ShowBox(ex.Message);
                    }
                    Cancel();
                }
                else
                {
                    MyMessageBox.ShowBox("Invalid email address");
                }
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult res = openFileDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                txtAttachments.Text = openFileDialog1.FileName;
            }
        }

       }
}

Step4:
Now build the Solution and Debug it for the output.

1 comment:

  1. Hi,
    The code
    if (txtAttachments.Text != string.Empty)
    {
    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment(txtAttachments.Text);
    msg.Attachments.Add(attachment);
    }


    Its says that the attachment column is mandatory how to implement if that particular column should not be mandatory.
    i.e it says that the file in the attachment is must, but i dont want to make it mandatory. i want to implement stating that if he wants he should go for it or else leave blank.
    how should we implement.

    ReplyDelete