// Author: Bill Farley // Date: 4/8/2001 // Time: 11:43:38 AM //Compile: csc /r:System.Net.dll /r:System.IO.dll /r:message.dll /target:library SMTPMailer.cs using System.Net.Sockets; using System.IO; using System.Net; using System; //Compile: csc /r:System.Net.dll /r:System.IO.dll /r:message.dll /target:library SMTPMailer.cs public class SMTPMailer { string SMTPServer; string host; public SMTPMailer(string server, string _host){ SMTPServer = server; host = _host; } public void Send(Message msg) { TCPClient sender = new TCPClient(SMTPServer,25); Byte[] outbytes; string input; try{ NetworkStream ns = sender.GetStream(); StreamReader sr = new StreamReader(sender.GetStream() ); sr.ReadLine(); input = "HELO " + host + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); input = "MAIL FROM: " +"<" + msg.From + ">" + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); input = "RCPT TO: " + "<" + msg.To + ">" + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); input = "DATA" + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); input ="Subject: " + msg.Subject + "\r\n" + msg.Body + "\r\n" + "." + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); input ="QUIT" + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length) ; sr.ReadLine(); sr.Close(); ns.Close(); } catch(InvalidOperationException ioe){ Console.WriteLine(ioe.ToString() ); Console.WriteLine("Could not connect to mail server"); } } }