Click here to Skip to main content
6,596,602 members and growing! (19,060 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Email     Intermediate

Mail library using sockets, in C#

By vmlinuxx

My own mail library without CDO and System.Web.Mail.
C#.NET 1.1, Win2003, Visual Studio, Dev
Posted:27 May 2004
Views:43,691
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 3.53 Rating: 3.00 out of 5
5 votes, 33.3%
1
2 votes, 13.3%
2
1 vote, 6.7%
3
2 votes, 13.3%
4
5 votes, 33.3%
5

1. Introduction

This mail library is an extension of SocketHelper which is described in my previous article.

No CDO needed and no System.Web.Mail which is a package to CDO needed.

Now, let's see what is inside.

2. Classes

  1. MailMessage
  2. SmtpServer

MailMessage is used for storing mail messages (currently doesn't support attachments).

SmtpServer is a class to communicate with SMTP servers, inside which there is a SocketHelper (with some updates, download source code for detail).

3. Code Detail

The class MailMessage:

    public class MailMessage
    {
        public enum BodyFormat
        {
            HTML,TEXT
        }

        private string sFrom;
        private string sTo;
        private string sCc;
        private string sSubject;
        private BodyFormat bfFormat=BodyFormat.TEXT;
        private string sBody;
        private string sFromName;
        private string sToName;

        public string Cc
        {
            get
            {
                return sCc;
            }
            set
            {
                sCc=value;
            }
        }

        public string FromName
        {
            get
            {
                if(sFromName==null || sFromName=="")
                    sFromName=sFrom;

                return sFromName;
            }
            set
            {
                sFromName=value;
            }
        }

        public string ToName
        {
            get
            {
                if(sToName==null || sToName=="")
                    sToName=sTo;

                return sToName;
            }
            set
            {
                sToName=value;
            }
        }

        public string From
        {
            get
            {
                return sFrom;
            }
            set
            {
                sFrom=value;
            }
        }

        public string To
        {
            get
            {
                return sTo;
            }
            set
            {
                sTo=value;
            }
        }

        public string Subject
        {
            get
            {
                return sSubject;
            }
            set
            {
                sSubject=value;
            }
        }

        public string Body
        {
            get
            {
                return sBody;
            }
            set
            {
                sBody=value;
            }
        }

        public BodyFormat MailFormat
        {
            get
            {
                return bfFormat;
            }
            set
            {
                bfFormat=value;
            }
        }

        public MailMessage()
        {
            //

            // TODO: Add constructor logic here

            //

        }

        public override string ToString()
        {
            StringBuilder sb=new StringBuilder();
            sb.AppendFormat("From: {0}\r\n",this.FromName);
            sb.AppendFormat("To: {0}\r\n",this.ToName);
            if(this.Cc!=null && this.Cc!="")
            {
                sb.AppendFormat("Cc: {0}\r\n",this.Cc.Replace(";",","));
            }
            sb.AppendFormat("Date: {0}\r\n", 
              DateTime.Now.ToString("ddd, d M y H:m:s z"));
            sb.AppendFormat("Subject: {0}\r\n",this.Subject);
            sb.AppendFormat("X-Mailer: vmlinux.Net.Mail.SmtpServer\r\n");
            switch(this.MailFormat)
            {
                case BodyFormat.TEXT:
                  sb.AppendFormat("Content-type:text/plain;Charset=GB2312\r\n");
                  break;
                case BodyFormat.HTML:
                  sb.AppendFormat("Content-type:text/html;Charset=GB2312\r\n");
                  break;
                default:
                  break;
            }
            sb.AppendFormat("\r\n{0}\r\n",this.Body);
            return sb.ToString();
        }
    }

I get all the message inside mail through the method ToString().

Now, the following is class SmtpServer:

    public class SmtpServer
    {
        private string sServer;
        private int iPort;
        private bool bAuth=false;
        private string sUser;
        private string sPass;
        private StringBuilder sbLog=null;
        private bool bLog=true;
        private SocketHelper helper=null;
        private TalkState state=TalkState.WaitInit;

        public enum TalkState
        {
            WaitInit,Initialized,SignedIn,LoginFailed,Ended
        }

        protected SocketHelper Helper
        {
            get
            {
                if(helper==null)
                    helper=new SocketHelper(this.Server,this.Port);

                return helper;
            }
        }

        public bool LogEvent
        {
            get
            {
                return bLog;
            }
            set
            {
                bLog=value;
            }
        }

        public string LogMessage
        {
            get
            {
                if(this.LogEvent)
                    return sbLog.ToString();
                else
                    return "";
            }
        }

        public string Server
        {
            get
            {
                return sServer;
            }
            set
            {
                sServer=value;
            }
        }

        public int Port
        {
            get
            {
                return iPort;
            }
            set
            {
                iPort=value;
            }
        }

        public bool RequireAuthorization
        {
            get
            {
                return bAuth;
            }
            set
            {
                bAuth=value;
            }
        }

        public string AuthUser
        {
            get
            {
                return sUser;
            }
            set
            {
                sUser=value;
            }
        }

        public string AuthPass
        {
            get
            {
                return sPass;
            }
            set
            {
                sPass=value;
            }
        }

        public SmtpServer()
        {
            //

            // TODO: Add constructor logic here

            //

        }

        public void Init()
        {
            Log(helper.GetFullResponse());
            Helper.SendCommand("EHLO "+this.Server);
            Log(Helper.GetFullResponse());
            state=TalkState.Initialized;
        }

        public bool Login()
        {
            if(this.RequireAuthorization)
            {
                Helper.SendCommand("AUTH LOGIN");
                Log(Helper.GetFullResponse());
                Helper.SendCommand(
                    Convert.ToBase64String(
                    Encoding.Default.GetBytes(this.AuthUser)));
                Log(Helper.GetFullResponse());
                Helper.SendCommand(
                    Convert.ToBase64String(
                    Encoding.Default.GetBytes(this.AuthPass)));
                Log(Helper.GetFullResponse());

                if(Helper.GetResponseState()==235)
                {
                    state=TalkState.SignedIn;
                    return true;
                }
                else
                {
                    state=TalkState.LoginFailed;
                    return false;
                }
//                return (Helper.GetResponseState()==235);

            }
            else
            {
                state=TalkState.SignedIn;
                return true;
            }
        }

        public void SendKeep(MailMessage msg)
        {
            if((state==TalkState.Initialized && 
               !this.RequireAuthorization) || state==TalkState.SignedIn)
            {
                Helper.SendCommand("MAIL From:"+msg.From);
                Log(Helper.GetFullResponse());
                Helper.SendCommand("RCPT To:"+msg.To);
                Log(Helper.GetFullResponse());
                Helper.SendCommand("DATA");
                Log(Helper.GetFullResponse());
                Helper.SendData(Encoding.Default.GetBytes(msg.ToString()));
                Helper.SendCommand(".");
                Log(Helper.GetFullResponse());
            }
        }

        public void Quit()
        {
            Helper.SendCommand("QUIT");
            Helper.Close();
            state=TalkState.Ended;
        }

        public bool Send(MailMessage msg)
        {
            if(state==TalkState.Ended)
                return false;

            Log(Helper.GetFullResponse());
            Helper.SendCommand("EHLO "+this.Server);
            Log(Helper.GetFullResponse());
            if(this.RequireAuthorization)
            {
                Helper.SendCommand("AUTH LOGIN");
                Log(Helper.GetFullResponse());
                Helper.SendCommand(
                    Convert.ToBase64String(
                    Encoding.Default.GetBytes(this.AuthUser)));
                Log(Helper.GetFullResponse());
                Helper.SendCommand(
                    Convert.ToBase64String(
                    Encoding.Default.GetBytes(this.AuthPass)));
                Log(Helper.GetFullResponse());
                if(Helper.GetResponseState()!=235)
                {
                    Helper.SendCommand("QUIT");
                    Helper.Close();
                    state=TalkState.Ended;
                    return false;
                }
            }
            Helper.SendCommand("MAIL From:"+msg.From);
            Log(Helper.GetFullResponse());
            Helper.SendCommand("RCPT To:"+msg.To);
            Log(Helper.GetFullResponse());
            Helper.SendCommand("DATA");
            Log(Helper.GetFullResponse());
            Helper.SendData(Encoding.Default.GetBytes(msg.ToString()));
            Helper.SendCommand(".");
            Log(Helper.GetFullResponse());
            Helper.SendCommand("QUIT");
            Helper.Close();
            state=TalkState.Ended;
            return true;
        }

        private void Log(string msg)
        {
            if(this.LogEvent)
            {
                if(sbLog==null)
                    sbLog=new StringBuilder();

                sbLog.AppendFormat("{0}\r\n",msg);
            }
        }
    }

You can send multiple emails in one connection or simply send one.

To send multiple emails, follow this command sequence:

  1. Init --- to receive server response and say hello.
  2. Login --- if the server requires user login.
  3. SendKeep --- send mail and keep connection.
  4. ... --- more mail through SendKeep.
  5. Quit --- say good bye to server.

If you are sending only one mail in one connection, simply call Send.

So that's it.

4. TODO

  1. error handling
  2. attachments

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

About the Author

vmlinuxx


Member

Occupation: Web Developer
Location: China China

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Generalworks without SMTP server ? PinmemberMember 35372291:47 22 Sep '09  
General[Message Deleted] Pinmemberit.ragester22:50 2 Apr '09  
GeneralSend Email Pinmemberam2h23:36 25 May '08  
GeneralSMTP Pinmemberprogramet_nje7:52 15 Apr '08  
GeneralCompact Framework Pinmemberchmod22228:02 24 Jul '07  
GeneralSending Progress PinmemberKostas Dak22:24 4 Sep '06  
GeneralTrouble Pinmember_BlackLine_3:00 21 May '06  
GeneralRe: Trouble Pinmember_BlackLine_3:27 21 May '06  
GeneralVery nice! Pinmember_BlackLine_2:42 21 May '06  
GeneralWierd dates on sent messages PinmemberScottEllisNovatex20:52 21 Apr '05  
GeneralRe: Wierd dates on sent messages Pinmemberjaygeek9:19 14 Sep '05  
GeneralYeah... Nice... PinmemberHumanOsc22:58 27 May '04  
GeneralRe: Yeah... Nice... PinmemberGiles23:46 27 May '04  
GeneralRe: Yeah... Nice... Pinmembervmlinuxx23:59 27 May '04  
GeneralRe: Yeah... Nice... PinmemberHumanOsc22:40 28 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 May 2004
Editor: Smitha Vijayan
Copyright 2004 by vmlinuxx
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project