/// <summary> /// Constructor is used for NUnit tests and SmtpStreamHandler(TcpClient). /// </summary> /// <param name="stream"></param> public SmtpStreamHandler(Stream stream) { this._CurrentTransportMIME = SmtpTransportMIME._7BitASCII; // Use new UTF8Encoding(false) so we don't send BOM to the network stream. this.ClientStreamReaderUTF8 = new StreamReader(stream, _UTF8Encoding); this.ClientStreamWriterUTF8 = new StreamWriter(stream, _UTF8Encoding); this.ClientStreamReaderASCII = new StreamReader(stream, Encoding.ASCII); this.ClientStreamWriterASCII = new StreamWriter(stream, Encoding.ASCII); }
public SmtpServerTransaction() { RcptTo = new List <string>(); MessageDestination = Enums.MessageDestination.Unknown; _hasMailFrom = false; Data = string.Empty; // Default value is set to 8bit as nearly all messages are sent using it. // Also some clients will send 8bit messages without passing a BODY parameter. TransportMIME = SmtpTransportMIME._8BitUTF; }
/// <summary> /// Say EHLO/HELO to the server. /// Will also check to see if 8BITMIME is supported. /// </summary> /// <param name="failedCallback">Action to call if hello fail.</param> private async Task <MantaOutboundClientSendResult> ExecHeloAsync() { // We have connected to the MX, Say EHLO. await SmtpStream.WriteLineAsync("EHLO " + _VirtualMta.Hostname); string response = await SmtpStream.ReadAllLinesAsync(); if (response.StartsWith("421")) { return(new MantaOutboundClientSendResult(MantaOutboundClientResult.ServiceNotAvalible, response, _VirtualMta, _MXRecord)); } try { if (!response.StartsWith("2")) { // If server didn't respond with a success code on hello then we should retry with HELO await SmtpStream.WriteLineAsync("HELO " + _VirtualMta.Hostname); response = await SmtpStream.ReadAllLinesAsync(); if (!response.StartsWith("250")) { TcpClient.Close(); return(new MantaOutboundClientSendResult(MantaOutboundClientResult.ServiceNotAvalible, response, _VirtualMta, _MXRecord)); } } else { // Server responded to EHLO // Check to see if it supports 8BITMIME if (response.IndexOf("8BITMIME", StringComparison.OrdinalIgnoreCase) > -1) { _DataTransportMime = SmtpTransportMIME._8BitUTF; } else { _DataTransportMime = SmtpTransportMIME._7BitASCII; } // Check to see if the server supports pipelining _CanPipeline = response.IndexOf("PIPELINING", StringComparison.OrdinalIgnoreCase) > -1; } } catch (IOException) { // Remote Endpoint Disconnected Mid HELO. return(new MantaOutboundClientSendResult(MantaOutboundClientResult.ServiceNotAvalible, response, _VirtualMta, _MXRecord)); } return(new MantaOutboundClientSendResult(MantaOutboundClientResult.Success, null, _VirtualMta, _MXRecord)); }
/// <summary> /// Set MIME type to be used for reading/writing the underlying stream. /// </summary> /// <param name="mime">Transport MIME to begin using.</param> public void SetSmtpTransportMIME(SmtpTransportMIME mime) { _CurrentTransportMIME = mime; }
/// <summary> /// Say EHLO/HELO to the server. /// Will also check to see if 8BITMIME is supported. /// </summary> /// <param name="failedCallback">Action to call if hello fail.</param> public async Task <bool> ExecHeloAsync(Action <string> failedCallback) { if (!base.Connected) { return(false); } _LastActive = DateTime.UtcNow; IsActive = true; // We have connected to the MX, Say EHLO. _LastActive = DateTime.UtcNow; await SmtpStream.WriteLineAsync("EHLO " + MtaIpAddress.Hostname); string response = await SmtpStream.ReadAllLinesAsync(); if (response.StartsWith("421")) { failedCallback(response); } _LastActive = DateTime.UtcNow; try { if (!response.StartsWith("2")) { // If server didn't respond with a success code on hello then we should retry with HELO await SmtpStream.WriteLineAsync("HELO " + MtaIpAddress.Hostname); response = await SmtpStream.ReadAllLinesAsync(); _LastActive = DateTime.UtcNow; if (!response.StartsWith("250")) { failedCallback(response); base.Close(); } } else { // Server responded to EHLO // Check to see if it supports 8BITMIME if (response.IndexOf("8BITMIME", StringComparison.OrdinalIgnoreCase) > -1) { _DataTransportMime = SmtpTransportMIME._8BitUTF; } // Check to see if the server supports pipelining if (response.IndexOf("PIPELINING", StringComparison.OrdinalIgnoreCase) > -1) { _CanPipeline = true; } } } catch (IOException) { // Remote Endpoint Disconnected Mid HELO. Most likly Yahoo throttling. } _HasHelloed = true; _LastActive = DateTime.UtcNow; IsActive = false; return(true); }