Library tutorials & articles

Custom SMTP in C#

The Other Methods

The other two methods are implemented in our class and described in the next paragraphs. The Write method writes data to the socket.

public void Write( string message)
{
    System.Text.ASCIIEncoding en = new System.Text.ASCIIEncoding() ;

    byte[] WriteBuffer = new byte[ 1024] ;
    WriteBuffer = en. GetBytes( message) ;

    NetworkStream stream = GetStream() ;
    stream. Write( WriteBuffer, 0, WriteBuffer. Length);
}

It must be remembered that C#'s native string type is not based on the ASCII characters, but rather is based on Unicode. Thus in order to send messages on SMTP we must first convert the string input parameter to an array of ASCII bytes. We use the ASCIIEncoding class in dotNET to make this transformation. Then we retrieve the socket stream using the GetStream method inherited from the TcpClient class and write the bytes to the stream.

The Response method receives data from the socket.

public string Response()
{
    System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
    byte[] serverbuff = new Byte[ 1024];
    NetworkStream stream = GetStream();
    int count = stream. Read( serverbuff, 0, 1024 );
    if (count == 0)
        return "";

    return enc. GetString( serverbuff, 0, count );
}

The Response method begins by retrieving the bytes from the stream and then converts the incoming ASCII bytes to our native C# string type. Now that we have an SMTP class, you might be wondering how you use it.

Comments

  1. 10 Nov 2005 at 15:51
  2. 10 Nov 2005 at 15:50

    Try using this library:
    http://www.lesnikowski.com/mail


    It's simple and effective.
    Allows sending, receiving and parsing email messages.
    Encodes/decodes any kind of attachment.
    Includes POP3 and STMP clients.
    Written entirely in managed code in C#.

  3. 01 Sep 2004 at 14:55
    well... TO is used for the main person you're sending to.

    CC (and BCC, but the B stands for "Blind" so it's not marked as a copy) are traditionally used to send
    it to others that need to see the letter as well without making any changes to it.

    With SMTP, I think you just use another RCPT TO and perhaps add a CC: blah
    and in the data section of the email...
  4. 01 Sep 2004 at 14:11
    I want to ask the same question as well.
  5. 14 May 2004 at 05:46
    Yes, but what if you want to code a filter application? Then you need to RECEIVE SMTP BEFORE sending it to SMTP and then POP3 servers.

    So to RECEIVE SMTP or INTERCEPT SMTP packets, does one need to write a local mail server?
  6. 10 Apr 2004 at 08:25

    Looking at the code, I don't see any differentiation between To, CC, and BCC when you are sending them to the Mail server. For that matter, RFC821 doesn't seem to be much help on the matter either.


    How should one identify the three types when doing an SMTP send?

  7. 02 Mar 2004 at 02:06

    In this example, SMTP class inherits tcpclient, is it more suitable to use a udpclient to send and receive e-mail?

  8. 01 Mar 2004 at 14:39

    I searched this site again and found this article


    http://www.developerfusion.com/show/4071/1/


    If I was to strip out all my proprietary code you would end up with this example. Hopefully this will be enough to
    get you started.


    Cheers
    Gord

  9. 27 Feb 2004 at 21:26

    Gordonm,
    thank you so very much for your willingness to share your codes with me. YES, I do want it. A little detail about my program:
    I am trying to built a mail server just like Yahoo or Hotmail, but it's a mini one It's supposed to receive an email then save the sender address, title, message, and date to SQL server 2000 database. I am writing it in C# and ASP.NET.
    Once again, THANK YOU SO MUCH


    Sincerely,
    tinybunny_8

  10. 27 Feb 2004 at 15:10

    I was getting frustrated with not being able to find any examples of this so I wrote my own.


    It is socket based, uses SMTP and seems to work very nicely. If you are interested let me know and perhaps I can post enough of an example to get people going.


    Gord

  11. 27 Feb 2004 at 05:46

    It would appear so at a cursory glance.... I'll email Randy about that....

  12. 27 Feb 2004 at 05:44

    You can't receive email with SMTP - as its only for sending email! You use POP3 instead - see http://www.developerfusion.com/show/4039/

  13. 27 Feb 2004 at 02:52

    I agree with gordonm, if you know how to receive email using SMTP and C#... would you please write one or show me where to start? I am working on project and need a clue so badly. Thanks


    tinybunny_8

  14. 10 Feb 2004 at 18:00

    I would love to see an example of using Sockets to receive email. Could you please do an article covering that area.


    TIA
    Gord

  15. 09 Feb 2004 at 04:41

    You saved my day with this excellent example, thx Randy

  16. 06 Nov 2003 at 14:18

    In your Write() method, you declare a byte array of size 1024.  Is that the maximum string length that you can send to the Write() method at once?

  17. 01 Jan 1999 at 00:00

    This thread is for discussions of Custom SMTP in C#.

Leave a comment

Sign in or Join us (it's free).

Randy Charles Morin Randy's article are Copyright 1998-2003 Randy Charles Morin

Related discussion

Related podcasts

Want to stay in touch with what's going on? Follow us on twitter!