Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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

Description

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();

objMsg.From=sFrom;

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();

objMsg.Body=txtMessage.Text;

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

MailAttachment attach1;
string sFile;
if(lstfiles.Items.Count>0)
{
    for(int i=0;i<lstfiles.Items.Count;i++)
    {
        lstfiles.SelectedIndex=i;
        sFile=lstfiles.Text;
        attach1=new MailAttachment(sFile);
        objMsg.Attachments.Add(attach1);
    }
}

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.

SmtpMail.Send(objMsg); 

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.Visible=true;

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

myIcon.ContextMenu=mymenu;

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()

    {

    InitializeComponent();

    }

    protected override void Dispose( bool disposing )

    {

        if( disposing )

        {

            if(disposing)

            {

                this.myIcon.Dispose();

            }

            if (components != null) 

            {

                components.Dispose();

            }

        }

            base.Dispose( disposing );


    }    

    [STAThread]

    static void Main() 

    {

        Application.Run(new SendMail());

    }

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

    {

    try

    {

    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",
MessageBoxButtons.OK,MessageBoxIcon.Information);

btnFrom.Focus();

}

else

{

SmtpMail.SmtpServer=sSmtpServer;

}

if(sFrom=="" || sFrom==<a href="mailto:test@mail.com">test@mail.com</a>)

{

MessageBox.Show("Please set your (Sender's) Address First","Sender Address",
MessageBoxButtons.OK,MessageBoxIcon.Error);

btnFrom.Focus();

return;

}


MailMessage objMsg=new MailMessage();

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

else throw new MyExp();

objMsg.From=sFrom;

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();

objMsg.Body=txtMessage.Text;

MailAttachment attach1;//, attach2, attach3;

string sFile;

if(lstfiles.Items.Count>

License

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

Share

About the Author

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

Comments and Discussions

 
Questionhow to know my smtp server PinmemberMember 1022082129-Sep-13 10:45 
QuestionEmail from C# program Pinmemberboss prabu19-Mar-13 7:51 
Really helpful post!! Thanks. Even this website http://www.compiletimeerror.com/2013/03/c-sharp-send-email-from-c-program.html[^] also addresses something similar.. Have a look.. Might help..
QuestionWhat is MailOpt? Pinmembertrantoan6710-Oct-12 8:01 
QuestionSending Emails from C# Application using default SMTP PinmemberWosu30-May-12 3:51 
QuestionEmail Component PinmemberGregory Nozik28-Sep-11 15:00 
GeneralMy vote of 4 Pinmemberjorge acevedo16-Aug-10 4:00 
GeneralGetting Error Pinmembersansar15-Feb-08 0:24 
GeneralKay says there's a potential bug PinadminSean Ewington4-Jun-07 8:55 
Generalhi Pinmemberazmiralda1228-Dec-06 3:17 
GeneralSMTP username/password Pinmembershabonaa1-Dec-06 10:48 
GeneralSMTP PinmemberGKT19-May-06 12:44 
QuestionCan not access CDO.Message Pinmembercanhga18-Sep-05 23:09 
AnswerRe: Can not access CDO.Message Pinmemberanzac5-Dec-05 10:40 
GeneralRe: Not robust enough PinmemberRajan2May14-Sep-04 17:59 
GeneralXP Home Pinmembermxmissile14-Sep-04 9:48 
GeneralRe: XP Home PinprotectorHeath Stewart21-Sep-04 10:42 
GeneralNot robust enough Pinmembermav.northwind13-Sep-04 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.141022.2 | Last Updated 21 Sep 2004
Article Copyright 2004 by Rajan2may
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid