Click here to Skip to main content
12,509,932 members (57,263 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked

Sending Emails from C# Application using default SMTP

, 21 Sep 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Sending Mails from C# using System.Web.Mail using default SMTP

Screenshot - SendMail.jpg


This article is provide a demo for how we can send Emails from a C# Windows Application.


First add the reference from project-->Add References then select System.Web.dll. Then include using System.Web.Mail in your class and create MailMessage object and assign values to all required properties.

MailMessage objMsg=new MailMessage();

if(ValidateEmail(txtTo.Text)==true) objMsg.To=txtTo.Text;

else throw new MyExp();


if (txtCc.Text!="") objMsg.Bcc=txtBcc.Text;

if (txtBcc.Text!="") objMsg.Cc=txtCc.Text;

if (txtSubject.Text!="") objMsg.Subject=txtSubject.Text;

else throw new MyExp();


You can send attachments also with your mails. For that we can use MailAttachment Object like :

MailAttachment attach1;
string sFile;
    for(int i=0;i<lstfiles.Items.Count;i++)
        attach1=new MailAttachment(sFile);

But before sending the mail you have to set From field by clicking From Button and Smtp Server, if you are using it on Windows 2000, Windows XP Professional, Windows Server 2003 and have a configured smtp server then you can leave it blank.

Then you can send the mail by clicking Send Button.


Also when you run this application it will show in System tray and when you right click on that icon it will show the popup menu.

mymenu=new ContextMenu();

mymenu.MenuItems.Add("Open",new System.EventHandler(open_Click));

mymenu.MenuItems.Add("Hide",new System.EventHandler(hide_Click));

mymenu.MenuItems.Add("Exit",new System.EventHandler(exit_Click));

myIcon=new NotifyIcon();

myIcon.Text="Right Click For Context Menu";


myIcon.Icon=new Icon(GetType(),"MAIL6.ICO");


This above image show the popup at System tray, from where we can Hide,Open and Exit the Application.

Complete Source Code

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Web.Mail;

using System.Text.RegularExpressions;

using MailOpt;

namespace MailTest


public class SendMail : System.Windows.Forms.Form


    private System.Windows.Forms.TextBox txtBcc;

    private System.Windows.Forms.Label label1;

    private System.Windows.Forms.Label label2;

    private System.Windows.Forms.Label label3;

    private System.Windows.Forms.Label label4;

    private System.Windows.Forms.Label label5;

    private System.Windows.Forms.Label label6;

    private System.Windows.Forms.Button button3;

    private System.Windows.Forms.Label label7;

    private System.Windows.Forms.OpenFileDialog dlgopenFile;

    private System.Windows.Forms.Button btnshowfile;

    private System.Windows.Forms.TextBox txtAttachment;

    private System.Windows.Forms.TextBox txtTo;

    private System.Windows.Forms.TextBox txtSubject;

    private System.Windows.Forms.TextBox txtCc;

    private System.Windows.Forms.RichTextBox txtMessage;

    private System.Windows.Forms.ListBox lstfiles;

    private System.Windows.Forms.Button btnSend;

    int c=0;

    public static string sFrom="";

    public static string sSmtpServer="";

    private System.Windows.Forms.Button btnFrom;

    NotifyIcon myIcon;

    ContextMenu mymenu;

    private System.ComponentModel.Container components = null;

    public SendMail()




    protected override void Dispose( bool disposing )


        if( disposing )






            if (components != null) 





            base.Dispose( disposing );



    static void Main() 


        Application.Run(new SendMail());


    private void btnSend_Click(object sender, System.EventArgs e)




    if (sSmtpServer=="")


    MessageBox.Show("You have not set any SmtpServer name! It will " & _
"use default Mail Server if you configured on your machine","SmtpServer Name Not Found",







if(sFrom=="" || sFrom==<a href=""></a>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
Working as Consultant with Merrill Lynch, New York

You may also be interested in...


Comments and Discussions

Questionhow to know my smtp server Pin
Member 1022082129-Sep-13 10:45
memberMember 1022082129-Sep-13 10:45 
QuestionEmail from C# program Pin
boss prabu19-Mar-13 7:51
memberboss prabu19-Mar-13 7:51 
QuestionWhat is MailOpt? Pin
trantoan6710-Oct-12 8:01
membertrantoan6710-Oct-12 8:01 
QuestionSending Emails from C# Application using default SMTP Pin
Wosu30-May-12 3:51
memberWosu30-May-12 3:51 
QuestionEmail Component Pin
Gregory Nozik28-Sep-11 15:00
memberGregory Nozik28-Sep-11 15:00 
GeneralMy vote of 4 Pin
jorge acevedo16-Aug-10 4:00
memberjorge acevedo16-Aug-10 4:00 
GeneralGetting Error Pin
sansar15-Feb-08 0:24
membersansar15-Feb-08 0:24 
GeneralKay says there's a potential bug Pin
Sean Ewington4-Jun-07 8:55
adminSean Ewington4-Jun-07 8:55 
Generalhi Pin
azmiralda1228-Dec-06 3:17
memberazmiralda1228-Dec-06 3:17 
GeneralSMTP username/password Pin
shabonaa1-Dec-06 10:48
membershabonaa1-Dec-06 10:48 
GeneralSMTP Pin
GKT19-May-06 12:44
memberGKT19-May-06 12:44 
QuestionCan not access CDO.Message Pin
canhga18-Sep-05 23:09
membercanhga18-Sep-05 23:09 
AnswerRe: Can not access CDO.Message Pin
anzac5-Dec-05 10:40
memberanzac5-Dec-05 10:40 
GeneralRe: Not robust enough Pin
Rajan2May14-Sep-04 17:59
memberRajan2May14-Sep-04 17:59 
GeneralXP Home Pin
mxmissile14-Sep-04 9:48
membermxmissile14-Sep-04 9:48 
GeneralRe: XP Home Pin
Heath Stewart21-Sep-04 10:42
protectorHeath Stewart21-Sep-04 10:42 
GeneralNot robust enough Pin
mav.northwind13-Sep-04 20:42
membermav.northwind13-Sep-04 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160929.1 | Last Updated 21 Sep 2004
Article Copyright 2004 by Rajan2may
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid