Click here to Skip to main content
15,878,953 members
Articles / Programming Languages / C#
Article

Sending Mail Using C# via SMTP

Rate me:
Please Sign up or sign in to vote.
3.65/5 (36 votes)
19 Dec 2001 511K   16.7K   94   57
A simple application for sending mail via SMTP server using C#

Introduction

This project shows how to send mail via an SMTP server using C#.

How to Send Mail via SMTP

Based on RFC 821 about Simple Mail Transfer Protocol, it's very easy to send mail via SMTP. Here's the handshaking for sending mail:

Receiver: 220 server.com Simple Mail Transfer Service Ready
Sender  : HELO server.com
Receiver: 250 server.com
Sender  : MAIL FROM: <agusk@host.com>
Receiver: 250 OK
Sender  : RCPT TO: <myhoney@host.com>
Receiver: 250 OK
Sender  : DATA
Receiver: 354 Start mail input: end with <CRLF>.<CRLF>
Sender  : all data like From, To, Subject, Body etc.
          ended with <CRLF>.<CRLF>
Receiver: 250 OK
Sender  : QUIT
Receiver: 250 server.com closing transmission channel

Building the Application Using C#

Here's a model of the GUI:

add these code lines for when the use clicks the Send button:

C#
 private void SendBtn_Click(object sender, System.EventArgs e)
{
    // change cursor into wait cursor
    Cursor cr = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;

    // create server SMTP with port 25
    TcpClient SmtpServ = new TcpClient(ServSMTP.Text,25);
    string Data;
    byte[] szData;
    string CRLF = "\r\n";
    LogList.Items.Clear();            

    try
    {
        // initialization
        NetworkStream NetStrm = SmtpServ.GetStream();
        StreamReader  RdStrm= new StreamReader(SmtpServ.GetStream());
        LogList.Items.Add(RdStrm.ReadLine());
            
        // say hello to server and send response into log report
        Data = "HELLO server " + CRLF;                
        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
        NetStrm.Write(szData,0,szData.Length);
        LogList.Items.Add(RdStrm.ReadLine());
        // send sender data
        Data = "MAIL FROM: " + "<" + sFrom.Text + ">" + CRLF;
        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
        NetStrm.Write(szData,0,szData.Length);
        LogList.Items.Add(RdStrm.ReadLine());

        // send receiver data
        Data = "RCPT TO: " + "<" + sTo.Text + ">" + CRLF;
        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
        NetStrm.Write(szData,0,szData.Length);
        LogList.Items.Add(RdStrm.ReadLine());

        // send DATA
        Data = "DATA " + CRLF;
        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
        NetStrm.Write(szData,0,szData.Length);
        LogList.Items.Add(RdStrm.ReadLine());                

        // send content data
        Data = "SUBJECT: " + sSubject.Text + CRLF +
           sMessage.Text + CRLF + "." + CRLF;
        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
        NetStrm.Write(szData,0,szData.Length);
        LogList.Items.Add(RdStrm.ReadLine());                

        // quit from server SMTP
        Data = "QUIT " + CRLF;
        szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
        NetStrm.Write(szData,0,szData.Length);
        LogList.Items.Add(RdStrm.ReadLine());                

        // close connection
        NetStrm.Close();
        RdStrm.Close();
        LogList.Items.Add("Close connection");
        LogList.Items.Add("Send mail successly..");

        // back to normal cursor
        Cursor.Current = cr;

    }
    catch(InvalidOperationException err)
    {
        LogList.Items.Add("Error: "+ err.ToString());
    }
}

Reference

  • Document RFC 821 and MSDN for .NET Development

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder PE College
Indonesia Indonesia
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly,and ASP/ASP.NET. He's founder for PE College(www.pecollege.net), free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus and http://blog.aguskurniawan.net

Comments and Discussions

 
GeneralMy vote of 1 Pin
Andrei Keino19-Apr-14 8:49
Andrei Keino19-Apr-14 8:49 
Questionpassword Pin
AETCoder19-Jun-11 10:07
AETCoder19-Jun-11 10:07 
GeneralMy vote of 3 Pin
udhaya2code31-Jul-10 0:28
udhaya2code31-Jul-10 0:28 
Generalmail in C++ Pin
misa_qn23-Sep-08 21:47
misa_qn23-Sep-08 21:47 
GeneralRe: mail in C++ Pin
urapola10-Nov-08 17:12
urapola10-Nov-08 17:12 
Generalsending mail using C# Pin
sobhaniir9-Aug-08 0:25
sobhaniir9-Aug-08 0:25 
GeneralRe: sending mail using C# Pin
Pedes21-Aug-08 1:23
Pedes21-Aug-08 1:23 
GeneralHelp , please Pin
programet_nje15-Apr-08 6:38
programet_nje15-Apr-08 6:38 
GeneralHelp , please Pin
programet_nje15-Apr-08 6:37
programet_nje15-Apr-08 6:37 
Questions this sending mail trhough SMTP dependent on any mail cilent?? Pin
tika122-Apr-07 23:03
tika122-Apr-07 23:03 
Generalhaving problem Pin
mohsen.karami5-Nov-06 17:48
mohsen.karami5-Nov-06 17:48 
QuestionAuthentication problem Pin
DannyAdler23-Mar-06 14:54
DannyAdler23-Mar-06 14:54 
GeneralAttachements Pin
jacek_pl14-Dec-05 2:57
jacek_pl14-Dec-05 2:57 
GeneralRe: Attachements Pin
Ista26-Dec-05 16:39
Ista26-Dec-05 16:39 
GeneralError in Code Pin
rob_johnson_200316-Nov-05 14:37
rob_johnson_200316-Nov-05 14:37 
QuestionReady for c++? Pin
cemaaiem30-Oct-05 4:46
cemaaiem30-Oct-05 4:46 
GeneralSend Mail Pin
fredo_lefran10-Aug-05 6:23
fredo_lefran10-Aug-05 6:23 
GeneralOpenSMTP.NET Pin
Huisheng Chen5-May-05 4:49
Huisheng Chen5-May-05 4:49 
OpenSMTP.NET http://sourceforge.net/projects/opensmtp-net is recommended.

Regards,
unruledboy@hotmail.com
GeneralRe: OpenSMTP.NET Pin
Anonymous14-Jul-05 6:59
Anonymous14-Jul-05 6:59 
QuestionWindows 98? Pin
before12324-Sep-04 0:39
before12324-Sep-04 0:39 
GeneralSMTP server Pin
jmkuznitzki4-Aug-04 2:43
jmkuznitzki4-Aug-04 2:43 
GeneralSpecifying &quot;from&quot; field while sending the mail!!!! Pin
Anonymous13-May-04 0:32
Anonymous13-May-04 0:32 
AnswerRe: SMTP AUTH Login ? Pin
Liang Yitao25-Mar-04 6:05
Liang Yitao25-Mar-04 6:05 
GeneralRe: SMTP AUTH Login ? Pin
Paresh Gheewala23-Apr-04 8:06
Paresh Gheewala23-Apr-04 8:06 
GeneralRe: SMTP AUTH Login ? Pin
web-mouse3-May-04 20:16
web-mouse3-May-04 20:16 

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.