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

Sending Mail Using C# via SMTP

, 19 Dec 2001
Rate this:
Please Sign up or sign in to vote.
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:

 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

Share

About the Author

Agus Kurniawan
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 Pinmemberdandy_andy19-Apr-14 9:49 
Questionpassword PinmemberAETCoder19-Jun-11 11:07 
GeneralMy vote of 3 Pinmemberudhaya2code31-Jul-10 1:28 
Generalmail in C++ Pinmembermisa_qn23-Sep-08 22:47 
GeneralRe: mail in C++ Pinmemberurapola10-Nov-08 18:12 
GeneralHelp , please Pinmemberprogramet_nje15-Apr-08 7:38 
GeneralHelp , please Pinmemberprogramet_nje15-Apr-08 7:37 
Questions this sending mail trhough SMTP dependent on any mail cilent?? Pinmembertika123-Apr-07 0:03 
Generalhaving problem Pinmembermohsen.karami5-Nov-06 18:48 
QuestionAuthentication problem PinmemberDannyAdler23-Mar-06 15:54 
GeneralAttachements Pinmemberjacek_pl14-Dec-05 3:57 
GeneralRe: Attachements PinmemberIsta26-Dec-05 17:39 
GeneralError in Code Pinmemberrob_johnson_200316-Nov-05 15:37 
QuestionReady for c++? Pinmembercemaaiem30-Oct-05 5:46 
GeneralSend Mail Pinmemberfredo_lefran10-Aug-05 7:23 
GeneralOpenSMTP.NET PinmemberUnruled Boy5-May-05 5:49 
GeneralRe: OpenSMTP.NET PinsussAnonymous14-Jul-05 7:59 
QuestionWindows 98? Pinmemberbefore12324-Sep-04 1:39 
GeneralSMTP server Pinmemberjmkuznitzki4-Aug-04 3:43 
GeneralSpecifying &quot;from&quot; field while sending the mail!!!! PinsussAnonymous13-May-04 1:32 
AnswerRe: SMTP AUTH Login ? PinmemberLiang Yitao25-Mar-04 7:05 
GeneralRe: SMTP AUTH Login ? Pinmemberpareshgheewala23-Apr-04 9:06 
GeneralRe: SMTP AUTH Login ? Pinmemberweb-mouse3-May-04 21:16 
AnswerRe: SMTP AUTH Login ? Pinmembermirsaman25-Oct-11 8:46 
GeneralBody message Pinmemberfelipedrumond25-Oct-03 10:55 
GeneralSMTP using C# Pinmembersanalsasiraj20-Aug-03 2:47 
GeneralTwo question PinmemberMazdak7-May-03 7:24 
GeneralHtml Format Pinmemberaudepoupette28-Apr-03 5:44 
GeneralUnhandled exception error Pinmemberjulianarevalo21-Feb-03 20:21 
GeneralRe: Unhandled exception error Pinmembercscoder31-May-03 7:19 
QuestionHow to format message as HTML ? Pinsusshandsmart17-Dec-02 10:14 
AnswerRe: How to format message as HTML ? PinmemberEd K17-Dec-02 10:20 
GeneralRe: How to format message as HTML ? Pinmemberrarerose9-Mar-06 14:54 
QuestionSMTP AUTH Login ? PinsussAnonymous29-Oct-02 23:05 
GeneralGetting POP3 Mail Attachments PinmemberAnonymous29-Aug-02 4:02 
Generalusing System.Web.Mail; PinmemberPhilip Fitzsimons15-Aug-02 4:42 
GeneralRe: using System.Web.Mail; PinsussAnonymous23-Aug-02 7:13 
GeneralRe: using System.Web.Mail; Pinmembersmallguy7819-Jun-03 14:14 
GeneralRe: using System.Web.Mail; PinmemberTim_Wilde12-Sep-05 8:41 
GeneralRe: using System.Web.Mail; PinmemberBarista Bill17-Nov-04 11:33 
GeneralRe: using System.Web.Mail; PinmemberPhilip Fitzsimons18-Nov-04 0:49 
GeneralRe: using System.Web.Mail; PinsussAnonymous4-Mar-05 12:55 
GeneralGet MAIL from SMTP Using C# PinsussAnonymous14-Aug-02 10:57 
GeneralRe: Get MAIL from SMTP Using C# PinmemberMadShad200417-Jan-05 21:57 
GeneralSMTP PinsussAnonymous7-Aug-02 21:55 
GeneralError PinsussAnonymous11-Jul-02 7:20 
GeneralRe: Error PinmemberDoug Brower26-Sep-02 3:35 
GeneralAttachment PinmemberAnonymous4-Jul-02 12:57 
GeneralRe: Attachment PinmemberAnonymous4-Jul-02 22:12 
GeneralLittle bug in your code PinmemberAlbert Pascual26-Apr-02 9:27 

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 | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 20 Dec 2001
Article Copyright 2001 by Agus Kurniawan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid