2
/****************************************************************************
4
| Copyright (c) 2007 Novell, Inc.
7
| This program is free software; you can redistribute it and/or
8
| modify it under the terms of version 2 of the GNU General Public License as
9
| published by the Free Software Foundation.
11
| This program is distributed in the hope that it will be useful,
12
| but WITHOUT ANY WARRANTY; without even the implied warranty of
13
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
| GNU General Public License for more details.
16
| You should have received a copy of the GNU General Public License
17
| along with this program; if not, contact Novell, Inc.
19
| To contact Novell about this file by physical or electronic mail,
20
| you may find current contact information at www.novell.com
22
| Author: Per Arneng <pt99par@student.bth.se>
23
|***************************************************************************/
27
using System.Collections;
29
using System.Security.Cryptography;
31
namespace Simias.Mail {
34
internal class SmtpStream
37
protected Stream stream;
38
protected Encoding encoding;
39
protected SmtpResponse lastResponse;
40
protected string command = "";
42
public SmtpStream( Stream stream )
45
encoding = new ASCIIEncoding();
48
public Stream Stream {
52
public SmtpResponse LastResponse {
53
get { return lastResponse;}
56
public void WriteRset()
61
CheckForStatusCode( 250 );
65
public void WriteHelo( string hostName )
67
command = "HELO " + hostName;
70
CheckForStatusCode( 250 );
74
public void WriteMailFrom( string from )
76
command = "MAIL FROM: <" + from + ">";
79
CheckForStatusCode( 250 );
83
public void WriteRcptTo( string to )
85
command = "RCPT TO: <" + to + ">";
88
CheckForStatusCode( 250 );
93
public void WriteData()
98
CheckForStatusCode( 354 );
102
public void WriteQuit()
105
WriteLine( command );
107
CheckForStatusCode( 221 );
111
public void WriteBoundary( string boundary )
114
WriteLine( "\r\n--{0}" , boundary );
118
public void WriteFinalBoundary( string boundary )
121
WriteLine( "\r\n--{0}--" , boundary );
125
// single dot by itself
126
public void WriteDataEndTag()
129
WriteLine( command );
131
CheckForStatusCode( 250 );
136
public void WriteHeader( MailHeader header )
139
foreach( string key in header.Data.AllKeys )
140
WriteLine( "{0}: {1}" , key , header.Data[ key ] );
142
// write the header end tag
146
public void CheckForStatusCode( int statusCode )
149
if( LastResponse.StatusCode != statusCode )
153
"Server reponse: '" + lastResponse.RawResponse + "';" +
154
"Status code: '" + lastResponse.StatusCode + "';" +
155
"Expected status code: '" + statusCode + "';" +
156
"Last command: '" + command + "'";
158
throw new SmtpException( msg );
164
// write buffer's bytes to the stream
165
public void WriteBytes( byte[] buffer )
167
stream.Write( buffer , 0 , buffer.Length );
170
// writes a formatted line to the server
171
public void WriteLine( string format , params object[] args )
173
WriteLine( String.Format( format , args ) );
176
// writes a line to the server
177
public void WriteLine( string line )
179
byte[] buffer = encoding.GetBytes( line + "\r\n" );
181
stream.Write( buffer , 0 , buffer.Length );
188
// read a line from the server
189
public void ReadResponse( )
193
byte[] buffer = new byte[ 4096 ];
195
int readLength = stream.Read( buffer , 0 , buffer.Length );
200
line = encoding.GetString( buffer , 0 , readLength );
202
line = line.TrimEnd( new Char[]{ '\r' , '\n' , ' '} );
206
// parse the line to the lastResponse object
207
lastResponse = SmtpResponse.Parse( line );
214
ISimiasLog logger = SimiasLogManager.GetLogger(typeof(SmtpStream));
216
private void DebugPrint( string line )